FRIENDz

FAILURE IS THE SIGN OF SUCCESS!!

Detecting Light using Arduino

AVR Arduino-Detecting Light:
       The circuit for this recipe is the standard way to use any sensor that changes its resistance based on some physical phenomenon. The circuit in will change the voltage on analog pin 0 when the resistance of the LDR changes with varying light levels. A circuit such as this will not give the full range of possible values from the analog input—0 to 1,023—as the voltage will not be swinging from 0 volts to 5 volts. This is because there will always be a voltage drop across each resistance, so the voltage where they meet will never reach the limits of the power supply. When using sensors such as these, it is important to check the actual values
the device returns in the situation you will be using it. Then you have to determine how to convert them to the values you need to control whatever you are going to control. The LDR is a simple kind of sensor called a resistive sensor. A range of resistive sensors respond to changes in different physical characteristics. The same circuit will work for any kind of simple resistive sensor.

const int ledPin = 13; // LED connected to digital pin 13
const int sensorPin = 0; // connect sensor to analog input 0
// the next two lines set the min and max delay between blinks
const int minDuration = 100; // minimum wait between blinks
const int maxDuration = 1000; // maximum wait between blinks
void setup()
{
pinMode(ledPin, OUTPUT); // enable output on the led pin
Serial.begin(9600); // initialize Serial
}
void loop()
{
int rate = analogRead(sensorPin); // read the analog input
// the next line scales the blink rate between the min and max values
rate = map(rate, 200,800,minDuration, maxDuration); // convert to blink rate
Serial.println(rate); // print rate to serial monitor
digitalWrite(ledPin, HIGH); // set the LED on
delay(rate); // wait duration dependent on light level
digitalWrite(ledPin, LOW); // set the LED off
delay(rate);

}

No comments:

Post a Comment