Using an IR sensor with Arduino
Microcontrollers have been around for many years, however, with the advent of Arduino, Raspberry Pi, it has become a lot easier to program it and create various DIY projects at home. These boards help in rapid prototyping.
Arduino is an open-source hardware and software which allows us to program sensors, connect to electronic circuits to control various output devices such as buzzers, LEDs, actuator etc.. These boards are fairly inexpensive and easy to program in C language.
Nowadays it is easy to buy these sensors and electronic parts with very little expense and build it yourself and also get your children interested in the basics of electronic circuits.
For this project, I am using an Arduino Uno board.
Some Basics
Arduino board have digital and analog pins besides power outputs ( 3.3V and 5V), it works by connecting with a USB charger or with your computer. One important thing to note is all we can measure from these pins are output voltages(not current). While constructing circuits one important consideration is to ensure that we do not exceed the power and current ratings for these electronic components such as LEDs, therefore often a pull-up resistor is connected to reduce the current passing through them. It's a good idea to refer to the datasheet for the electronic component you are trying to use. They are available on the web, if you search on the component model #, you will get the datasheet. Most of the time we use basic Ohm’s law to determine the value of the resistor to use. For example for these small LED’s 220 Ohms resistors works well, if we consider the Vs ( Forward voltage of the LED as 2V) and minimize the current to less than 20mA. Going beyond 20mA can ruin your LED. It is good to plan for 10–15mA instead of max current permissible. Better safe than sorry!
Code which drives the logic ( sketch) is written using the free IDE provided by Arduino. Arduino has special libraries for various sensors which you need to download. The code has a common skeleton which consists of setup() and loop(). The setup is run once and works as an initializer. the loop() method runs continuously and the main logic sits here. Once the code is compiled it's uploaded to the Arduino micro-controller using USB cable from the IDE.
IR Sensor Project
In this IR Sensor project, the goal is to use an IR( Infra-Red) sensor together with an IR remote controller to program a 10 Pin, 1 digit LED as shown in the above image. As we press numbers 0..9 on the remote, we see the number getting displayed on the LED immediately.
We are using IR receiver TSOP1738 for this project. It receives at 38KHz frequency. This comes with inbuilt amplifier, band filter besides the IR sensor all integrated together in one chip. While using any component for the first time, make use you get yourself familiar with PinOut, so for this IR Sensor, the third pin ( Signal) is used to detect the signal it produces when it receives IR rays from the remote. Although we can even use a TV remote for this project and program the remote with the sensor, I have used a lightweight remote that came with this sensor.
The sensor Vcc is connected to Arduino 5V with a pull-up resistor 220 Ohms and the GND terminal to Arduino GND. The sensor terminal of the IR is connected to the digital PIN 11 of the Arduino. Idea is to read the signal from PIN 11 and control these LEDs.
Now, the 10 Pin LED, comes with common cathode on both sides, usually the Center Pin. You need to connect one of them(say pin 3 ) it to the GND of the Arduino. Rest of the 8 PINs of the LED are used to light up the 8 LEDs that compose the digit include the decimal point(PIN 4). So we need to connect these 8 PINs to the digital PINS on Arduino board. You can choose any of them, I have used PIN 2..9. They are all connected using 220 Ohms pull-up resistor. Now load the remote with battery and keep it ready for the project. We are using SUN056CC, 7-segment common cathode display LED.
Once the connections are done, it time to decipher the hex code that comes from the IR sensor on Arduino PIN 11. It quite simple, every time you press a key on the remote use the Serial.println output from the PIN 11. Serial.println prints on the serial port onto the Arduino IDE console, it is very useful for such debugging. For example for my remote hex code for 0 on the remote is 0x1FE48B7. You need to find this for all the keys you want to program. Next, you need to light up the LEDs one by one and figure out which combination of LED helps in displaying the given number. For example for digit 1, we need to light up LEDs 2,6 and 5 for the decimal.
Source Code ( Sketch)
The source code is available on Github, here is an explanation of the core logic here.
At top we
#include <IRremote.h>,
you may need to install it by going to Arduino IDE->Sketch->include library->install( by selecting IR library)
First we create a array that we can later use to initialize the digital PINs to be used for the LEDs.
int led[] = {2,3,4,5,6,7,8,9} ;
Next we create a 2D array in C to define the LED PINS that will allow us to create various digits. This array has 10 rows and 8 columns. Numbers rows is equal to number of keys you plan to program from the remote.
int ledSwitch[10][8]={{0,1,2,4,5,6,7,-1}, //zero
{0,5,4,-1},
{1,0,6,7,3,4,-1 },
{1,0,3,5,6,4,-1 }, //3
{2,3,5,0,4,-1},
{1,2,3,5,6,4,-1},
{2,7,6,5,3,4,-1}, //six
{1,0,5,4,-1},
{1,2,3,5,6,7,0,4},
{1,2,3,0,5,4,-1} //nine
} ;
I have added a couple of functions to make the code modular. The reset() function is written to switch off all the LEDs once a new command is sent from the remote. The lightUp(int row) function is for lighting up specific LED based on the row number provided. For example, if we call this function with row:1, the LEDs corresponding on digit 1 will light up.
In setup() we initialize the LEDs, define the pinMode as OUTPUT so that we can switch them on based on the IR signal. We also enable the IR Pin using enableIRIn() function. Initially, all the LED pins are set to off ( LOW).
In the loop(), we construct a switch statement as follows and use lightUp() function on the LEDs as required.
case 0x1FEE01F :
Serial.println(“ZERO”);
reset();
lightUp(0);
break;
Conclusion
Overall its a quite easy project to do at home, however, it can be extended to do much more important task than switching LEDs. If you later decide to switch on your room light or fan using a remote, you could use a similar sketch, perhaps add a relay and control a high voltage relay circuit. Arduino is a great way to start off with iOT projects.