Skip to content
Rob Stave edited this page Nov 10, 2015 · 3 revisions

Reading pots

The easiest way to get an analog value to the arduino is by using a potentiometer as a voltage divider.

There are plenty of good sites that go over this in pretty good detail.

When the chip reads the value of a potentiometer (pot) with the analogRead function, it returns a value from 0 to 1023. This is based on the measured voltage at the pin. The best type of potentiometer to use in this case has a linear taper. That way the voltage read is proportional to the position of the pot.

Mapping values.

The Arduino library has a mapping function that you can use to map the values from the ADC to a more reasonable range.

https://www.arduino.cc/en/Reference/Map

outputValue = map(readValue, 0, 1023, 0, 255);

This maps the values to 0-255.

Other Tapers

Other pots can be used, however you might find the results to be a bit "squeezed". The audio taper, is going to have the lower values spread out and as you reach 100%, everything is going to ramp up real fast. The reason for this is that usually, these parts are used in things like filters and volume circuits where our ears sense things differently than a linear scale. Again, if you can, just use linear pots. But I have a ton of audio ones hanging around.

In the situation where you want a pot to result in say 4 or 5 values, you can map things other ways with a series of if statements.

I prefer this to say

outputValue = map(readValue, 0, 1023, 0, 3);

because sometimes the border cases get odd. Instead I map it to a range like 50 or 100 and if check it

pattern = mapPattern(map(pattern_t, 0, 1023, 0, 40));

//Use this to tweek the feel of the pot.  
//If you are using an audio pot versus a linear, set this value to 0

 #define MAP_VALUES_AS_LINEAR 0

#if MAP_VALUES_AS_LINEAR == 1
  #define STATE0 10
  #define STATE1 20
  #define STATE2 30
  #define STATE3 40
#else
  #define STATE0 5
  #define STATE1 10
  #define STATE2 20
  #define STATE3 40
#endif

int mapPattern (int sample) {

  if (sample <= STATE0) {
    return 0;
  } else if (sample <= STATE1) {
    return 1;
  } else if (sample <= STATE2) {
    return 2;
  } else if (sample <= STATE3) {
    return 3;
  }
  return 0;
}

As I'm writing this, I see that you could totally eliminate the mapping step and use the 0-1023 ranges like

  #define STATE0 128
  #define STATE1 256
  #define STATE2 512
  #define STATE3 1024

https://en.wikipedia.org/wiki/Potentiometer

Clone this wiki locally