DIY Infrared Sensor Module




About: I've always taken stuff apart, almost always put it back together... I enjoy tinkering around with pretty much anything.

Have you ever wanted to make a line following robot but the infrared sensors were too expensive for you? Do you want to upgrade the robot in my other instructable?

Well this instructable comes in two stages, Stage 1 is the prototyping stage, where you build the circuit on the bread board and debug it for design flaws.

Stage 2 is fabrication where I’ll teach you how to create a strip board product that you can plug straight into your robot.

The good thing about this tutorial is you’re able to do many things with these sensors.

Also, if you’re from the UK and have a maplin store near you, you’ll be able to get some IR transmitter and receivers for 99p (They’re side facing LED/phototransistors, so you can have a low-profile sensor module), More on this at the end of the instructable.

1x Arduino
1x Bread board
1x Strip board
1x Wire reel, black
1x Wire reel, red
1x Wire reel, green
2x Infrared LED (Maplin/Sparkfun)
2x Phototransistor (Maplin/Sparkfun)
2x Red 5mm LED (Maplin/Sparkfun)
2x 1k Ohm resistor
2x 220 Ohm resistor
Wire cutters
Wire strippers
Helping Hands
Heat shrink tube (Not used)
Heat source (Not used)
Soldering iron

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Let's Get Started

You want to take your bread board and your infra red LED’s and photo transistors and place them side by side (1 hole apart) in the bread board, repeat for the other infra red pair.

Now let’s start wiring up the infrared LED’s first, place a 220 Ohm resister in the same row as the IR LED cathode then wire it to ground, and make sure to wire both. Next you need to wire up both of the IR LED anodes to the power rail.

Step 2: Keep Going

Now we’re moving onto the photo transistors. Take the red LED’s and place the cathode in the photo transistors anode row, next we place a 1k Ohm resistor inside the photo transistors cathode.

Now we take a wire and connect the anode of the red LED to the power rail. We also need to ground the photo transistors do that now.

Now we just need to wire the photo transistors to the analogue inputs zero and one, to do this wire the cathode of the photo transistor before it goes to the 1k Ohm resistor, the left most wire should go to analogue zero and the one on the right should go to analogue one.

Step 3: It's Alive, Alive!!!

Now if you wire in the power supply from the Arduino (+5v and Ground) if you wave your hand in front of the IR LED/phototransistor pairs, you should notice the red LED’s fading from dim to bright and back as you move your hand. If the LED’s are indeed facing back and forth congratulations you’ve completed the basic prototype, you can move onto the next step.

How ever, if you’re not getting any results from waving your hand back and forth you must check your wiring (Maybe your IR LED’s are either burned out or put it in backwards).

Step 4: The Code

If you want to test out the circuit, upload the following sketch to your Arduino.

/* Line Following Robot

Demonstrates the use of IR LED's for a simple line
following robot, if it leaves the black line it will
attempt to locate it.

The circuit:
* Follow the instructable on how to construct it.

created 2011
by Dominion-Network

This example code is in the public domain.


// Motor Outputs
// Left Motor
int M1B = 11; // Reverse
int M1F = 10; // Forward
// Right Motor
int M2B = 6; // Reverse
int M2F = 5; // Forward
// Motor Speed
int mSpeed = 115;

// 2 IR Sensor Analog Input Pins
int LIRPin = A0; // Left Sensor
int RIRPin = A1; // Right Sensor

// 2 IR Analog Reading variables
int LIRReading; // Left Reading
int RIRReading; // Right Reading

void setup(void) {
Serial.begin(9600); // For debugging purposes
pinMode(M1F, OUTPUT); // Motor 1 Forward
pinMode(M1B, OUTPUT); // Motor 1 Reverse
pinMode(M2F, OUTPUT); // Motor 2 Forward
pinMode(M2B, OUTPUT); // Motor 2 Reverse

void loop(void) {

void rightirscan() {
RIRReading = analogRead(RIRPin);

Serial.print("Right = ");

// We'll have a few threshholds, qualitatively determined
if (RIRReading < 20) {
Serial.println(" - No Correction");
analogWrite(M2F, 0);

} else if (RIRReading < 200) {
Serial.println(" - Minor Correction");
analogWrite(M2F, mSpeed + 10);

} else if (RIRReading < 500) {
Serial.println(" - Moderate Correction");
analogWrite(M2F, mSpeed + 50);

} else if (RIRReading < 800) {
Serial.println(" - Severe Correction");
analogWrite(M2F, mSpeed + 100);

} else {
Serial.println(" - Extreme Correction");
analogWrite(M2F, 255);

void leftirscan() {
LIRReading = analogRead(LIRPin);

Serial.print("Left = ");

// We'll have a few threshholds, qualitatively determined
if (LIRReading < 20) {
Serial.println(" - No Correction");
analogWrite(M1F, 0);

} else if (LIRReading < 200) {
Serial.println(" - Minor Correction");
analogWrite(M1F, mSpeed + 10);

} else if (LIRReading < 500) {
Serial.println(" - Moderate Correction");
analogWrite(M1F, mSpeed + 50);

} else if (LIRReading < 800) {
Serial.println(" - Severe Correction");
analogWrite(M1F, mSpeed + 100);

} else {
Serial.println(" - Extreme Correction");
analogWrite(M1F, 255);
Once you've uploaded the sketch to your Arduino open up the serial monitor and you should notice a series of values been printed in the window e.g. “Left = 21 – Minor Correction”.

That means the left sensor is getting more light reflected into the photo transistor and will correct it's course to reduce the light been reflected by turning either left or right until the value has been decreased below the threshold.

Here's a simple concept demo.

Step 5: Optional Step 1

This is an optional step, if you’ve got any heat shrink tubing slip it over the photo transistors and heat it up until its snug, make sure to trim the end of the tubing so the tip of the photo transistor is poking out.

This will help block out most if not all the ambient light to improve the readings.

Step 6: Placing the Componants

Now we move onto the good stuff, the strip board fabrication.

Take your strip board and mark an area that is about five by nine holes long then snip the rectangle out.

Take your components one by one from your bread board and recreate the circuit on the strip board the same way you created the bread board prototype, start with the IR LED’s and photo transistors.

Bend the components legs slightly to keep them on the strip board.

Step 7: Soldering

Once you’ve finished placing all the components on the strip board cut four lengths of different coloured wire (1x red, 2x black and 1x green, yellow or white respectfully), again bend the wire slightly so it holds in place on the strip board.

I apologise for this step as I forgot to place a ground wire from the 1k Ohm resistor to the empty hole between the 220 Ohm resistor and the other ground wire.

Alternatively, if you’ve got some male header pins like me, cut three off and solder them in place instead of the wires (You will still need the previously mentioned 1k Ohm to 220 Ohm resistor ground wire), This is what I’ve done.

Step 8: End Result and Going Further

The end result should look something like this (If you used the 3-pin header) I apologise for not having a video of the finished product but at some point something on the strip board broke as the IR LED nor the photo transistor appears to be working, and I’m currently in the process of figuring out what it was so I can repair it.

This is another optional step, instead of creating separate modules that you can connect via 3 wire connectors, you can make a strip module that uses a 5 pin header, that’d be 3 photo transistors/infra red receivers and 4 IR LED’s/transmitters so the header would look like this.

Another step you could take is by buying some IR transmitters and some IR receivers from Maplin (Can’t find USA Equivalent) they’re side facing so you can bend the legs so the back of them are touching the strip board which means you can have them closer to the ground for more accurate readings.

Here's some suggestions to expand on this instructable.
  1. An alternative input device (See Make - Arduino Bots and Gadgets: Interactive Painting)
  2. An extremely basic edge avoiding robot, using the IR sensors, like the line following robot with altered code you’ll be able to tell the robot it has come to an edge and needs to turn around.
  3. Maze solving robot, like the line following robot, or edge avoiding robot with slight alteration to the code you could have your robot attempting to solve mazes.
  4. A basis for a motion sensor project (See 51 High-tech Practical Jokes For The Evil Genius)

Be the First to Share


    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    17 Discussions


    3 years ago

    colud I replace R1 for a potentiometer for calibration, or what can I do to adjust it. sorry my english


    5 years ago on Introduction

    Thank you for posting this instructable. I am seeking a non-contact sensor that I can attach to a Digital Scale and move in increments along and object (scale model car) and detect a consistent signal when the surface of the object is detected. Industrial Machines such as I wish to build are called Coordinate Measurement Machines (CMM's) and include contact probes. Digital Readout calipers or scales can be designed into my machine with a .001" resolution. What resolution or accuracy of detection can be realized with this instructable?


    7 years ago on Step 4

    sweet sweet, I like the line idea for drawing a path. have to try it on an rc car next, or a lawnmower.


    7 years ago on Introduction

    Hey, Great instructable exactly what I was looking for.

    I am trying to build my own line following robot. I would like to build my own line detector.

    I am pretty sure that I can make lets say 6 of these in a row. But I am wondering if they will be interfering with each other?

    3 replies

    Reply 7 years ago on Introduction

    Glad you liked the instructable!

    If you use narrow band IR emitters and maybe the heat shrink tubing to block out most of the ambient light out I don't think it'd interfere with each other to much, also depends on placement.

    I wish you luck in building your own line following robot, mine is currently on the fritz (Doesn't like turning).


    Reply 7 years ago on Introduction

    Well in the spectrum of light black absorbs all light whilst white reflects it, the example code simply tells the robot what to do when certain amounts of light is detected, and no I didn't buy an IR detection kit, this is scratch built trial and error.

    Thank you for reminding me, I've been meaning to add a schematic for a while, if you check step 1 you should notice the schematic there, hopefully it'll help you.


    8 years ago on Introduction

    Can I use this circuit and the program, run by Ziolog microcontroller or it has to be exact arduino controller, reason for this I have to design module for my capstone project making the robot that my group is desiging fallow the tape as well, it will be security system looking for motion around with camera activated after it finds it , using Zilog microprocessor, I dont know if code will be simillar or not, if so, modyfing code would be hard with zilog?



    1 reply

    The code is Arduino Based, I've not had a look at Ziolog so I couldn't tell you if you'd need to edit it much, but the actual module itself is just hardware, you can connect it to any microprocessor you want

    Unfortunately no, it's not that powerful, or good lol the only distance you'd be able to measure is about 1 - 5ish cm away from it.