Light Dependent Resistors (LDR) with evive

What is LDR
Description
Learn what is LDR, and how to interface it with evive. Understand analog and digital signals and how to visualize the LDR values on evive’s screen. Write a script to change the backdrop of PictoBlox’s Stage according to the amount of light incident on the LDR.

Introduction

LDRs are tiny light-sensing devices also known as photoresistors. An LDR is a resistor whose resistance changes as the amount of light falling on it changes. The resistance of the LDR decreases with an increase in light intensity. This property allows us to use them for making light sensing circuits.  Learn what is LDR, how does it work, how to interface it with evive and program it in PictoBlox – our Scratch blocks-based graphical programming platform with advanced hardware interaction abilities, and finally what exciting DIY projects you can make using the LDR available in the evive Starter Kit.

To work in PictoBlox, you’ll first need to download it from HERE.

Ready? Set. Go!

What is LDR or Photoresistor?

Let’s begin by understanding what is LDR and how does LDR works.

LDR is an acronym for Light Dependent Resistor. LDRs are tiny light-sensing devices also known as photoresistors. An LDR is a resistor whose resistance changes as the amount of light falling on it changes. The resistance of the LDR decreases with an increase in light intensity, and vice-versa. This property allows us to use them for making light sensing circuits.c

For using an LDR, we always have to make a voltage divider circuit. When the value of resistance of LDR increases in comparison to the fixed resistance, the voltage across it also increases.

What are Signals?

But before we move ahead, let’s talk a little about signals.

A signal is anything that carries some sort of information. It can be an action, a sound, or a type of movement.
Any signal can be classified as:

  1. Analog Signal
  2. Digital Signal

Analog Signal

An analog signal is a signal that represents ALL of the possible values in a given range as it varies with respect to time; it is analogous to the time-varying quantity it represents.analog signal

Digital Signal

On the contrary, a digital signal is a signal that represents a quantity as a series of discontinuous values.

spribe aviator game!

The digital signal can represent only 2 values: ‘HIGH‘ and ‘LOW‘.digital signal

An LDR is an analogous device; its resistance changes gradually, and not in discontinuous steps, 

Interfacing an LDR with evive

Now that we have a little idea about how does an LDR work, let’s take a look at how to interface it with evive and see it in action.

  1. Connect the LDR and 4.7k ohm resistor in series.
  2. Connect the 5V pin to the first leg of the LDR.
  3. Connect the ground pin to the end of the resistor.
  4. Connect the common leg of LDR and resistor to the A0 pin on evive.Connection of LDR with evive

Visualizing the LDR values on evive’s screen

Now, that we have connected LDR to evive, let’s visualize the change in the resistance of the LDR on evive’s pin state monitor.

  1. Switch ON evive. In its menu, navigate to pin state monitor.Menu Pin State Monitor Selected
  2. Select analog pin states.Pin State Monitor Analog
  3. Observe the value in front of the A0 pin. As the light decreases the value also decreases.Pin State Monitor Analog Panel

Working with the LDR in Real-Time

Now, we have connected our photoresistor to evive, We’re going to create a script to change the backdrop of PictoBlox’s Stage according to the amount of light incident on the LDR.
Follow the steps to write the script to make the backdrop looks like day when the incident amount of light is more and it should seem night when the light incident on the LDR is less than a certain amount or none.

  1. Open PictoBlox. Connect evive/your prototyping board to the computer using the USB cable. 
  2. Click on the board button and select evive from the drop-down.
  3. Next, from the dialogue box that appears, select the appropriate serial port.
  4. Open the backdrop library and select two backdrops: we are selecting Metro and Night City with Street for day and night respectively.
  5. Select Tobi.
  6. To obtain the values from LDR, we will use a read analog sensor () at () block from the Sensor’s palette and select light/photoresistor from the drop-down. Select the pin to which it is connected from the second dropdown.read analog sensor block
  7. To check whether the incident light is less or more, use an if-else block. To compare the value obtained from the photoresistor to a certain value, select less than operator block. In to the first blank space, place the read analog sensor () block and write 100 in the second space.
  8. If it is less than 100, the backdrop should change to Night City With Street. Thus, from the costume palette select switch backdrop to () block and select Nigth City With Street from the drop-down.switch backdrop to
  9. Otherwise, we will keep Metro as our backdrop. Thus, duplicate the switch backdrop to () block and place it below the else arm. Finally, select Metro from the drop-down.Programming LDR in real-time
  10. To make the script run forever, we will use the forever block around the entire code.
  11. Now, place a when flag clicked hat block above the forever block. It will ensure that the script runs when the green flag is clicked.Programming LDR in real-time1

You can download the entire code to make the LED blink in real-time here.

Controlling Pin 13 LED using an LDR

In this example, we’re going to use an LDR to turn ON the pin 13 LED on evive. If the intensity of light is more than a value predefined, the LED will turn ON; otherwise, it will remain OFF. Follow the steps below to write the script to control the LDR/photoresistor without evive being connected to the computer.

  1. Switch to the Upload Mode.
  2. If the value obtained from LDR is less than 100 then the Pin 13 LED of evive should turn ON, else it should remain OFF.
  3. To check whether the incident light is less or more, use an if-else block. To compare the value obtained from the photoresistor to a certain value, select less than operator block. In to the first blank space, place the read analog sensor () block and write 100 in the second space.
  4. To turn the pin 13 LED ON, drag-and-drop set digital pin () output as () block from the evive palette. By default, pin 13 is selected. Select HIGH from the second drop-down to turn the LED ON.set digital pin block
  5. The LED will remain OFF, if the value obtained from the photoresistor in more than 100. Thus, duplicate the set digital pin block and place it below the else arm.
  6. To turn the LED OFF, select LOW from the drop-down.making the led blink using ldr
  7. Add a forever block around the script to run it forever.
  8. Finally, add when flag clicked hat block to execute the program.
  9. Upload the code to evive using Upload Code button.making the led blink using ldr

You can download the PictoBlox program from here.

Conclusion

In this lesson, we learned what is LDR, how to interface it with evive and program it in PictoBlox, and finally what exciting DIY projects you can make using the LDR available in the evive Starter Kit. We also understood signals, analog and digital signals, and visualized the LDR values on evive‘s screen. We also wrote a script to change the backdrop of PictoBlox‘s Stage according to the amount of light incident on the LDR and a script to control the LDR/photoresistor without evive being connected to the computer. We have seen that an LDR is a versatile device and can be used for various applications.

Table of Contents