Arduino #9 - Adaptive Dark Sensor using Arduino | Conditionals in arduino


Hello friend's, welcome to the another tutorial of How to Electro. In this tutorial, we will know about adaptive dark sensor, and also make it using arduino. We will also know about some of the functions of arduino, along with conditionals statements. So friend's, lets begin.....

H.T.E Highlighted - Above is the video of the whole tutorial in hindi.

Adaptive Dark Sensor ( Introduction )


A Dark Sensor is an electronic circuit, which detects darkness or low light in it's surroundings, and gives certain output.
e.g. Automatic Street Light ( It automatically turns on in the darkness, and turn off in the bright light ).
An Adaptive Dark Sensor is also a dark sensor, whose sensitivity can be manually adjusted with the help of a potentiometer. This helps the dark sensor to adapt itself according to different light conditions at different place.

Step 1 - Things Needed

  1. Arduino UNO - 1 piece. ( Amazon.in / Banggood.com / Gearbest.com )
  2. Potentiometer (10K) - 1 piece.
  3. LDR - 1 piece.
  4. LED - 1 piece ( Of any color ).
  5. Resistor : 220 ohm - 1 piece, 1K ohm - 1 piece.
  6. Some Wires.
H.T. E Note - The links for buying Arduino UNO are affiliate links. We will earn some profit, if you buy through these links. We will appreciate your support in this way.

Circuit

The circuit of the adaptive sensor consists of two parts, one is potentiometer circuit, and another is LDR Circuit. We will know about these one by one.

Potentiometer ( Manual Adjuster Circuit )

 

This circuit is also known as adjuster circuit, as it adjusts the sensitivity of the Dark Sensor. It consists of an Arduino, a potentiometer and some wires.

Connection
One end of the Potentiometer is connected to +5V, and another end is connected to GND. We can reverse this connection also, and the circuit will properly work. The center pin also known as signal pin is connected to the analog pin number 3 (A3).

Working
The potentiometer ( For more details, read the previous tutorial ) is an adjustable voltage divider, whose resistance changes, when the knob is rotated. We had used this property of the potentiometer here.
When the knob is at initial point i.e. no any rotation takes place, then the resitance of it is high, so very less voltage is supplied through signal pin to A3, whereas for whole rotation, the resistance decreases very much, and hence supply maximum voltage to A3 via signal pin.
This is interpreted by the analog pin, with an in-built Analog-to-Digital converter, and gives value in the range of 0-1023 ( 0 means 0V and 1023 means 5V).

LDR Circuit ( Dark detector circuit )


This Circuit is also known as Dark detector circuit, as it detects darkness or low light, and gives certain output. It consists of an Arduino, an LDR, an LED, two resistors and some wires.

Connection
One pin of the LDR is connected to +5V, and the another is connected to GND via a 1K ohm resistor. The point at which the LDR is connected to the 1K ohm resistor is connected to the analog pin number 0 ( A0 ).
The positive terminal of the LED is connected to digital pin number 2 via a 220 ohm resistor, and the negative terminal is connected to the GND.

Working
The LDR ( for more details, read the light sensor project ) is a type of resistor, whose resistance depends on the surroundings light conditions. It decreases with increase in light and increases with decrease in light. We are using this property of the LDR here, to detect the darkness.
In the circuit, one pin of the LDR is connected to +5V and another is to A0 via a 1K ohm resistor. This acts as an voltage divider, same as of potentiometer, but the only difference is that, the resistance of the potentiometer changes by mechanical energy, whereas, the resistance of the LDR Circuit changes with various light conditions.
When the LDR is in darkness, it's resistance increases to maximum, and hence only a few voltage is supplied to A0. While in bright light, its resistance decreases to minimum, and hence, maximum volatge is supplied to the A0. This is interpreted by the arduino in a integer range of 0-1023.
In the circuit, negative pin of the LED is connected to GND, and positive pin is connected to the digital pin number 2 via a 220 ohm resistor, and +5V required for led to glow is supplied with this pin, when digitalWrite() function will be called for this pin, with value HIGH. The method to calculate the resistor value for an LED is already published. Read it for more details. 

Code

int POTPin = A3; // declaring integer type variable for storing pin number of potentiometer

int LDRPin = A0;//declaring integer type variable for storing pin number of LDR

int LEDPin = 2;//declaring integer type variable for storing pin number of LED

int Thresold = 0; // declaring integer type variable for storing the value given by Potentiometer

int Intensity = 0;//declaring integer type variable for storing the value given by LDR

void setup(){
pinMode(LEDPin, OUTPUT);//declaring LEDPin as output pin
}

void loop(){
Thresold = analogRead(POTPin);// reading value from the potentiometer and replacing the value of Thresold variable with this

Intensity = analogRead(LDRPin);// reading value from the LDR and replacing the value of the Intensity variable with this

if ( Intensity < Thresold ){
digitalWrite(LEDPin,HIGH); // turns the LED on
}
else{
digitalWrite(LEDPin,LOW); // turns the LED off
}
}

First five line of codes includes the initialization of various variables of integer type. In these lines, first three lines of codes includes initialization of variables, storing pin number for various components with unique name. We had used this unique name in the void setup() function and void loop() function for getting the pin number stored in it, as the value. Last two lines includes the variable whose value is updated in Runtime in the void loop() function and is accessed.

void setup() function
This is a function, which runs only one time, when the board is powered on or the reset button is pressed on the arduino. So here, only the codes related to intialization of functions, methods is used.
In the above code, it contains a function pinMode(LEDPin,OUTPUT). This function sets the mode for any digital pin, and takes two parameters, one is the pin number, and another is mode of the pin either INPUT or OUTPUT. This function is declared here, because it needs to be run once to set the pin mode.
This function is not applied to analog pins because, they are specially designed for sensors, and hence, its pin mode is INPUT by default. So, we had not declared the pin mode of the input components.

void loop() function
This is a function, which runs consecutively till the arduino is turned off. It contains the functions and methods, that needs to be consecutively run, like giving output by checking given inputs etc.
In the above code, it contains two functions, which is analogRead() and digitalWrite(). The analogRead() function reads the input at the specified analog pin, and converts it into an integer value range of 0-1023. It takes the analog pin number to which sensor is connected as its parameter.
The digitalWrite() function is used for giving +5V or 0V to the specified digital pin number, which turns on or off the components.
e.g. LED, MOTOR etc.
It takes two parameters, in which first is the digital pin number, and another is the constant value HIGH or LOW ( HIGH means +5V and LOW means 0V ).
First two lines of code in the void loop() function is used to change the value of the variables in runtime. The value assigned to them is the value from the sensor at the specified analog pin.
Third line of code contains a conditionals, which is an if-else statement. if statement has some conditions, which, if returns true will run the function defined in it, and if returns false, will run the function defined in the else statement.
The condition given to it is to check whether, the light intensity is less than the Thresold. If it is, then there is darkness and will turn the LED on, and turn off, if it is wrong.

So friend's, that's all for this tutorial. Hope u enjoyed it.
Thanks -
How to Electro

Comments

Post a Comment