Using a Potentiometer to Control 3 LEDs"

by Zorro-Alpha in Circuits > Arduino

12 Views, 0 Favorites, 0 Comments

Using a Potentiometer to Control 3 LEDs"

How to use Potentiometer ( Controls 3 LEDs).png

Project Overview

In this project, we'll use a potentiometer to control the brightness of three LEDs connected to an Arduino. The potentiometer will act as a variable resistor, changing the voltage and thus controlling the brightness of the LEDs.

Supplies

Materials Needed

  1. Arduino Uno
  2. Breadboard
  3. Potentiometer (10k ohms)
  4. 3 LEDs (any color)
  5. 3 Resistors (220 ohms)
  6. Jumper wires
  7. USB cable (for connecting Arduino to the computer)


Circuit Diagram

Refer to the image you provided for the circuit setup.

  1. Arduino Pins:
  2. Connect the GND pin of the Arduino to the ground rail of the breadboard.
  3. Connect the 5V pin of the Arduino to the positive rail of the breadboard.
  4. Connect pins 9, 10, and 11 of the Arduino to three different rows on the breadboard (for controlling the LEDs).
  5. Potentiometer Connections:
  6. The left pin of the potentiometer is connected to the 5V rail.
  7. The right pin is connected to the GND rail.
  8. The middle pin is connected to analog pin A0 of the Arduino.
  9. LED Connections:
  10. Connect the anode (long leg) of each LED to the corresponding digital pins (9, 10, and 11).
  11. Connect the cathode (short leg) of each LED to one end of a 220-ohm resistor.
  12. Connect the other end of each resistor to the GND rail of the breadboard.

Arduino Code

cpp
Copy code
// Pin definitions
const int potPin = A0; // Potentiometer pin
const int led1 = 9; // LED 1 connected to pin 9
const int led2 = 10; // LED 2 connected to pin 10
const int led3 = 11; // LED 3 connected to pin 11

void setup() {
// Set LED pins as OUTPUT
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}

void loop() {
int potValue = analogRead(potPin); // Read the potentiometer value (0-1023)

// Map the potentiometer value to the LED brightness range (0-255)
int brightness1 = map(potValue, 0, 1023, 0, 255);
int brightness2 = map(potValue, 0, 1023, 255, 0); // Inverse brightness
int brightness3 = map(potValue, 0, 1023, 127, 255); // Mid-range brightness

// Set the brightness of each LED
analogWrite(led1, brightness1);
analogWrite(led2, brightness2);
analogWrite(led3, brightness3);

delay(10); // Small delay for stability
}

Explanation of the Code

  1. Variables: potPin, led1, led2, and led3 store the pin numbers.
  2. setup(): Sets the LED pins as OUTPUT.
  3. loop(): Continuously reads the potentiometer value and maps it to LED brightness. Each LED will behave differently based on the mapped value, providing visual feedback of the potentiometer position.

Testing the Project

  1. Upload the code to your Arduino using the Arduino IDE.
  2. Turn the potentiometer knob and observe the changes in the brightness of the LEDs. Each LED should show a different pattern of brightness variation.

Conclusion

This project is a simple yet effective way to demonstrate the use of a potentiometer in controlling multiple outputs on an Arduino. It's great for beginners to understand analog input and PWM output in a practical context.



project link

https://www.tinkercad.com/things/lJvZ68s5tP3-how-to-use-potentiometer-controls-3-leds-by-zorro-alpha

and subscribe my channel please.

https://youtube.com/@steam-diy?si=EuZwV7HjRYV-SusJ

thanks