tutorial
Interfacing LDR (light dependent resistor) with Arduino
In order to detect the intensity of light or darkness, we use a sensor called an LDR.
The LDR is a special type of resistor that allows higher voltages to pass through it (low resistance) whenever there is a high intensity of light, and passes a low voltage (high resistance) whenever it is dark.
How Does
It Work?
· This
system works by sensing the intensity of light in its environment. The sensor
that can be used to detect light is an LDR.
The LDR gives out an analog voltage when connected to VCC (5V), which varies in magnitude in direct proportion to the light intensity on it.
That is, the greater the intensity of light, the greater the corresponding voltage from the LDR will be.
Since the LDR gives out an analog voltage, it is connected to the analog input pin on the Arduino.
The Arduino, with its built-in ADC (analog-to-digital converter), then converts the analog voltage (from 0-5V) into a digital value in the range of (0-1023).
When there is sufficient light in its environment or on its surface, the converted digital values read from the LDR through the Arduino will be in the range of 800-1023.
Connection Diagram:
Code:
After connecting the LDR to your Arduino you can check for the values coming from the LDR via the Arduino. To do this, connect the Arduino via USB to your PC and open up the Arduino IDE or software. Next, paste this code and upload it to your Arduino:
int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
void setup()
{
Serial.begin(9600); //sets serial port for communication
}
void loop()
{
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
delay(100);
}
After uploading the code, click the button on the Arduino IDE called “Serial monitor". This will open a new window, which prints different values on the screen. Now, test out the sensor by blocking its surface from light and see what values you get on the serial monitor. This is how the serial monitor looks:
Output:
You will get the intensity values of light on the above serial monitor.
Reference:
- Serial.begin(): Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the computer, use one of these rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200.
- Serial.println (): Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n'). This command takes the same forms as Serial.print().
- analogRead(): Reads the value from the specified analog pin.
No comments:
Post a Comment