Light sensitivity sensor - LDR

We know how resistor works right? Let's go back to Chapter 3, Components and Connections for a quick recap and come back here. It essentially restricts the flow of electrons in a circuit. Light Depended Resistor is a special kind of resistor that reduces its resistance when light falls on it. How does that work you ask? Good question.

Let's dig a little deeper into what is LDR and how it works. First of all, this is how the symbol of LDR looks like:

  

Light sensitivity sensor - LDR

(Image source: https://cdn.instructables.com/F1H/DRCQ/IDCXJLG6/F1HDRCQIDCXJLG6.MEDIUM.jpg)

Symbol:

Light sensitivity sensor - LDR

The arrow signs coming down show the light falling on the LDR. You know what everything in the world is made up of right? Remember we learnt about electrons in the beginning of the book? Electrons are the fundamental part of any material.

Let's start from there. So similar to electrons, photons are fundamental part of light. Light is not made up of only particles though, it has waves too (I know right? We think we know about science one day and the other day we don't! Exciting isn't it?). Learning more about the dual nature of light is out of the scope of this book, but I encourage you to use your googling skills to find out more!

Coming back to photons, they are the smallest part of light. Every photon has a lot of energy in it. So when they hit anything or any material they transfer a part of that energy to that material. Recall how we studied about a resistor slowing down the movement of electrons. Look at the following two diagrams:

Light sensitivity sensor - LDR

The electron starts cycling at point 1 and gets tired at point 2.

An electron trying to cycle on a up-hill track. Its very difficult isn't it?

Light sensitivity sensor - LDR

Electron now also has energy from the photon.

Now imagine if a friend starts pushing it from the back, it get additional energy, wouldn't it be much easier for it to go up hill now? This is exactly it works inside the LDR. Photons give more energy to the electrons to pass through.

So, in summary of the LDR, we know when light falls on the LDR, it will reduce the resistance of the LDR, electrons flow to the other part, hence the circuit is closed. OK enough theory! Let's get back to building.

Note

Find out how the screen in laptops and phones adjust their lights automatically. What kind of sensors do they use?

We will build another block of the jigsaw puzzle now. We will first test how the LDR works with an Arduino. Once we have successfully done that, we will think about integrating it with above piece. Remember LDR is going to be the final version of our external influence.

Following is the figure representing circuit connections:

Light sensitivity sensor - LDR

On connecting all the components, this is how the circuit will look like:

Light sensitivity sensor - LDR

(Image source: http://www.tweaking4all.com/wp-content/uploads/2015/11/arduino-ldr-analog.jpg)

From the above diagram you can see that our LDR is connected to A0 to give us an analog input. We will work on top of the analog read example from this example. This is how the code is going to look like:

Light sensitivity sensor - LDR

Code:

int lightPin = AO; //define a pin for Photo resistor 
void setup()
{ 
    Serial.begin(9600); //Begin serial communcation 
} 
void loop()
{
    Serial.println(analogRead(lightPin)); //Write the value of the photoresistor to the serial monitor.  
    delay(10); //short delay for faster response to light.
}

In the above code we will use the Serial Monitor to read the values. You will see after uploading the values, the serial monitor will start showing values anywhere between 0 to 1023. We however now know how to read the values from LDR.

Let's go back to the first two pieces of puzzle again.

We made a circuit that blinks an LED-from here we keep the code of turning LED on or off.

We made a circuit that uses a switch which when pressed switches on the LED. - We will keep the logic from here. This means that on certain level on input, I will switch on the LED (push button closed) and switch it off of another level (push button open). Our LED right now works with two states then, On and Off.

From the LDR experiment, we are going to do something very small, yet very important. We are going to define our own set of values on which the LED will be On and other set of values where the LED will be off. You saw that the LDR input is between the number 0 and 1023. Depending on your circuit, the numbers could not go down to 0 or reach 1023, but they will typically be in the range.

So how about this, let's try to see when do we get what numbers from the LDR? Run the code and cover the LDR with a small cloth or with just a thick paper. Do the Serial Monitor values go down or Up? Now remove the cover and read the values again. What do you see? Do you see a pattern?

In my case, when I cover the LDR, the reading becomes low in my case I get values under 800. When I remove the cover, reading becomes high. So let's look at the numbers. If the readings from the LDR were to be lower than 800, it means that LDR senses Darkness. Otherwise LDR senses Light. Let's work using these numbers and turn ON the LED when LDR senses darkness and OFF when the LDR senses light.

We will tweak the code we wrote for sensing LDR to the following:

Light sensitivity sensor - LDR

Code:

int lightPin = A0; //define a pin for Photo resistor
int ledPin = 13; // We are using the inbuilt LED

void setup() {
  Serial.begin(9600); //Begin serial communication
  pinMode(ledPin, OUTPUT); //Configure the pin 13 as output
}

void loop() {
  Serial.println(analogRead(lightPin)); //Write the value of the photoresistor to the serial monitor.
  if (analogRead(lightPin)<800){ //Darkness Detected by the LDR
    digitalWrite(ledPin,HIGH);
    }
    else{                         //When Light detected outside
      digitalWrite(ledPin,LOW);
      }

     delay(10); //short delay for faster response to light.
}

When all is done, compile the code, upload and check. If you notice that in your case, on covering the LDR you get another set of values, like 600 or less, keep the threshold according to that. Threshold is a bar that is set as a condition. If the new values from the sensor are lower than that bar (threshold) we perform one type of actions, if the input values are higher, we perform another set of actions.

Now that we have successfully done all the integrations let's reflect on what we have accomplished now. We have successfully created the brain of a bot that will switch on the LED when there is dark outside and switch off the LED when it is bright outside.

Note

Think of where else can you use this logic to build? Can you use this logic to switch off the lights in your room when you are not around? What sensor could you use for that? Maybe a motion sensor? Write down all the problems you would want to solve using this logic.

Now that we have successfully built the bot. Let me tell you the scope of what you have just achieved. No matter what sensor there is out there, it will work on the same logic. You will have an analog input, you will read the values using serial monitor and check what changes you see in different conditions. You will then set a threshold and on the basis of that threshold, you will perform actions. Look at the diagram below, it explains this a little better.

Light sensitivity sensor - LDR

This is the process of using any sensor. Once you have figured out what thresholds to use, rest is simple!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset