Arduino #6 - LDR With Arduino ( Light Sensor ) | Introduction to LDR #1
Hello friends, welcome to the another tutorial of Programming Infinity. In this tutorial, we will know about LDR, its uses, and also know the way of using it with Arduino. We will know about LDR use with Arduino by creating a simple Light Sensor. So friends, let's begin...
Introduction
Light Sensor is a project, which detects light and give output for indicating the detection of light. We will use a LDR to detect light in this project, arduino to process signal from LDR, and gives output to the LED, which shows whether light is detected or not. So friends, let's gather the required Components for this project.Required things
Components
- Arduino ( I used Arduino UNO,which is the commonly used board for beginners. Almost all the functions of the Arduino boards are same. ) - Banggood.com / Amazon.in / Gearbest.com.
- LDR ( Light Dependent Resistor ) - 1 piece.
- 1K ohm resistor - 1 piece.
- 220 ohm resistor - 1 piece.
- A LED ( we can use any color LED, that we want. )
- Some Wires.
Miscellaneous
- Soldering wire.
- Soldering flux.
- Soldering iron.
LDR ( Light Dependent Resistor )
Working Principle
It works on the principle of photo-conductivity. Photo-conductivity for any substance means that, it will allow the transfer of electricity through it in the presense of light. Photo in the above term, stands for photons, which are the small particles, that constitutes the light.It is made up of a high-resistance semiconductor. If the light incident on the LDR exceeds a certain frequency, the photons absorbed by the semiconductor gives electrons of the semiconductor enough energy to go into the conductor state, and hence acts as a conductor in the presence of light, by lowering the resistance. The resistance range and sensitivity of the LDR varies with different-different types of semiconductors.
There are two types of LDR these days, namely, intrinsic and extrinsic, based on the types of semiconductor used in the manufacturing of LDR. In Intrinsic type, it has its own charge-carriers and is not an efficient LDR. e.g. Sillicon. It had only available electrons in the valence shell, and hence the light particles falling on the semiconductor must have enough energy to excite these electrons to behave like an conductor. Whereas, In the Extrinsic type, there is impurities in the semiconductor, whose ground state energy ( lowest energy of it ) is nearer to the conduction band; helps the electron to easily jump into conductor state with small energy. This makes this sensitive and also a larger range of the resistance value of LDR.
Applications
It is used in the Light Dependent projects, as, light sensor, dark sensor, street light, solar tracker, line follower robot, obstacle avoiding robot etc.Circuit Diagram
In the circuit diagram, the connections between the components can be easily seen. The LDR is connected to the Arduino with the voltage divider. We will know more about the voltage divider in the upcoming tutorials. We will here only understand, how the voltage regulator works with the LDR to give Arduino the proper input. One pin of LDR is connected to +5V, and another pin is connected to the 1K ohm resistor, and the resistor is connected to the GND. The input is taken from the connection between resistor and LDR and is fed to the A0. Here, when light falls on the LDR, its resistance will decrease and the input to the A0 will increase and is fed to the ground. This means the more light on the LDR, the more will be input at the A0. We will use this condition to turn on/off the led in the code section.
The LED is simply comnectedcto pin no. 7 via a 220 ohm resistor. The value of the resistor used with the LED is count according to some rules. For details, please check the previous tutorial.
Code
int LEDPin = 7;
int LDRPin = A0;
int LightStatus = 0;
void setup(){
pinMode(LEDPin,OUTPUT);
}
void loop(){
LightStatus = analogRead(LDRPin);
if (LightStatus > 500){
digitalWrite(LEDPin,HIGH);
}
else
{
digitalWrite(LEDPin,LOW);
}
}
int LDRPin = A0;
int LightStatus = 0;
void setup(){
pinMode(LEDPin,OUTPUT);
}
void loop(){
LightStatus = analogRead(LDRPin);
if (LightStatus > 500){
digitalWrite(LEDPin,HIGH);
}
else
{
digitalWrite(LEDPin,LOW);
}
}
First three lines of code contains the initialization of variable, which is accessed within the code, and also its value is changed in the runtime. For more information about the variables, please read the previous tutorials.
In the setup() function, the mode of the pin number 7 is set as output, as the LED needs output to glow. For the A0, its not declared as input, because all the analog pins are already defined as input by built.
In the loop() function, first the value of the variable with name "LightStatus" is changed by defining its value equal to the value given at the analog pin. For getting the value at the analog pin, analogRead() function is used which reads the value at the analog pin. After getting the value, a condition will run, which will check whether the "LightStatus" is greater than 500 or not. Here, 500 is the Normal value which comes in the normal light condition. It varies depending on the condition, so we will had to know, how to calculate the normal value of the LDR. We will know about it in the next tutorial. When condition is checked, if the value is greater than 500, which means there is light on the LDR, and hence the LED will glow and when value is lower than 500, which means there is no light or low light on the LDR, and hence the LED will be off.
We can also make a dark sensor by only changing the condition.
So friends, that's all for this tutorial. Hope u enjoyed it. If u have any question/problem, then ask me in comment box. I will answer it as soon as possible.
Thanks -
How to Electro ( Samridh Sharma )
Comments
Post a Comment