An experiment with LDR and Arduino

1
9335

It is a very simple experiment for Arduino amateurs, kids and beginners. It needs two LEDs, one LDR (Light Dependent Resistor), one tactile switch and some resistors of different values. Once the circuit is connected as shown in figure and activated by turning on the switch, it turns on the first LED, according to luminosity of the room. The LED can also be turned with the same switch. Therefore, it works as a toggle switch. Once activated, whenever the LDR senses that the room is dark, it turns on the second LED automatically and vice versa.

WATCH THE VIDEO

You can set the threshold for sensing darkness in the Arduino sketch.

Resistors details:

a. Resistor connected to LEDs: 330Ohm

b. Resistor connected to LDR: 1k

Arduino Sketch

DOWNLOAD ARDUINO CODE

/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                              %%                                                                                                %%
%% Author: Marcel "vuln" Ribeiro Dantas <mribeirodantas@lais.huol.ufrn.br>  %%
%%                                            %%                                                                                                    %%    
%% This code is licensed under GNU GPLv3 or any other newer version         %%
%% of this license released of your choice (GPLv3+).                %%
%%                                                                          %%
%% A copy of the license can be read in:                                   %%
%% http://www.gnu.org/licenses/gpl-3.0.text                                 %%
%% or in the COPYING file which should follow with this                %%
%% source file one.                                 %%                                                                %%
%%                                        %%                                                                                                %%
%% An LED will be turned on according to your place's luminosity.           %%
%% It is basically performed through a Light Dependent Resistor (LDR).      %%
%% It is an Arduino project. An Arduino Uno Rev 3 has been used.            %%
%%                                                                          %%
%%                                                                          %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */

#define PRESSED LOW
// Digital pins
const int TactileSwitchPin = 2;
const int PowerLED =  12;
const int SensorLED = 5;
const int Threshold = 100;
// Analog pins
const int LDR = 0;
// Calibrating variables
int SensorMin = 1023;
int SensorMax = 0;

int SwitchState = 0;
int power = 0; // Turn on the LDR
int LDRvalue = 0;

void setup() {
  Serial.begin(9600);
  pinMode(PowerLED, OUTPUT);      
  pinMode(TactileSwitchPin, INPUT);
  pinMode(SensorLED, OUTPUT);

  // signals the end of the calibration period
  digitalWrite(PowerLED, HIGH);
  // calibrate during the first five seconds 
  while (millis() < 5000) {
    LDRvalue = analogRead(LDR);

    // record the maximum sensor value
    if (LDRvalue > SensorMax) {
      SensorMax = LDRvalue;
    }

    // record the minimum sensor value
    if (LDRvalue < SensorMin) {
      SensorMin = LDRvalue;
    }
  }
  // signals the end of the calibration period
  digitalWrite(PowerLED, LOW);
}

void loop(){
  Serial.println(LDRvalue);
  delay(200);
  LDRvalue = analogRead(LDR);
  LDRvalue = map(LDRvalue, SensorMin, SensorMax, 0, 255);
  LDRvalue = constrain(LDRvalue, 0, 255);
  if (power == 0) {
    digitalWrite(PowerLED, LOW);
  } else if (power == 1) {
    digitalWrite(PowerLED, HIGH);
  }
  SwitchState = digitalRead(TactileSwitchPin);
  if (SwitchState == PRESSED && power == 0) {     
    power = 1;
  } else if (SwitchState == PRESSED && power == 1) {
    power = 0;
  }
  if (power == 1 && LDRvalue > Threshold) {
    digitalWrite(SensorLED, HIGH);
  } else {
    digitalWrite(SensorLED, LOW);
  }  
}

WATCH THE VIDEO

This project has been derived from fritzing.org. Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

p

BuildCircuit proudly recommends EasyEDA for circuit and PCB design

EasyEDA: A Powerful Free Circuit, Simulation & PCB Design Tool

Register now to use it for free. No Need to download. Lots of resources and Step by step tutorials

 

SHARE

1 COMMENT

LEAVE A REPLY