Arduino Light Sensitive Bar Graph
by akellyirl in Circuits > Arduino
21596 Views, 32 Favorites, 0 Comments
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
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
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
* 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
Wire It Up
Wire up the circuit as shown in the diagrams.
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.
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.
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 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
}
}
}
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 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
}
}
}
Enjoy
Try changing it to "decay" the bar graph using:
decay = max(sensorValue, decay);
and reducing decay each iteration.
See more at:
https://www.inkling.com/read/arduino-cookbook-michael-margolis-2nd/chapter-7/recipe-7-5
decay = max(sensorValue, decay);
and reducing decay each iteration.
See more at:
https://www.inkling.com/read/arduino-cookbook-michael-margolis-2nd/chapter-7/recipe-7-5