Lesson 4 of 13
Turn a knob
Objective: Read a knob with analogRead(), scale it with map(), and set a brightness with PWM: the same signal that sets a motor's speed through a driver.
0% is the GND end, 100% the 5V end. Turning the knob is a simulated input: the sketch does not change.
While the sketch runs, a zoomed-in view shows the output pin switching, and a chart draws the reading and the output over the last 4 seconds.
Stage 1 of 7 · What am I building?
Explore
A knob is how an operator gives a robot an amount: how fast, how far, how bright. The board measures the knob on A0 and sets the output on a ~ pin. Select each part on the circuit, or below.
Pick a part. 0/6 inspected.
Before you start (optional refreshers)
Voltage, current and resistance
Voltage pushes, resistance restricts, current is the flow. Ohm's law ties them together: I = V ÷ R.
Digital HIGH and LOW
A digital pin is either HIGH (about 5 V on an Uno) or LOW (0 V). digitalWrite() sets an output; digitalRead() reports an input.
Timing and delay()
delay(ms) pauses the whole program. Nothing else happens while it waits, so a blink lasts ON delay + OFF delay.
New ideas in this lesson
Voltage dividers
Two resistances in series split the supply voltage. A potentiometer is a divider whose middle leg (the wiper) slides along it: 0 V at the GND end, 5 V at the 5V end, anything between. Change one resistance (a knob, an LDR) and the middle voltage changes.
Analog-to-digital conversion
analogRead() measures 0–5 V on A0–A5 and reports 0–1023: about 4.9 mV per step.
PWM and duty cycle
analogWrite() switches a ~ pin on and off quickly. The ON fraction (0–255 → 0–100%) sets average power. It is not a true analog voltage.
Mapping ranges
map(x, inLo, inHi, outLo, outHi) rescales a value with whole-number maths, so the result is rounded down.
Build it for real · Arduino Uno R3
You need
- Arduino Uno R3 (or compatible) and its USB cable
- Breadboard and jumper wires
- 10 kΩ potentiometer
- LED
- 220 Ω resistor
Wiring
- Potentiometer outer legs → 5V and GND.
- Potentiometer middle leg → A0.
- LED: D9 (a ~ PWM pin) → 220 Ω → LED long leg; short leg → GND.
Before you power up: Connect only the outer legs to 5V and GND. If the middle leg were also tied to 5V or GND, turning the knob to the end would short the supply.
Tip: To see the numbers on a real board, add Serial.begin(9600); to setup() and Serial.println(reading); to loop(), as in Meet the board. A motor never goes on a pin: lesson 9 adds the driver it needs.
The lab models ideal parts. Real circuits add tolerances, noise and loose wires, so check your wiring against this list before you plug in.
// Turn a knob to set a brightness (or, through a driver, a motor's speed)
// Potentiometer: outer legs to 5V and GND, middle leg to A0
// LED: D9 (a ~ pin) through a 220 ohm resistor to GND
const int potPin = A0; // the knob's middle leg
const int ledPin = 9; // a ~ pin: it can make PWM
void setup() {
// nothing to do: analogWrite() makes ledPin an output by itself
}
void loop() {
int reading = analogRead(potPin); // 0 to 1023
int brightness = map(reading, 0, 1023, 0, 255); // 0 to 255
analogWrite(ledPin, brightness); // HIGH for brightness/255 of each cycle
}Press ▶ Run, then turn the knob.