Arduino: Potentiometer Diagrams & Code
Arduino: Potentiometer Diagrams & Code
Some projects require the use of the serial monitor in your Arduino IDE program (or whatever you are using to
transfer code to the Arduino).
void setup() {
Serial.begin(9600); // initialize the serial communication
// Note: analog pins are automatically set as inputs
}
void loop() {
int potValue = analogRead(A0); // get a reading from the potentiometer on A0
Serial.println(potValue); // print out the value you read
delay(100); // a delay makes values easier to read
}
3/2018
Brown County Library
/*
Potentiometers 02 : Changing Potentiometer Reading to a Percentage
Source: Code adapted from Jeremy Blum's Exploring Arduino
(http://www.exploringarduino.com/content/ch6/)
*/
void setup() {
Serial.begin(9600); // initialize the serial communication
// Note: analog pins are automatically set as inputs
}
void loop() {
potValue = analogRead(potPin); // get a reading from the potentiometer, assign the name potValue
percent = map(potValue, 0, 1023, 0, 100); // convert reading to a percentage
3/2018
Brown County Library
Project 03: Blink LED Based on Potentiometer Reading
Components needed:
Arduino Uno board
breadboard
5 jumper wires
10k potentiometer
220 ohm resistor
LED
3/2018
Brown County Library
/*
Potentiometers 03 : Blink LED Based on Potentiometer Reading
Source: Code adapted from SparkFun Inventor's Kit Example Sketch 2
(https://learn.sparkfun.com/tutorials/sik-experiment-guide-for-arduino---v33/experiment-2-reading-a-
potentiometer)
*/
void setup() {
pinMode(ledPin, OUTPUT); // LED is as an output
Serial.begin(9600); // initialize the serial communication
// Note: analog pins are automatically set as inputs
}
void loop() {
potValue = analogRead(potPin); // read the value from the sensor and assign the name potValue
Serial.println(potValue); // print out the value you read
3/2018
Brown County Library
Project 04: Control Two LEDs with a Potentiometer
Components needed:
Arduino Uno board
breadboard
6 jumper wires
10k potentiometer
2 x 220 ohm resistors
2 x LEDs (two different colors, if possible)
3/2018
Brown County Library
/*
Potentiometers 04 : Control Two LEDs with a Potentiometer
Source: Code adapted from Jeremy Blum's Exploring Arduino
(http://www.exploringarduino.com/content/ch6/)
and "Getting Started with Arduino" by Banzi/Shiloh (3rd ed.)
*/
void setup() {
pinMode(redPin, OUTPUT); // red LED is as an output
pinMode(greenPin, OUTPUT); // green LED is as an output
// Note: analog pins are automatically set as inputs
}
void loop() {
potValue = analogRead(potPin); // read the value from the potentiometer and assign the name potValue
percent = map(potValue, 0, 1023, 0, 100); // convert potentiometer reading to a percentage
3/2018
Brown County Library
Project 05: Control a RGB LED with a Potentiometer
Components needed:
Arduino Uno board
breadboard
8 jumper wires
10k potentiometer
3 x 220 ohm resistors
RGB LED (common cathode)
3/2018
Brown County Library
/*
Potentiometers 05 : Control a RGB LED with a Potentiometer
Source: Code adapted from SparkFun Inventor's Kit Example Sketch 10
(https://learn.sparkfun.com/tutorials/sik-experiment-guide-for-arduino---v33/experiment-10-reading-a-
soft-potentiometer)
*/
void setup() {
// No need for any code here
// Note: analog pins are automatically set as inputs
}
void loop() {
potValue = analogRead(potPin); // read the value from the potentiometer and assign the name potValue
setRGB(potValue); //Set a RGB LED to a position on the "rainbow" of all colors based on the potValue
}
void setRGB(int RGBposition) { // a new function to make the "rainbow" of colors possible
int mapRGB1, mapRGB2, constrained1, constrained2; // define varibles that we need in this function
mapRGB1 = map(RGBposition, 0, 341, 255, 0); // the function maps each potentiometer value to a specifc color
constrained1 = constrain(mapRGB1, 0, 255); // combination of the three RGB lights
blueValue = constrain(map(RGBposition, 341, 682, 0, 255), 0, 255) //Create the blue peak
- constrain(map(RGBposition, 682, 1023, 0, 255), 0, 255);
3/2018
Brown County Library
Project 05a: Control a RGB LED with a Soft Potentiometer
Components needed:
Arduino Uno board
breadboard
8 jumper wires
Soft potentiometer (example)
3 x 220 ohm resistors
1 x 10k ohm resistor
RGB LED (common cathode)
Use the same code from Project 05, but adjust your setup as follows. Then replace the regular potentiometer
with a soft potentiometer (plugging the legs into the same lines on the breadboard).
3/2018
Brown County Library
Ideas to Build On
Learn more about how the soft potentiometer works by running some simple code to see a graph of where
you finger is on the slider:
https://learn.sparkfun.com/tutorials/softpot-hookup-guide#example-circuit
Learn More
Want to learn more about how potentiometers work? Try these resources:
Sparkfun SIK Experiment Guide for Arduino V4.0 – Circuit 1B: Potentiometer.
https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v40/circuit-1b-potentiometer
Sparkfun SIK Experiment Guide for Arduino V3.3 – Experiment 10: Reading a Soft Potentiometer.
https://learn.sparkfun.com/tutorials/sik-experiment-guide-for-arduino---v33/experiment-10-reading-a-soft-
potentiometer