Pages

Sunday, March 7, 2010

LDR and Arduino


This is a practical use for an LDR. The LED on the arduino turns on when there is low light and turns off when there is plenty. This could be useful as a night light.

Press "Read More " To copy the code:




/*

* LDR and Arduino

*

* Reads an input from LDR, sends it to serial monitor and turns a LED off or on.

*

* Code by Joshua Lopez http://joshualopezarduino.blogspot.com/

*/



// variables

int LDR_pin = 0; // analog pin 0 (connect LDR here)

int LDR_val = 0; // variable use to read input data

int LEDpin = 13; //This is the LED



// a threshold to decide when the LED turns on

int threshold = 500;


void setup(){



// declaration of pin modes

pinMode(LDR_pin, INPUT);

pinMode(LEDpin, OUTPUT);


// begin sending over serial port

Serial.begin(9600);

}



void loop(){

// read the value on analog input

LDR_val = analogRead(LDR_pin);



// if value greater than threshold turn on LED

if (LDR_val < threshold) digitalWrite(LEDpin, HIGH); else digitalWrite(LEDpin, LOW);



// output 'LDR_val' value into the console

Serial.print("LDR = ");

Serial.print(LDR_val);



}

2 comments:

  1. How would you do this with 5 LEDs and turn on more as it got darker and less as it got brighter?

    ReplyDelete
  2. I'll tell you how!

    [quote]
    [color=#7E7E7E]/*[/color]
    [color=#7E7E7E]* LDR and Arduino[/color]
    [color=#7E7E7E]*[/color]
    [color=#7E7E7E]* Reads an input from LDR, sends it to serial monitor and turns a LED off or on.[/color]
    [color=#7E7E7E]*[/color]
    [color=#7E7E7E]* Code by Joshua Lopez http://joshualopezarduino.blogspot.com/[/color]
    [color=#7E7E7E]*/[/color]

    [color=#7E7E7E]// variables[/color]
    [color=#CC6600]int[/color] LDR_pin = 0; [color=#7E7E7E]// analog pin 0 (connect LDR here)[/color]
    [color=#CC6600]int[/color] LDR_val = 0; [color=#7E7E7E]// variable use to read input data[/color]

    [color=#CC6600]int[/color] LED1pin = 13; [color=#7E7E7E]//This is the LED 1[/color]
    [color=#CC6600]int[/color] LED2pin = 12; [color=#7E7E7E]//This is the LED 2[/color]
    [color=#CC6600]int[/color] LED3pin = 11; [color=#7E7E7E]//This is the LED 3[/color]
    [color=#CC6600]int[/color] LED4pin = 10; [color=#7E7E7E]//This is the LED 4[/color]

    [color=#7E7E7E]// a threshold to decide when the LEDs turn on[/color]
    [color=#CC6600]int[/color] threshold1 = 500;
    [color=#CC6600]int[/color] threshold2 = 350;
    [color=#CC6600]int[/color] threshold3 = 250;
    [color=#CC6600]int[/color] threshold4 = 150;

    [color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]() {
      
    [color=#7E7E7E]// declaration of pin modes[/color]
    [color=#CC6600]pinMode[/color](LDR_pin, [color=#006699]INPUT[/color]);
    [color=#CC6600]pinMode[/color](LED1pin, [color=#006699]OUTPUT[/color]);
    [color=#CC6600]pinMode[/color](LED2pin, [color=#006699]OUTPUT[/color]);
    [color=#CC6600]pinMode[/color](LED3pin, [color=#006699]OUTPUT[/color]);
    [color=#CC6600]pinMode[/color](LED4pin, [color=#006699]OUTPUT[/color]);

    [color=#7E7E7E]// begin sending over serial port[/color]
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](9600);
    }

    [color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color](){
      
    [color=#7E7E7E]// read the value on analog input[/color]
    LDR_val = [color=#CC6600]analogRead[/color](LDR_pin);
    [color=#7E7E7E]// if value greater than threshold turn on LED[/color]

    [color=#CC6600]if[/color] (LDR_val < threshold1) {
      [color=#CC6600]digitalWrite[/color](LED1pin, [color=#006699]HIGH[/color]);
    }
    [color=#CC6600]else[/color] {
      [color=#CC6600]digitalWrite[/color](LED1pin, [color=#006699]LOW[/color]);
    }

    [color=#CC6600]if[/color] (LDR_val < threshold2) {
      [color=#CC6600]digitalWrite[/color](LED2pin, [color=#006699]HIGH[/color]);
    }
    [color=#CC6600]else[/color] {
      [color=#CC6600]digitalWrite[/color](LED2pin, [color=#006699]LOW[/color]);
    }

    [color=#CC6600]if[/color] (LDR_val < threshold3) {
      [color=#CC6600]digitalWrite[/color](LED3pin, [color=#006699]HIGH[/color]);
    }
    [color=#CC6600]else[/color] {
      [color=#CC6600]digitalWrite[/color](LED3pin, [color=#006699]LOW[/color]);
    }

    [color=#CC6600]if[/color] (LDR_val < threshold4) {
      [color=#CC6600]digitalWrite[/color](LED4pin, [color=#006699]HIGH[/color]);
    }
    [color=#CC6600]else[/color] {
      [color=#CC6600]digitalWrite[/color](LED4pin, [color=#006699]LOW[/color]);
    }

    [color=#7E7E7E]// output 'LDR_val' value into the console[/color]
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"LDR = "[/color]);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](LDR_val);
    }


    [/quote]

    ReplyDelete