Zach Christensen
arduino journey
P05bTiming and BrightnessSession 5✓ Working

Knob-Controlled Brightness

A pot, an LED, and the first loop where something outside the board decides what happens.

Carried forward fromP05 PWM Fade
Running
Notebook

Worked out by hand before anything got wired. Why that matters.

The sketch

05b_Knob_Brightness.ino
// P05b - Knob-controlled LED brightness// Read a potentiometer (0-1023), rescale to PWM range (0-255), drive the LED.// This is the read -> map -> write loop: the atom of sensor-driven actuation.const uint8_t PIN_POT = A0; // pot wiper -> A0const uint8_t PIN_LED = 9; // LED via 220Ω -> GND (PWM pin)void setup() {  pinMode(PIN_LED, OUTPUT); // note: analog INPUT pins need no pinMode  Serial.begin(9600);}void loop() {  int raw = analogRead(PIN_POT); // READ: 0..1023 from the knob  int duty = map(raw, 0, 1023, 0, 255); // MAP: rescale to 0..255  analogWrite(PIN_LED, ((long)duty * duty) >> 8); // WRITE: drive the LED brightness    Serial.print("raw="); Serial.print(raw);  Serial.print(" duty="); Serial.println(duty);  delay(20); // ~50 updates/sec, and keeps Serial readable}

Part 2 of the same session. The fade from PWM Fade with the counter torn out and a 10 kΩ pot dropped in its place. Read A0, rescale, write D9, about 50 times a second.

What the knob actually is

A pot is a voltage divider you can turn. The track is a resistive strip with 5 V at one end and GND at the other, so a continuous voltage gradient is sitting there in the material. The wiper is a sliding contact that touches one point on it and carries that point's voltage out of the middle pin.

Which means the two "resistors" are only track above the wiper and track below it. They always add to 10 kΩ and the knob just moves the split. Wiper sitting at 2 kΩ over 8 kΩ reads 4 V.

The useful part falls straight out of that. V at the wiper is Vcc × fraction, and the track resistance cancels, so a 1 kΩ pot and a 100 kΩ pot read the same voltage at the same angle. A divider sets a ratio, not a value. It also means the reading is ratiometric, so if the supply sags to 4.8 V the fraction is unchanged. Mechanically it is an angle sensor that answers in volts.

What I learned