Tutorial 02. Potentiometer control

MEAP has a few built-in devices for user input. The first ones we will look at are the two general purpose potentiometers (if you're unfamiliar with this term it is engineering-speak for a knob). They lie on either side of the volume knob with Potentiometer 0 being to its left and Potentiometer 1 being to its right.

As you move through the tutorials and your own compositional journey you may end up using these potentiometers to control abstract features of a composition such as timbres or the amount of entropy, but for now we are going to use it as a frequency control for our oscillator.


  1. We will begin with the code we ended with in Tutorial 01 where we created a sine wave oscillator and connected it to the output. The two things we need to do in this tutorial are:
    • Read data from the potentiometer
    • Use that data to control our oscillator's frequency.
  2. Getting data from the potentiometer is easy, the meap.readInputs() function called in updateControl() continuously reads all of our inputs including the potentiometers. It stores the potentiometers' positions in an array called meap.pot_vals where the rotational position of Potentiometer 0 is stored at index 0 and Potentiometer 1's potition is stored at index 1. The position is stored as an integer between 0 and 4095 (which makes it an unsigned 12-bit integer if you want to think of it that way), with 0 indicating the potentiometer is turned fully counter-clockwise and 4095 indicating it is turned fully clockwise.
  3. Control rate is plenty fast for user inputs like this so let's add the following two lines of code to updateControl().

    float my_freq = map(meap.pot_vals[0], 0, 4095, 20, 2000);

    my_sine.setFreq(my_freq);

    In the first line, we grab the current position of the potentiometer using meap.pot_vals[0]. This value could be anywhere from 0 to 4095 and if we used that value directly to control the oscillator's frequency, we could end up with an oscillator playing frequencies below 20Hz (which is the lower limit of human hearing). To avoid this, we want to scale this number in between 0-4095 to a new range. The Arduino library contains a convenient function called map() which will help us do this. map() takes a number in one range and transposes it to another, provided by the programmer. I chose our output range somewhat arbitrarily to be 100-2000. As an example, if the potentiometer is turned fully clockwise, we will get a reading of 4095 from meap.pot_vals[0] and the map() function will rescale this value to give an output of 2000.

    map() takes 5 arguments.

    • The first is the number that you want to scale
    • The next pair are the minimum and maximum values that this number could be.
    • The final pair are the minimum and maximum values that you want to scale this number to.

    Once we have scales this value, in the second line we use it to set the frequency of our oscillator, just like we had done in setup()

  4. Upload your code, turn potentiometer 0 and you should hear the pitch of the sine follow it.

FULL CODE HERE