All courses

Arduino C++ · Lesson 38 of 40

Build a responsive light controller

Integrated Builds and Next Steps

Combine input, output, and observation

Concept illustration connecting a potentiometer, microcontroller, LED, and pulse signals
From an analog input to a digital output. Concept illustration, not a wiring diagram.

Build a potentiometer-controlled LED dimmer on an Uno R3. You need a 10 kilohm potentiometer, LED, 330 ohm resistor, breadboard, and jumpers. The task combines ADC scaling, PWM output, and independent serial reporting.

With power disconnected, connect the potentiometer's outer pins to 5V and GND, and its wiper to A0. Connect D9 through the 330 ohm resistor to the LED anode, and the cathode to GND. Confirm the wiper pin from the part's pinout. The LED series path is the same circuit used in the foundations lesson.

SignalPurposeRange on Uno R3
A0Read knob voltage0–1023 ADC codes
D9LED PWM output0–255 duty command
SerialReport raw reading9600 baud

The PWM pin switches between logic levels. It does not provide a smoothly adjustable DC voltage to arbitrary devices. An LED's perceived brightness is also not linearly proportional to the numeric command.

Upload a complete controller

Arduino C++
const byte potPin = A0;
const byte ledPin = 9;
unsigned long lastReport = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  const int raw = analogRead(potPin);
  const int duty = raw / 4;
  analogWrite(ledPin, duty);
  const unsigned long now = millis();
  if (now - lastReport >= 250) {
    lastReport = now;
    Serial.print(F("raw=")); Serial.print(raw);
    Serial.print(F(" duty=")); Serial.println(duty);
  }
}

On this board, integer division by four maps 0–1023 to 0–255. ADC sampling and LED updates continue while reporting waits for its interval. Use Serial Monitor at 9600 baud to compare knob position and output.

Acceptance checks: Fully toward GND gives near zero and an off LED. The other end gives near full scale and a bright LED. Mid-position is around half-scale, subject to component tolerance. Response continues between serial reports. If readings drift with the knob disconnected, that is a floating input, not a valid sensor reading.

Extensions: Add a debounced D2 button to enable/disable output, preserving the knob setting. Add a filter and measure the new response time. Write down the desired startup state before changing the program.

Official references for further study

A useful reminder

Clean Code: Use descriptive constants instead of magic numbers

Instead of calling digitalWrite(13, HIGH) directly in your code, define descriptive constants like const int LED_PIN = 13; at the top of your file. This makes your code self-documenting and extremely easy to update if you change your wiring.