How to Make a Light Sensitive Bar Graph with Arduino

This is a project for Arduino to make a Light Sensitive Bar Graph, aimed at beginners.

Arduino measures the voltage on a voltage divider composed of a resistor and an LDR (light dependant resistor).
A bar of LEDs are illuminated corresponding to the measured voltage.

* Learn how to use an LDR
* Learn how to illuminate a bar of LEDs according to a measured voltage

Step 1: What you need

You will need:
* Arduino UNO
* Some LEDs
* A 560 Ohm resistor for each LED  (or a value similar to that)
* A 10k Ohm resistor
* An LDR
* Breadboard for wiring up e.g. (
* Some Wires


Step 2: Wire it up


Wire up the circuit as shown in the diagrams.


Step 3: How to Illuminate the LEDS in a Bar

Converting the analogue value to a bar display is accomplished pretty easily with the map function:

sensorValue = analogRead(analogInPin);                   // read the analog in value
ledLevel = map(sensorValue, 0, 700, 0, NbrLEDs);    // map to the number of LEDs

The sensorValue range is 0 to 1023 theoretically. Practically it goes between about 0 and 700 for daylight and the chosen component values.

ledLevel will be 0 to 6 for example (if NbrLEDs = 6), over that same input range.

All you have to do then is use a for loop to turn ON the LEDs based on the ledLevel.

Step 4: Here’s the code

Bargraph sketch example
Recipe 7.5 from the Arduino Cookbook by Michael Margolis
Turns on a series of LEDs proportional to a value of an analog sensor.
Six LEDs are controlled but you can change the number of LEDs by changing
the value of NbrLEDs and adding the pins to the ledPins array

    const int NbrLEDs = 6;
const int ledPins[] = { 13, 12, 11, 10,9, 8};
const int analogInPin = 0; 
// Analog input pin connected to variable resistor
    const int wait = 30;

// Swap values of the following two constants if cathodes are connected to Gnd
    const boolean LED_ON = LOW;
const boolean LED_OFF = HIGH;

int sensorValue = 0;        // value read from the sensor
    int ledLevel = 0;           // sensor value converted into LED ‘bars’

void setup() {
for (int led = 0; led < NbrLEDs; led++)
pinMode(ledPins[led], OUTPUT); 
                            // make all the LED pins outputs

void loop() {
sensorValue = analogRead(analogInPin);                 // read the analog in value
      ledLevel = map(sensorValue, 0, 700, 0, NbrLEDs);  // map to the number of LEDs
      for (int led = 0; led < NbrLEDs; led++)
if (led < ledLevel ) {
digitalWrite(ledPins[led], LED_ON);    
                // turn on pins less than the level
else {
digitalWrite(ledPins[led], LED_OFF);                
// turn off pins higher than the level

Step 5: Enjoy



Try changing the code to “decay” the bar graph using:

decay = max(sensorValue, decay);

and reducing decay each iteration.

See more at: