Potentiometer
Analog Input
Arduino Uno
Turn a knob — control LED brightness, servo angle, and RGB colour zones simultaneously. Learn ADC, PWM, and map() all in one project.
What You'll Build & Learn
This beginner-friendly project teaches how to read analog input from a 10kΩ potentiometer with Arduino Uno. One knob controls four outputs simultaneously: LED brightness, RGB colour zones, servo angle, and live Serial Monitor data.
ADC Reading
Understand 10-bit analog-to-digital conversion (0–1023)
PWM Output
Control LED brightness smoothly with analogWrite()
map() Function
Scale ADC values to voltage, %, brightness and servo angle
RGB Zones
Multi-range analog mapping: Red → Green → Blue
Servo Control
Turn the knob to sweep a servo from 0° to 180°
Serial Monitor
Live debugging: view raw ADC, voltage, %, brightness & angle
What You Need
All virtual components are available free in Wokwi — no shopping required.
| # | Component | Qty | Role |
|---|---|---|---|
| 1 | Arduino Uno | ×1 | Main controller — reads the potentiometer and drives all outputs |
| 2 | 10kΩ Potentiometer | ×1 | Variable resistor that sends 0–5V to analog pin A0 |
| 3 | LED (Yellow) | ×1 | PWM brightness control via pin 9 |
| 4 | LEDs (Red, Green, Blue) | ×3 | RGB colour zones mapped to 0–33%, 34–66%, 67–100% of knob |
| 5 | 220Ω Resistors | ×4 | Current limiting for all 4 LEDs |
| 6 | Servo Motor (SG90) | ×1 | Angle controlled 0°–180° by the potentiometer position |
| 7 | Jumper Wires | ×~12 | All component connections |
Circuit Connections
If you paste the diagram.json (see section below), all wiring is automatic. Use this table for manual reference.
Potentiometer
| Pot Pin | Arduino Connection | Wire |
|---|---|---|
| Left Pin | 5V | Red |
| Middle (Wiper) | A0 — variable voltage 0–5V | Green |
| Right Pin | GND | Black |
LEDs & Servo
| Component | Pin | Arduino | Wire |
|---|---|---|---|
| Yellow LED (via 220Ω) | Anode → R → Arduino | Pin 9 (PWM) | Yellow |
| Red LED (via 220Ω) | Anode → R → Arduino | Pin 3 (PWM) | Red |
| Green LED (via 220Ω) | Anode → R → Arduino | Pin 5 (PWM) | Green |
| Blue LED (via 220Ω) | Anode → R → Arduino | Pin 6 (PWM) | Blue |
| All LED Cathodes | Cathode | GND | Black |
| Servo (Red) | V+ | 5V | Red |
| Servo (Brown) | GND | GND | Black |
| Servo (Orange) | PWM Signal | Pin 10 | Orange |
analogWrite().
Try It — Drag the Knob
Drag the slider to simulate rotating the potentiometer. Watch all 4 outputs update in real time.
Live Potentiometer Simulator — Drag to Control
The Control Loop Explained
The Arduino Uno has a 10-bit ADC — it converts 0–5V into a digital value from 0 to 1023.
Read the Potentiometer
analogRead(A0) returns a value from 0 to 1023 corresponding to the wiper voltage (0–5V). This is the raw ADC value.
Convert Using map()
The map() function linearly scales the ADC value to the desired output range. For example:
- Brightness:
map(potValue, 0, 1023, 0, 255) - Servo angle:
map(potValue, 0, 1023, 0, 180) - Percentage:
map(potValue, 0, 1023, 0, 100)
Control LED Brightness (PWM)
analogWrite(LED_PIN, brightness) uses PWM (Pulse Width Modulation) to smoothly vary LED intensity from off (0) to full brightness (255).
RGB LED Colour Zones
Each zone gradually brightens its LED as the knob sweeps through that third — demonstrating multi-range analog mapping.
Servo Motor Control
myServo.write(servoAngle) positions the servo horn from 0° to 180° in direct proportion to the potentiometer rotation.
Serial Monitor Output
Every 100 ms the code prints a formatted table row to the Serial Monitor so you can visualise the ADC conversion in real time:
The Complete Sketch
Paste this into the sketch.ino tab in Wokwi. The code is organised into clean helper functions for each output.
/* * Potentiometer Analog Input with Arduino Uno * Features: LED brightness, servo motor, RGB LED, serial output * Platform: Wokwi Simulator · MakeMindz Summer Course */ #include <Servo.h> // ── Pin Definitions ────────────────────────────── const int POT_PIN = A0; // Potentiometer wiper → A0 const int LED_PIN = 9; // PWM yellow LED const int LED_RED = 3; // RGB Red (PWM) const int LED_GREEN = 5; // RGB Green (PWM) const int LED_BLUE = 6; // RGB Blue (PWM) const int SERVO_PIN = 10; // Servo signal // ── Variables ──────────────────────────────────── int potValue = 0; int brightness = 0; int servoAngle = 0; float voltage = 0.0; int percentage = 0; Servo myServo; unsigned long lastUpdate = 0; const int updateInterval = 100; // ── setup() ────────────────────────────────────── void setup() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT); pinMode(LED_RED, OUTPUT); pinMode(LED_GREEN, OUTPUT); pinMode(LED_BLUE, OUTPUT); myServo.attach(SERVO_PIN); Serial.println("================================="); Serial.println(" Potentiometer Analog Input"); Serial.println("================================="); Serial.println("Raw\tVoltage\tPercent\tBright\tAngle"); Serial.println("-----------------------------------------"); testSequence(); // Boot animation } // ── loop() ─────────────────────────────────────── void loop() { readPotentiometer(); controlLED(); controlRGBLED(); controlServo(); if (millis() - lastUpdate >= updateInterval) { lastUpdate = millis(); displayData(); } delay(10); } // ── Read & Convert ─────────────────────────────── void readPotentiometer() { potValue = analogRead(POT_PIN); voltage = potValue * (5.0 / 1023.0); percentage = map(potValue, 0, 1023, 0, 100); brightness = map(potValue, 0, 1023, 0, 255); servoAngle = map(potValue, 0, 1023, 0, 180); } // ── LED Brightness (PWM) ───────────────────────── void controlLED() { analogWrite(LED_PIN, brightness); } // ── RGB LED Colour Zones ───────────────────────── void controlRGBLED() { if (potValue < 341) { // 0–33%: Red zone analogWrite(LED_RED, map(potValue, 0, 341, 0, 255)); analogWrite(LED_GREEN, 0); analogWrite(LED_BLUE, 0); } else if (potValue < 682) { // 34–66%: Green zone analogWrite(LED_RED, 0); analogWrite(LED_GREEN, map(potValue, 341, 682, 0, 255)); analogWrite(LED_BLUE, 0); } else { // 67–100%: Blue zone analogWrite(LED_RED, 0); analogWrite(LED_GREEN, 0); analogWrite(LED_BLUE, map(potValue, 682, 1023, 0, 255)); } } // ── Servo Control ──────────────────────────────── void controlServo() { myServo.write(servoAngle); } // ── Serial Monitor Output ──────────────────────── void displayData() { Serial.print(potValue); Serial.print("\t"); Serial.print(voltage, 2); Serial.print("V\t"); Serial.print(percentage); Serial.print("%\t"); Serial.print(brightness); Serial.print("\t"); Serial.print(servoAngle); Serial.println("°"); } // ── Boot Test Sequence ─────────────────────────── void testSequence() { Serial.println("Running startup test..."); // Fade LED up and down for (int i=0; i<=255; i+=5) { analogWrite(LED_PIN, i); delay(10); } for (int i=255; i>=0; i-=5) { analogWrite(LED_PIN, i); delay(10); } // Flash RGB colours analogWrite(LED_RED, 255); delay(300); analogWrite(LED_RED, 0); analogWrite(LED_GREEN, 255); delay(300); analogWrite(LED_GREEN, 0); analogWrite(LED_BLUE, 255); delay(300); analogWrite(LED_BLUE, 0); // Servo sweep for (int a=0; a<=180; a+=10) { myServo.write(a); delay(20); } for (int a=180; a>=0; a-=10) { myServo.write(a); delay(20); } Serial.println("Test complete! Ready for input."); }
millis() instead of blocking delay() for the Serial output — this keeps all outputs responsive while printing data every 100 ms.
diagram.json — Paste to Auto-Wire Everything
In Wokwi, click the diagram.json tab, select all (Ctrl+A), delete, paste this, and press Ctrl+S. All components appear and wire themselves automatically.
{ and ends with }. Don't truncate any brackets or the circuit won't load.
{
"version": 1,
"author": "Potentiometer Analog Input",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-arduino-uno",
"id": "uno",
"top": 0, "left": 0, "attrs": {}
},
{
"type": "wokwi-potentiometer",
"id": "pot1",
"top": -57.6, "left": -201.6,
"rotate": 90,
"attrs": { "value": "512" }
},
{
"type": "wokwi-led", "id": "led1",
"top": -96, "left": 153.6,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-resistor", "id": "r1",
"top": -9.6, "left": 153.6,
"rotate": 90, "attrs": { "value": "220" }
},
{
"type": "wokwi-led", "id": "led_red",
"top": -96, "left": 249.6,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led", "id": "led_green",
"top": -96, "left": 297.6,
"attrs": { "color": "green" }
},
{
"type": "wokwi-led", "id": "led_blue",
"top": -96, "left": 345.6,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-resistor", "id": "r2",
"top": -9.6, "left": 249.6,
"rotate": 90, "attrs": { "value": "220" }
},
{
"type": "wokwi-resistor", "id": "r3",
"top": -9.6, "left": 297.6,
"rotate": 90, "attrs": { "value": "220" }
},
{
"type": "wokwi-resistor", "id": "r4",
"top": -9.6, "left": 345.6,
"rotate": 90, "attrs": { "value": "220" }
},
{
"type": "wokwi-servo", "id": "servo1",
"top": 172.8, "left": -288, "attrs": {}
}
],
"connections": [
[ "pot1:GND", "uno:GND.1", "black", ["v0"] ],
[ "pot1:VCC", "uno:5V", "red", ["v0"] ],
[ "pot1:SIG", "uno:A0", "green", ["v0"] ],
[ "led1:A", "r1:1", "yellow", ["v0"] ],
[ "r1:2", "uno:9", "yellow", ["v0"] ],
[ "led1:C", "uno:GND.2", "black", ["v0"] ],
[ "led_red:A", "r2:1", "red", ["v0"] ],
[ "r2:2", "uno:3", "red", ["v0"] ],
[ "led_red:C", "uno:GND.2", "black", ["v0"] ],
[ "led_green:A", "r3:1", "green", ["v0"] ],
[ "r3:2", "uno:5", "green", ["v0"] ],
[ "led_green:C", "uno:GND.2", "black", ["v0"] ],
[ "led_blue:A", "r4:1", "blue", ["v0"] ],
[ "r4:2", "uno:6", "blue", ["v0"] ],
[ "led_blue:C", "uno:GND.2", "black", ["v0"] ],
[ "servo1:GND", "uno:GND.3", "black", ["v0"] ],
[ "servo1:V+", "uno:5V", "red", ["v0"] ],
[ "servo1:PWM", "uno:10", "orange", ["v0"] ]
],
"dependencies": {}
}
Open the Pre-Built Simulation
Circuit already wired with code loaded — just click ▶ Play and rotate the potentiometer dial.
Quick Quiz
Test your understanding of analog input and ADC conversion. Select an answer and click Check.
1 Why does analogRead() return values from 0 to 1023?
2 Which pin receives the potentiometer wiper output?
3 What colour does the RGB LED show when potValue = 200 (≈20%)?
4 If analogRead(A0) returns 768, what servo angle will be set?
5 Why must analogWrite() use PWM pins (3, 5, 6, 9, 10, 11)?
Frequently Asked Questions
Q1: Why does analogRead() give values from 0–1023?
Because the Arduino Uno uses a 10-bit ADC (2¹⁰ = 1024 discrete levels). The 5V supply is divided into 1024 steps, giving a resolution of about 4.9 mV per step.
Q2: Can I use this to control LED brightness?
Yes! Use map() to scale the ADC value to 0–255, then pass it to analogWrite() for smooth PWM brightness control.
Q3: Is this project beginner-friendly?
Absolutely. This is one of the first projects every Arduino learner should build. It teaches ADC, PWM, and the map() function — the three most fundamental analog skills.
Q4: What is the difference between analogRead() and digitalRead()?
digitalRead() returns only HIGH or LOW (1 or 0). analogRead() returns any value from 0 to 1023, capturing the exact voltage level — essential for variable sensors like potentiometers.
Comments
Post a Comment