Arduino #7 - Calculating Light Intensity using LDR | analogRead, delay and Serial function


Hello friends, welcome to the another tutorial of How to Electro. In this tutorial, we will know the way to calculate the intensity of light using LDR and Arduino. We will also know about some of the function/method related to arduino, including, analogRead, delay and Serial function. So friends, let's begin...

Introduction

LDR is a type of resistor, which changes its resistance value in varied light condition. It is also called photo-conductive cell because, the resistance of the LDR decreases in the bright light, hence allow more current to pass through it, and resistance increases in the dim-light, hence decreases the amount of current to pass through it.

Connecting the Circuit

Circuit Connection

Connect one pin of LDR to +5V of arduino, and the another pin of LDR is connected to GND via a 100K ohm resistor. For giving arduino input from the LDR, connect the point of connection between LDR and the resistor to AO pin.

Circuit Explanation

The LDR is connected to the Arduino via a voltage divider. We will know about this in the upcoming tutorials. When the light falls on the LDR, its resistance decreases, and the +5V starts passing through the LDR to the analog pin A0. and when there is low light over the LDR, its resistance increases, and hence decreases the amount of current passing through LDR to the analog pin A0. This is due to the photo-conductive property of the LDR.
Resistor is connected for lowering down the current to prevent the short-circuit, when the current starts passing through the LDR by supplying it to the ground.

Code 

int LDRPin = A0;
int LightStatus = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
LightStatus = analogRead(LDRPin);
Serial.println(LightStatus);
delay(100);
}

In the first two line of code, variables are initialized, which contains the pin number and the value of the input at analog pin respectively. They are initialized with name, LDRPin for storing the pin number and with LightStatus to store the analog value from the A0.
Next line of code is the setup() function, which is run by the arduino once when the arduino is powered on or the reset button is pressed. Here, we had started the serial communication with the baud rate 9600, which is the default baud rate to communicate with the arduino serial monitor.
The last function is the loop() function, which reads the analog value from the pin, and sends it to the Arduino serial monitor with a delay of 100 milliseconds ( 1 second = 1000 milliseconds ) . Here, the value of the variable LightStatus is replaced with the new value which is the value given from the analog pin A0, by the function analogRead(). It gives the value in the range of 0-1023 due to varying inputs, and this value is given by the in-built Analog-to-Digital converter, which converts the analog signal into digital signal, readable by the arduino. and this is send to the serial monitor with the println method of Serial function. The delay() function is used to wait for specified number time in milliseconds before going to the next line. Here, delay with 100ms is used, which causes the loop() function to run consecutively with the specified delay.

Function and Method

  • analogRead() :- This function takes the analog pin number as its parameter, in which the sensor is connected, and gives the output in range between 0-1023 according to different input values.
  • delay() :- This function is used to specify the delay between the lines of codes i.e. if delay is specified after any line of code, then before going to the next line for execution, arduino waits for the specified number of milliseconds.
  • begin() :- It is a method of the Serial function, which starts communication between any serial monitor and arduino. it takes the baud rate of the serial monitor as its parameter for communicating with the serial monitor. We will know about the Serial function in the upcoming tutorials.
  • println() :- It is also a method of the Serial Function, which sends value specified as its parameter to the serial monitor and it is displayed to the user.

Uploading Code and reading Values ( Serial Monitor )

Now upload code to the arduino, and after uploading, run the serial monitor. Now, you will see different values on the serial monitor with a delay of 100ms. This is the value which tells us the intensity of the light.

So friends, that's all for this tutorial. Hope u enjoyed it. 
Thanks - 
How to Electro ( Samridh Sharma ).

Comments

Post a Comment