What is an infrared sensor?
Calibrating the infrared sensor
Before using our infrared sensor, we first need to calibrate it so that it will correctly detect black and white. Follow this video to carry out the process:
Shut the door, it´s cold! Programming an alarm for the door
In many countries, winter means very cold weather. One solution is an alarm for the door that goes off whenever the door is open.
When the infrared sensor detects that the door is open, or when it detects black, we want the buzzer to sound. To do this, we need to use the If…do block.
As you will see, the program is very simple. If the IR sensor detects black, or 0, the buzzer will sound until white or 1 is detected. This means that we can control the door and make sure that it´s always closed so that the heat doesn´t escape.
The green block is a comment. Comments are messages used within programs to make them easier to understand. They are not executed by the board, they are simply there to help you or anyone else see how it works. You can use the comments block under the Code tab. Use them whenever you need them!
My problem with crooked pictures. Using two infrared sensors.
I´m going to let you in on a secret. I can´t stand crooked picture frames in my house. Every time I walk through the hallway, I end up having to reposition them all. But the remedy is often worse than the ailment… Every time I touch them, they end up being even more crooked! My doctor has told me to relax and that life is full of imperfections, but I don´t agree and I´ve decided to create a system to make sure they are always straight. To do this, I´m using two infrared sensors; one hanging on the wall and the other on the picture frame. When the sensors are face to face and perfected lined up, they will detect each other and the LED will light up. And when one of them is crooked, the alarm will go off and the LED will light up. Eureka! I won´t have to worry about crooked pictures ever again!
Now let´s make the program. You will need an LED, a buzzer and the two infrared sensors:
We want an LED to indicate that the picture is straight when the sensors are face to face, and the LED to switch off and the buzzer to sound when they are out of alignment. To indicate that the two sensors must be equal to 1, which means being able to detect each other, we have used the “and” logic block, which is located under the Logic tab.
This way, if the first infrared sensor is equal to 1, which means it´s detecting the light from the other sensor “and” the second infrared sensor is also equal to 1, the condition has been met and the LED will light up. The “and” logic block and its variants will enable you to create more advanced programs, which we will be using often.
And there´s more
- Here´s a few more ideas so you can keep on practising:
- Create your own robot or use a PrintBot to program a line follower.
- You can make a robot vacuum cleaner that detects when it gets to the edge, stops and reverses so that it doesn´t fall off. Your table has to be white!
- Make a little Halloween cut-out.
The infrared sensor is super easy to use and super useful. Practise using it and you´ll come up with a ton of new ways to use it in no time. Want to know more sensors? We´ve only just got started!