Arduino - LED Bar Graph
Arduino Project: Potentiometer-Controlled LED Bar Graph
Section titled “Arduino Project: Potentiometer-Controlled LED Bar Graph”This project demonstrates a powerful and common pattern in physical computing: reading an analog sensor and visualizing its value using a series of digital outputs. We will build an LED bar graph that responds in real-time to the turning of a potentiometer.
Components Required
Section titled “Components Required”- 1 × Arduino Uno R3 or equivalent
- 1 × Breadboard
- 1 × 10kΩ Potentiometer (variable resistor)
- 8 × LEDs (any color)
- 8 × 220Ω Resistors (for current limiting)
- Jumper Wires
Circuit Connections
Section titled “Circuit Connections”Carefully connect the components on the breadboard as described below. A resistor is essential for each LED to prevent damage.
1. Potentiometer:
- Connect one outer pin to the Arduino’s GND pin.
- Connect the other outer pin to the Arduino’s 5V pin.
- Connect the middle pin (the wiper) to Analog Pin A0 on the Arduino.
2. LEDs:
- For each of the 8 LEDs, connect the cathode (shorter leg) to the breadboard’s ground rail (which should be connected to Arduino GND).
- Connect the anode (longer leg) of the first LED to one end of a 220Ω resistor. Connect the other end of that resistor to Digital Pin 2.
- Repeat for the remaining 7 LEDs, connecting them via their own resistors to pins 3, 4, 5, 6, 7, 8, and 9 respectively.
The Sketch
Section titled “The Sketch”Enter the following code into the Arduino IDE or your preferred editor. This code is designed to be readable and easily adaptable.
/* LED Bar Graph Controller
This sketch reads an analog value from a potentiometer and lights up a corresponding number of LEDs in a bar graph fashion.
It demonstrates arrays, for loops, analog input, and value mapping.*/
// --- Constants for Configuration ---const int analogSensorPin = A0; // Pin connected to the potentiometer's wiperconst int ledCount = 8; // The total number of LEDs in our bar graph
// An array to hold the pin numbers for each LED. This makes the code scalable.const int ledPins[ledCount] = {2, 3, 4, 5, 6, 7, 8, 9};
// --- Setup Function (runs once) ---void setup() { // Use a for-loop to iterate through our array of LED pins // and set each one to be an output. for (int i = 0; i < ledCount; i++) { pinMode(ledPins[i], OUTPUT); } // Optional: Start serial communication for debugging Serial.begin(9600);}
// --- Loop Function (runs repeatedly) ---void loop() { // 1. Read the raw sensor value from the potentiometer (0-1023) int sensorReading = analogRead(analogSensorPin);
// 2. Map the sensor's 10-bit range (0-1023) to the number of LEDs (0-8). // The map() function handles this conversion neatly. int ledsToLight = map(sensorReading, 0, 1023, 0, ledCount);
// Optional: Print values to the Serial Monitor to debug and understand Serial.print("Sensor: "); Serial.print(sensorReading); Serial.print("\t -> LEDs to light: "); Serial.println(ledsToLight);
// 3. Loop through the LEDs and turn them on or off. for (int i = 0; i < ledCount; i++) { // If the index 'i' is less than the number of LEDs we want to light, // turn this LED on. if (i < ledsToLight) { digitalWrite(ledPins[i], HIGH); } else { // Otherwise, turn it off. digitalWrite(ledPins[i], LOW); } }
delay(10); // A small delay for stability}How It Works
Section titled “How It Works”The logic of the sketch is straightforward:
- Read the Sensor: In each loop,
analogRead()gets a value from 0 to 1023 from the potentiometer. - Map the Value: The
map()function is key. It proportionally converts the sensor’s wide range (0-1023) into our smaller output range (0-8, the number of LEDs). This tells us how many LEDs should be lit. - Update the Display: A
forloop efficiently iterates through all our LEDs. Anifstatement checks if a particular LED should be on or off based on the mapped value, anddigitalWrite()sets its state accordingly.
Result & Extension Ideas
Section titled “Result & Extension Ideas”When you upload the sketch and turn the potentiometer, you should see the number of lit LEDs change, creating a smooth bar graph effect. From here, you can:
- Use a different sensor: Replace the potentiometer with a photoresistor (to measure light) or a temperature sensor.
- Change the display style: Instead of a bar graph, make only a single LED light up to indicate the level.
- Add PWM Fading: Modify the code to use
analogWrite()to make the last active LED in the bar graph fade smoothly instead of just being on or off.