top of page

LED Bar Graph

Creating a bar graph using individual LEDs involves representing data visually by turning on a series of LEDs, where the number of lit LEDs corresponds to the value or magnitude of the data. Each LED represents a segment of the bar, and the more LEDs that are lit, the higher the value being represented.

Materials

LED Bar Graph

To build a simple LED graph project with an Arduino Uno, you'll need the following materials:

Components:

 

1. Arduino Uno (or compatible board):
The microcontroller that will control the LEDs based on the input from the potentiometer.


2. LEDs (10):
Used to represent the bar graph visually. Each LED corresponds to a segment of the bar.


3. Resistors (10x 220 ohms):
One resistor for each LED to limit the current and protect the LEDs from burning out.


4. Potentiometer (1):
An adjustable resistor used to vary the input voltage to the Arduino, allowing you to control the number of lit LEDs.


5. Breadboard:
A platform to easily connect and organize your components.


6. Jumper Wires:
Used to connect the components together on the breadboard and to the Arduino.


7. USB Cable:
Connects the Arduino to your computer for programming and power.

Basic Setup

Breadboard.png
Schematic.png

1) Insert LEDs into the Breadboard:

​

  • Place 10 LEDs on the breadboard in a row or spread them out according to your preference.


 2) Connect the LEDs:

 

  • Anode (long leg) of each LED: Connect to separate digital pins on the Arduino (e.g., pins 2 through 11). Use jumper wires to make these connections.

 

  • Cathode (short leg) of each LED: Connect to one end of a 220-ohm resistor. Place a resistor for each LED.

​​

  • Other end of each resistor: Connect to the GND (ground) rail on the breadboard.


3) Connecting the Potentiometer:

Middle Wiper Pin (wiper or center pin): Connect to the A0 (analog input) pin on the Arduino. This pin reads the variable resistance as an analog value.


One Outer Pin: Connect to the 5V pin on the Arduino. This provides the power supply for the potentiometer.


Other Outer Pin: Connect to the GND (ground) rail on the breadboard. This completes the circuit for the potentiometer.


4) Power and Ground Connections:


Connect the GND pin on the Arduino to the GND rail on the breadboard.


Connect the 5V pin on the Arduino to the 5V rail on the breadboard, if not already done for the potentiometer.

Code
LED Bar Graph.png

CODE BREAK-DOWN

Code Break Down

Explanation:

​

1) const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

  • const: This keyword declares a constant variable, meaning its value cannot be changed once set.

  • int ledPins[] = {...};: This declares an array of integers named ledPins, where each element represents a digital pin on the Arduino that controls an LED. The array contains the pin numbers from 2 to 11.


2) const int numLeds = 10;

  • numLeds: This integer variable represents the total number of LEDs used in the bar graph. Here, it’s set to 10, corresponding to the 10 LEDs connected to the Arduino.


3) for (int i = 0; i < numLeds; i++) { ... }:
This for loop iterates through each LED pin defined in the ledPins[] array.
pinMode(ledPins[i], OUTPUT);: Inside the loop, this function sets each pin in the ledPins[] array as an output. This is necessary because the Arduino will be sending voltage to the LEDs to turn them on or off.


4) int value = analogRead(A0);:
analogRead(A0);: This function reads the analog value from pin A0, where the potentiometer is connected. The potentiometer's position will vary the voltage between 0 and 5V, which corresponds to a digital value between 0 and 1023.
The read value is stored in the value variable.


int ledLevel = map(value, 0, 1023, 0, numLeds);:

5) map(value, 0, 1023, 0, numLeds);:

The map() function scales the value from one range to another. Here, it takes the value (which ranges from 0 to 1023) and maps it to a range from 0 to numLeds (0 to 10 in this case).
The result is stored in the ledLevel variable, representing how many LEDs should be turned on based on the potentiometer's position.


for (int i = 0; i < numLeds; i++) { ... }:
This for loop iterates over each LED in the ledPins[] array.

if (i < ledLevel) { ... }:
Checks if the current index i is less than ledLevel. If it is, the corresponding LED should be turned on.

digitalWrite(ledPins[i], HIGH);:
Turns on the LED at the current index i by setting the corresponding pin to HIGH (5V).
else { ... }:
If the current index i is greater than or equal to ledLevel, the corresponding LED should be turned off.

digitalWrite(ledPins[i], LOW);:
Turns off the LED at the current index i by setting the corresponding pin to LOW (0V).

 

delay(100);:
This introduces a 100-millisecond delay between each loop iteration. It helps to stabilize the readings and prevents flickering of the LEDs.

bottom of page