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.

​

How It Works

When the potentiometer is turned, its resistance changes, which adjusts the voltage at the analog pin. The Arduino reads this voltage (0–5V) as an analog value (0–1023). The program maps this value to the 10 LEDs, turning on a number of LEDs proportional to the input voltage.

Materials Anchor

Materials

20241209_191630.jpg

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Ω):

  • One resistor for each LED to limit the current and protect the LEDs from burning out.

​​

4) Potentiometer (10kΩ):

  • 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 (Type-A to Type-B)

  • Connects the Arduino to your computer for programming and power.

Setup Anchor

Basic Setup

breadboard_GREY.png
schematic_GREY.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
Code.png

CODE BREAK-DOWN

Code Break Down
  • const int potPin = A0;

    • Declares a constant integer variable named potPin and sets it to A0, which is the analog input pin where the potentiometer is connected.

    • Using const means this variable's value won't change during the program's runtime. Defining it as a constant helps avoid accidental modifications and makes it clear which pin is assigned to the potentiometer.

​

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

    • Declares an array named ledPins that holds the digital pin numbers (2 through 11) where each LED is connected.

    • Arrays are useful here because they allow us to manage all LED pins as a single group, making it easier to control each LED in a loop.

​

  • const int numLEDs = 10;

    • Declares a constant integer numLEDs to represent the total number of LEDs (10 in this case).

    • This is helpful if you need to modify the number of LEDs later; you can change this value instead of rewriting parts of the code that refer to the number of LEDs.

​

  • void setup() { ... }

    • setup() is a function that runs once when the Arduino is powered on or reset. It’s used for initialization.

​

  • for (int i = 0; i < numLEDs; i++) { ... }

    • This for loop goes through each LED pin defined in the ledPins array. It iterates 10 times (from 0 to 9), covering all LEDs.

​

  • pinMode(ledPins[i], OUTPUT);

    • Inside the loop, pinMode() is called to set each pin in ledPins as an OUTPUT. This lets the Arduino control each LED connected to these pins.

    • ledPins[i] accesses each element in the array by index. So on each loop, it sets the respective LED pin to output mode.

​

  • void loop() { ... }

    • loop() is a function that runs continuously after setup(), allowing the Arduino to repeat instructions indefinitely.

​

  • int potValue = analogRead(potPin);

    • Reads the voltage level from the potentiometer connected to A0, which gives a value between 0 and 1023 based on the potentiometer's position.

    • The value is stored in potValue to be used later in the code.

​

  • int numLEDsOn = map(potValue, 0, 1023, 0, numLEDs);

    • Maps the range of potValue (0 to 1023) to the number of LEDs (0 to 10) using the map() function.

    • map(potValue, 0, 1023, 0, numLEDs); converts the potentiometer's value to a number between 0 and 10. If potValue is low, numLEDsOn will be low, meaning fewer LEDs will light up. If potValue is high, more LEDs will light up.

​

  • for (int i = 0; i < numLEDs; i++) { ... }

    • This for loop iterates through each LED.

​

  • if (i < numLEDsOn) { ... }

    • Checks if the current LED (represented by i) is less than numLEDsOn, which indicates how many LEDs should be turned on.

    • For example, if numLEDsOn is 3, the LEDs at indices 0, 1, and 2 will be turned on.

​

  • digitalWrite(ledPins[i], HIGH);

    • Turns on the LED at index i by setting the corresponding pin to HIGH, which applies a voltage and lights up the LED.

​

  • digitalWrite(ledPins[i], LOW);

    • Turns off LEDs that aren’t part of numLEDsOn by setting their pins to LOW, which removes the voltage and keeps them off.

​

  • delay(50);

    • Adds a short delay of 50 milliseconds to avoid rapid, jittery changes in LED lighting, making the changes smoother as the potentiometer is adjusted.

bottom of page