Welcome to the final lesson for CR102. In this tutorial, we’ll be learning about the light sensor, and using it to build one last project.
The Light Sensor
A light sensor is exactly what it sounds like – it detects the amount of light directed at it, then outputs a signal that changes with the light level it senses. Arduino can read this signal and use it in programs. The more light the sensor detects, the lower the value that it returns.
The light sensor is also known as an LDR (Light Dependent Resistor). The sensor’s values will always fall into the range of 0-1023.
Project 1: Using the Light Sensor
For this project, we will be programming an LED to turn on when it’s dark and turn off if it’s bright.
- Connect the light sensor to an output pin on the SwissCHEESE board.
- Like the ultrasonic sensor from the previous lesson, we will need to declare a variable for the value we’re getting from the light sensor. Use the set integer variable block, and name the variable “Light”.
- Go into the CAROBOT SwissCHEESE bin and find the LDR block. Place this into the value slot of the set integer variable block. Make sure the pin is set to the same one that the light sensor is plugged into!
- Drag a delay block under the variable declaration. It doesn’t have to be very long – just 10 milliseconds will do.
- Get an if/else statement and a “greater than” number comparison block. It should be prepared similarly to what you did for the ultrasonic sensor – it’ll be checking if the Light variable is greater than 400 in this case.
- Program the board to turn the light on if the Light variable is greater than 400. On the other hand, make it turn off otherwise.
- Upload your program and test it out.
- Change the light sensor’s threshold so that the LED only turns on in extreme darkness.
- Try using the serial monitor together with the LDR and see what numbers different light levels give you.
For the last activity of this course, you will be using two light sensors together with the rover in order to steer it. Program it so that if you shine a flashlight on the left sensor the robot will turn left, and vice versa with the right sensor. If you shine lights on both sensors at once, it’ll just go forwards.
Tip: Control your motor speed! If you make the rover go too fast, it’s going to be very hard to steer.
This concludes the CR102 course. We hope it helped you learn more about robotics!