# Arduino Light Sensitive Bar Graph

19,269

31

4

## Introduction: Arduino Light Sensitive Bar Graph

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.

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

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## 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. (http://www.oomlout.co.uk/prototyping-bundle-for-arduino-ardp-p-186.html)
* 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:

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
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 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() {
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 it to "decay" the bar graph using:

decay = max(sensorValue, decay);

and reducing decay each iteration.

See more at:

44 4.3K
28 3.4K
169 10K