Site Guide

Find your way around the Engineering Club.

Know what you need? Search for a component, concept or project.

Around the site

Start

Labs

TestingExperimental

Build

Club

LearnExperimental

Go a little deeper

Entry points to larger collections. Use Search for individual records.

Learning paths & reference

Inside PiCrawlerExperimental

Inside Inventory

← All labs

LAB 005

Potentiometer: Measure and Dim

Measure a changing voltage on A0 and set the brightness of a resistor-protected LED on PWM pin D9.

Intro labArduino Uno R3

01

What you will build

A knob controls the perceived brightness of an external LED.

Why it matters

Input, measurement, processing and output form a reusable embedded control chain.

What you will learn

  • potentiometer as voltage divider and 10-bit ADC
  • analogRead 0–1023 and PlatformIO Monitor
  • PWM analogWrite 0–255 on Uno D9, proportional control

02

Parts List

  • Arduino Uno R3 is a 5 V ATmega328P microcontroller board for direct control of sensors, switches and small driver circuits using a single uploaded program. UNO-format board with USB-B socket, barrel connector, ATmega328P and digital/analog header labels. The canonical Uno R3 is separate from the generic compatible-board record. Identify pins from the board labels/pinout rather than a diagram for a newer Uno generation.
  • An 830-contact solderless breadboard joins inserted leads through hidden spring strips so circuits can be rebuilt without solder. Full-size rectangular board, center channel, numbered five-hole terminal groups and side power rails. Counted contact holes are not component units.
  • An assortment of discrete indicator LEDs provides several visible colors for status and display experiments. Loose two-lead lens packages of different tints; transparent lenses can emit several different colors.
  • A mixed resistance assortment supplies values for current limiting, biasing and divider experiments. Groups of axial two-lead resistors; use labeled storage and measure uncertain parts out of circuit.
  • A shaft-operated potentiometer forms an adjustable voltage divider for calibration or a user control. Three terminals and a rotating shaft; resistance marking must read 10 kΩ. Identify both track ends and wiper with an unpowered resistance measurement.
  • Male-to-male jumper leads make removable point-to-point connections for low-current prototype signals. Pins at both ends; count individual leads rather than ribbon groups. Check intact insulation and snug contacts before use.
  • A USB-A to USB-B cable carries USB power and, when its data conductors are present, a host/device data link. Rectangular USB-A plug at one end and square USB-B plug at the other; standalone purchase length is recorded as ten feet. Inspect the plug shape; micro-USB is not USB-C, and USB-B is the squarer device connector.

03

Assembly

Disconnect USB before changing breadboard wiring; check connections before reconnecting.

Use one 220–330 Ω resistor in series with each ordinary LED. Never connect 5V directly to GND.

Use Uno 5V and GND only across the potentiometer; never place the pot wiper on a GPIO output.

  1. 01

    Disconnect USB. Identify the potentiometer’s two outer terminals and its center wiper (confirm the pinout if the part is unfamiliar). Connect the outer terminals to Uno 5V and GND and the wiper to A0; keep the LED disconnected for this first test.

  2. 02

    Select Read values in PlatformIO Monitor (02-pot-serial.cpp), replace src/main.cpp with that complete example, then Save, Build and Upload. Open PlatformIO Monitor at 9600 baud and turn the knob; readings should move across roughly 0–1023. The different Read analog values example only changes the onboard LED and does not print serial readings.

  3. 03

    Disconnect USB before adding the output circuit. Build a second branch from Uno D9 through one verified 220–330 Ω series resistor to the external LED anode, then connect its cathode to Uno GND. The LED and potentiometer use the same Uno ground.

  4. 04

    Check the 5V/GND potentiometer connections, A0 wiper, resistor, LED polarity, and D9 output. Select the core program Proportional LED brightness (04-pot-led-brightness.cpp), then Save, Build and Upload.

  5. 05

    Turn the knob through at least three positions and observe the LED brightness. In PlatformIO Monitor, compare the A0 readings (0–1023) with the PWM settings (0–255); D9 changes duty cycle rather than producing a continuously variable voltage.

FromToPurpose / notes
Uno D9LED 1 series resistor 220–330 ΩDrive output with current limitingPut the resistor in series, never across 5V and GND.
LED 1 series resistorLED 1 anode (long leg)Feed LED in forward directionResistor may instead go on the cathode side if it stays in series.
LED 1 cathode (short leg/flat side)Uno GNDComplete the circuitLED must not be connected directly across 5V and GND.
Uno 5Vpotentiometer outer terminal 1Provide top of voltage dividerPower off before moving connections.
Uno GNDpotentiometer outer terminal 2Provide bottom of voltage dividerCommon ground with the LED circuit.
potentiometer center wiperUno A0Measure a 0–5 V variable inputSwitch the outer terminals to reverse knob direction.

Predict before you run

  • Does the knob change the measured number continuously or only HIGH/LOW?
  • At half travel, will duty cycle be near zero, half, or full?
Expected result

At one end of the knob LED is off, at the other it is bright; intermediate values change perceived brightness.

  • With Read values in PlatformIO Monitor (02-pot-serial.cpp) uploaded, confirm the 9600-baud A0 readings change as the knob turns.
  • With Proportional LED brightness (04-pot-led-brightness.cpp) uploaded and the D9 LED connected, verify that brightness changes at three or more knob positions.

04

Code

Development · VS Code + PlatformIO

Arduino Uno R3

Replace src/main.cpp with one complete example. Save → Build → Upload. These labs need no external libraries.

Copy the Uno platformio.ini
platformio.ini
[env:uno]
platform = atmelavr
board = uno
framework = arduino
monitor_speed = 9600

Getting Started: project and board setup →

Code experiments

Open PlatformIO Monitor at 9600 baud after Upload. Close Monitor before another Upload if the port is busy.

04-pot-led-brightness.cpp
#include <Arduino.h>

// A0 wiper; D9 is PWM capable, with resistor and LED to GND.
const int potPin = A0;
// D9 supports PWM on the Uno, which lets us control perceived LED brightness.
const int ledPin = 9;

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

void loop() {
  // Read the potentiometer as a 10-bit number from 0 to 1023.
  int raw = analogRead(potPin);
  // map() scales the input range to 0–255 using integer math; it does not smooth noisy
  // readings.
  int brightness = map(raw, 0, 1023, 0, 255);
  // On the Uno, analogWrite controls PWM duty cycle here, not a smooth analog voltage.
  analogWrite(ledPin, brightness);
  // Print a label, the raw reading, then the PWM value so you can compare input and output.
  Serial.print("A0: ");
  Serial.print(raw);
  Serial.print(" PWM: ");
  Serial.println(brightness);
  // Repeat around every 50 ms to update the brightness and Serial display.
  delay(50);
}

Experiments & challenges

  • Compare 0, near midpoint, and 1023 readings.
  • Swap only the pot outer 5V and GND leads with power off, then predict direction.

Scale it up

Optional threshold program, inverted response, and five-LED bar graph on D8–D12 each make a different decision from the same A0 reading.

Challenge

Map the knob backwards so clockwise dims, or choose three distinct brightness bands.

What to remember

Why can the analog reading be 700, but PWM output accepts 0–255?
analogRead() uses the Uno's 10-bit ADC scale, 0?1023. analogWrite() uses an 8-bit PWM setting, 0?255. The program maps the measured input range to the PWM range before setting brightness.
Is PWM a steady 2.5 V analog output?
No. The PWM pin switches rapidly between LOW and HIGH, approximately 0 V and 5 V on an Uno. At about half duty it is HIGH for about half the time; an average near 2.5 V is not a steady 2.5 V output.

Learning summary

The Uno samples 0–5 V on A0 into 0–1023; PWM on D9 switches rapidly with 0–255 duty-setting values. The full chain is input → read → process → output.

Use this control chain next with light or temperature sensors, RGB LED, buzzer or a separately powered actuator design.

Troubleshooting

No upload or no power

Check USB data cable, selected PlatformIO Uno environment and upload port; verify the power LED.

Unexpected external LED behavior

Disconnect USB, check the breadboard row, LED polarity, series resistor, GND, then compare the code pin with the wire.

Serial always 0 or 1023

Verify wiper goes to A0, both outer terminals reach 5V/GND, and PlatformIO Monitor uses monitor_speed = 9600.

LED only on/off

Use the D9 PWM circuit and brightness program; compare mapped 0–255 values, and verify resistor and polarity.