Light Detection
Control LED brightness depending on the amount of light
In this program we will learn two different concepts.
Control brightness of LED depending on the amount of light that falls on one of the Light Detecting Resistor (LDR)
Display the analog value read by the two LDR's on a serial monitor.
To begin with, upload the program below and monitor the LED. Move the hand closer to dim the LED. Flash a light on the LDR and the LED turns brighter. The inverse effect (LED brightens with lesser light) is also possible by changing the threshold_val
and modifying the if
condition.
/*
Light Detecting: This program controls LED brightness depending on the amount of Light it detects
At VEEROBOT, we invest time and resources providing this open source code,
Please support VEEROBOT and open-source hardware by purchasing products
from us @ http://veerobot.com
-----------------------------------------------------------------------------
You are free to redistribute it and/or modify it under the terms of the GNU
Lesser General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
This Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
See <http://www.gnu.org/licenses/>
*/
#include "config.h"
int ldr1Reading = 0; // to store analog value of LDR1
int ldr2Reading = 0; // to store analog value of LDR2
int LEDBrightness = 0; // to store the value of LED Brightness
int threshold_val = 50; // Add a threshold to control on and off of LED
void setup() {
Serial.begin(9600); // initializing serial communication.
pinMode(LED_BUILTIN, OUTPUT); // Defining LED pin as output
}
void loop() {
ldr1Reading = analogRead(LDR1); // Reading LDR1 value
ldr2Reading = analogRead(LDR2); // Reading LDR2 value
Serial.println((String)"LDR1:" + ldr1Reading + " LDR2:" + ldr2Reading); // Printing to serial monitor
if (ldr1Reading > threshold_val) { // Condition to make LED ON
LEDBrightness = map(ldr1Reading, 0, 1023, 0, 255); // Converting LDR to LED Brightness
analogWrite(LED_BUILTIN, LEDBrightness); // Writing Brightness to LED
}
else {
analogWrite(LED_BUILTIN, 0); // If LDR is below threshold make LED OFF
}
delay(300); // delay to make output readable on serial monitor
}
Serial Monitor is an essential tool when creating projects with Arduino. It can be used as a debugging tool, testing out concepts or to communicate directly with the Arduino board. Its job is to allow you to send messages from your computer to an Arduino board (over USB) and also to receive messages from the Arduino board.
Arduino IDE has Serial Monitor tool integrated with the editor, which means that no external software is required.The command 'Serial.begin(9600)' starts serial communication, so that Arduino can send out commands through the USB connection. The value 9600 is called the 'baud rate' of the connection. This is how fast the data is to be sent. You can change this to a higher value, but you will also have to change the Arduino Serial monitor to the same value.
Once the code is uploaded, Tools -> Serial Monitor opens a new window where all the messages are displayed. You should be able to see LDR values printed on the monitor. For Arduino versions > 2.0, Serial monitor is built into the IDE and does not open a new window.
Last updated