SENSOR-LUZ

What is a light sensor?

fotorresistencia

The light sensor, also known as photoresistor or photoelectric sensor, is a sensor which detects light (as the name would suggest). A photoresistor responds to changes in light intensity and modifies its resistance accordingly. The greater the light intensity, the lower the resistance to the flow of current.

This component has many applications in equipment that we use in our everyday lives like, for example:

  • Garage doors.
  • Burglar alarms.
  • Computers, phones and televisions (to control the brightness of the screen).
  • Barcode readers.

The ZUM Kit light sensor returns values approximately between 0 (in total darkness) and 800 (in direct sunlight on a clear day), if you have a Mi primer kit de Robótica light sensor, this value oscillates between 0 and 500, approximately. It´s basically an analogue component as it returns a range of values, in contrast with digital sensors, which can only return two values, such as a push-button (on or off). That is why it must be connected to the string of analogue pins, numbered as A0, A1, A2, etc. This sensor is an input device because it serves to enter data into the program, in contrast to an LED or a buzzer, which are output devices.

As you can see, the light sensor is a very interesting component. Want to learn to program it?  Let´s get started! You will need:

  1. A ZumBloq light sensor and ZUMBloq LED
  2. A ZUM BT 328 controller board or one compatible with Arduino UNO
  3. A USB cable

G003522
zumbloqLED

We will now give different examples to show you how to use this component.  Let´s get on it!

Example 1

Lighting up an LED when the light sensor detects a small amount of light

From now on, we will be using more than one component in each exercise and they will be connected to the board simultaneously.

Before connecting the light sensor, let´s remember that with analogue components, they must be connected on one of the analogue pins, for example, the A0. This diagram will be our guide:
sensordeluzled

Now, we have programmed the controller board using bitbloq so that when there is little light, the LED will light up. Let´s get started, first we have to read the amount of light, and to do this we will assign the reading of the light sensor to a variable. The variables are “places”  in which we store data. As it´s the first time that we are using the variable, we must declare it, this is like giving it the name that we will use for the rest of the program.

Now we need to check that there is little light. We have said that this light sensor gives a value between 0 and 500, which is why we can consider it to be already dark when the variable quantity_of_light is less than 200.

In that case, we must light up the LED, which is connected to digital PIN 3.

But that´s not all, try programming the board and you will see that it doesn´t work exactly as you would expect,  What´s missing? The LED doesn´t go off when there is light again. Try to finish the program yourself and then have a look at the solution below.

reloj-de-arena-10375

It needs to be told that if the light is greater than 200, the LED goes off, which is done like this:

As you can see in the previous image, the block if, do can have more options, such as else, which are executed if the condition is not fulfilled (in this case, when the amount of light is equal or greater than 200). To add the else option, press on the star of the block and add it:

Did you get it right? Sure you did! Any doubts, just ask.  You can load the full program in bitbloq by looking for the bq project Lámpara nocturna (night light).

Do you dare to modify the program so that the night light comes on when it gets dark and goes off after 1 minute? Then it shouldn´t come on again until the next time it gets dark.  Have a go! You can find the solution in the Lámpara noctura avanzada (Advanced night light) project in bitbloq.