Teach lesson
Basic Arduino 3/4: control speed with a potentiometer
Students read a potentiometer with analogRead(), transform the reading with map(), and control the blink speed of a real LED.
Learning Outcomes
Read a potentiometer with analogRead.
Interpret the 0-1023 range.
Use map() to turn a reading into a visible action.
Student activity preview
Activity Content
Preview only. In a class session, students can fill in responses and submit their work to the teacher.
From the physical world to a number
9 min
A potentiometer is like a rotary knob: turning it changes the voltage that reaches the analog pin. Arduino does not measure that voltage as a moving needle; it uses an analog-to-digital converter, or ADC, to turn it into a number. On Arduino Uno, analogRead(A0) asks the ADC to sample pin A0 and returns a value from 0 to 1023. Readings near 0 represent one end of the turn, and readings near 1023 represent the other end.
In this practice, that reading will control LED 1's speed. The function map(value, input_min, input_max, output_min, output_max) converts a number from one range to another. Here it converts the 0-1023 reading into a 100-1200 ms delay:
int reading = analogRead(A0);
int waitTime = map(reading, 0, 1023, 100, 1200);
delay(waitTime);
map(reading, 0, 1023, 100, 1200) turns the potentiometer reading into a blink delay: a value near 0 becomes close to 100 ms, and a value near 1023 becomes close to 1200 ms. The same idea is used with analog sensors such as knobs, light sensors, temperature sensors, or position sensors: first read a number, then convert it into a useful action.
According to that fragment, if analogRead(A0) returns a small number, the delay will be...
Make the knob control the LED
18 min
Upload the program. Turn potentiometer 1 and observe how the blink changes. Then change the 100, 1200 range to other values: for example, try a lower maximum delay if you do not want the LED to ever blink so slowly.
const int pot1 = A0;
const int led1 = 8;
void setup() {
pinMode(led1, OUTPUT);
}
void loop() {
int reading = analogRead(pot1);
int waitTime = map(reading, 0, 1023, 100, 1200);
digitalWrite(led1, HIGH);
delay(waitTime);
digitalWrite(led1, LOW);
delay(waitTime);
}Open the lab and find potentiometer 1.
Upload the program.
Test the potentiometer near the minimum, in the middle, and near the maximum.
Change the
100, 1200range to other values and test again.
Submit your code
Capture the code with the delay range that seems clearest to you. Before attaching it, save main.ino in the lab environment. The snapshot must show the program you actually want your teacher to review.
Connect the number and the effect
8 min
Describe what you observed when you moved the potentiometer. What delay range did you choose, and why?