Example-3
Controlling LED Brightness with a Potentiometer
Objective
To control the brightness of an LED using a potentiometer with Arduino Uno by converting analog input values into Pulse Width Modulation (PWM) signals.
Learning Outcomes
-
Understand how to connect and use a potentiometer as an analog input.
-
Learn about analogRead() and analogWrite() functions in Arduino.
-
Observe how PWM controls the brightness of an LED.
-
Build confidence in reading values from sensors and mapping them to actuators.
Components (From Tinkercad Palette)
-
Arduino Uno board
-
Breadboard (small)
-
1 × LED
-
1 × 220Ω resistor
-
1 × Potentiometer (10kΩ)
-
Jumper wires (male-to-male)
Circuit Wiring (Exact)
-
LED Connection:
-
LED anode (long leg) → one end of 220Ω resistor → Arduino digital pin 9
-
LED cathode (short leg) → GND
-
-
Potentiometer Connection:
-
Middle pin → A0
-
Left pin → 5V
-
Right pin → GND
Step-by-Step in Tinkercad
-
Open Tinkercad Circuits.
-
Drag Arduino Uno, breadboard, LED, resistor, and potentiometer onto the workspace.
-
Connect the LED to pin 9 (through resistor) and GND.
-
Connect potentiometer: left pin → 5V, right pin → GND, middle pin → A0.
-
Open the Code editor → Select Text mode (C++).
-
Paste the following Arduino program.
int potPin = A0; // Potentiometer connected to A0
int ledPin = 9; // LED connected to PWM pin 9
int potValue = 0; // Store potentiometer value
int ledValue = 0; // Store LED brightness
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
potValue = analogRead(potPin); // Read 0–1023
ledValue = map(potValue, 0, 1023, 0, 255); // Scale to 0–255
analogWrite(ledPin, ledValue); // Output PWM
}
-
Click Start Simulation.
-
Rotate the potentiometer and observe the LED brightness change.
How It Works (Explain to Students)
-
The potentiometer acts like a variable resistor that gives values between 0 and 1023.
-
Arduino reads this analog value using analogRead().
-
The map() function scales it to a PWM range (0–255).
-
The analogWrite() function sends PWM signals to the LED, making it glow dim or bright.
-
Turning the knob clockwise increases brightness; turning it anticlockwise decreases it.
Common Mistakes & Quick Fixes
-
🔴 LED not glowing → Check polarity (long leg should connect via resistor to pin 9).
-
Potentiometer not working → Ensure middle pin is connected to A0.
-
LED always at same brightness → Verify code uses
analogWrite()
, notdigitalWrite()
. -
Error uploading code → Make sure the board selected in Tinkercad is Arduino Uno.
Viva / Check-Your-Understanding
-
What is the function of the potentiometer in this circuit?
-
Why do we use the map() function here?
-
What is PWM and why is it needed to control LED brightness?
-
Which Arduino pins support PWM output?
-
What is the difference between analogRead() and analogWrite()?
Extensions (Mini-Tasks)
-
Replace LED with a small DC motor → observe speed control.
-
Connect two LEDs to different PWM pins and control them with one potentiometer.
-
Try using
Serial.print(potValue)
to display potentiometer readings in Serial Monitor.
Lab Record (Suggested Headings)
-
Aim
-
Circuit diagram (screenshot from Tinkercad)
-
Code listing
-
Output observation (simulation screenshot)
-
Inference/Conclusion
Comments
Post a Comment