Materials: Arduino UNO, breadboard, potentiometer, assorted wire.
As mentioned earlier, Arduino boards are useful to get information from sensors and control devices and to send information to actuators. If the device we are getting information from is analog like a potentiometer, FSR, bend sensor, fader, and so on, then we use an analog pin.
In the picture of the Arduino board below, analog pins are located at the bottom. We will use one of these pins to receive information from the potentiometer. You can also see ground connections labeled GND and two power connections +5V and 3.3V. We will use the 5V pin in this example.
- Making the connections
In this example, our potentiometer goes in the breadboard. Potentiometers are variable resistors that reduce current depending on the resistance value. Therefore, as the resistance changes, the current changes proportionally and this is reflected in the value read in the analog pin. Make the connections as follows:
This way, 5v flows into the potentiometer so that resisted current flows into analog pin 0. Thus the aim of the code is to set analog pin 0 to measure the amount of current coming out of the potentiometer.
The code
The code we will use is located in the example File/Examples/02.Analog/AnalogInput as follows:
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
Again we start by creating a few variables (sensorPin, ledPin, sensorValue), then in the setup routine we set the ledpin to output mode. The loop uses a new function analogRead to measure the incoming current from the analog pin. After that it uses digitalWrite to switch the LED on and off, separated by a delay determined by the sensor value. This way, the LED’s blinking rate becomes a way to monitor the changes in resistance in the potentiometer.
- Monitoring output
Another way to gauge what is being read by your Arduino is to print its values into a monitor. You’ll need two pieces of code to do this. First you need to initialize serial communication by including the following piece of code in the setup routine.
Serial.begin(9600);
Then at the end of your loop routine, before the last “}” bracket, you need to print into the monitor with a line of code like this one:
Serial.println(sensorValue);
In this case, Serial.println will print the value on a new line each time it prints, but you can also use Serial.print to print without moving to the next line.
In order to see the printed output, you need to open the serial monitor by clicking on the Tools/Serial Monitor menu, or pressing Ctl+Shift+M. After doing this, you need to make sure that the monitor has the value “9600 baud” to match the argument in Serial.begin. Finally, though it is a useful tool, the monitor takes over serial communication so it will prevent you from using serial communication elsewhere or from uploading code into the board, so you’ll need to close the monitor to do these things.
- Variations
You should attempt a few variations to understand how to repurpose examples and bits of code for your own purposes. Here are a few ideas:
- What happens if you move the wire going into pin 0 to pin 3. What changes do you need to make to the code to make it work again?
- Switch the potentiometer for a fader, FSR, flex sensor, or other analog sensor.
- Add a second potentiometer, how do you modify the code to do this?