Saturday, September 10, 2011

[Arduino] Light-detected Alarm

This is one of Arduino practice project from "Beginning Arduino, Michael McRoberts". In this exercise, I use an arduino uno board to control a light-detective alarm.


Required component includes:

  • An arduino board
  • Piezo Sounder (or piezo disc)
  • Light Dependent Resistor (LDR)
  • 10k Ohms Resistor (accordingmay need different resister)
The circuit for this project:
Figure from "Beginning Arduino"
In this circuit, the voltage value inputted Analog pin 0 is the same as the voltage of LDR, and Resistor and LDR construct a voltage divider. In addition, LDR is a resistor depends on light (in dark environment, LDR has a very high resistance)
Code for this project:

// Light-detected Alarm
int piezoPin = 8;  // Piezo on Pin 8
int ldrPin = 0;  // LDR on Analog Pin 0
int ldrValue = 0;  // value read from the LDR

void setup()
{
    //nothing to do here
}
void loop()
{
    ldrValue = analogRead(ldrPin);  // read the value from the LDR
    tone(piezoPin, 1000);  // play a 1kHz tone from the piezo
    delay(25);             // wait a bit (still produce sound)
    noTone(piezoPin);      // stop the tone
    delay(ldrValue);       // wait the amount of millisecond in ldrValue 
}
In this code, the value of ldrValue depend on the voltage of Analog pin 0, which
is  the same as the voltage of LDR (the darker the environment is, the higher the voltage of LDR). Therefore, the piezo sounder will sound quickly in light environ-ment.   

No comments:

Post a Comment

Fork me on GitHub