Teach Remote lab lessons

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.

  • Arduino Board (code)
  • 35 min
  • Lower secondary / first physical-programming lessons
  • English
  • Embedded systems
Arduino Board (code)
Arduino Board (code) ยท Lab details

Learning Outcomes

  • Read a potentiometer with analogRead.

  • Distinguish the ADC's theoretical range from the potentiometer's practical 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.

1

From the physical world to a number

9 min

A potentiometer is like a rotary knob. Arduino Uno's default analog-to-digital converter represents 0-5 V with values from 0 to 1023. This laboratory's potentiometer supplies 0-3.3 V, so its useful reading range is approximately 0-675. The code clamps small measurement variations and maps that practical range to LED 1's speed:

const int potMaxReading = 675;
int reading = analogRead(A0);
int calibratedReading = constrain(reading, 0, potMaxReading);
int waitTime = map(calibratedReading, 0, potMaxReading, 100, 1200);
delay(waitTime);

The mapping turns a reading near 0 into a delay close to 100 ms and a reading near the laboratory maximum into a delay close to 1200 ms.

According to that fragment, if analogRead(A0) returns a small number, the delay will be...

2

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;
const int potMaxReading = 675;

void setup() {
  pinMode(led1, OUTPUT);
}

void loop() {
  int reading = analogRead(pot1);
  int calibratedReading = constrain(reading, 0, potMaxReading);
  int waitTime = map(calibratedReading, 0, potMaxReading, 100, 1200);

  digitalWrite(led1, HIGH);
  delay(waitTime);
  digitalWrite(led1, LOW);
  delay(waitTime);
}
  1. Open the lab and find potentiometer 1.

  2. Upload the program.

  3. Test the potentiometer near the minimum, in the middle, and near the maximum.

  4. Change the 100, 1200 range 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.

3

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?