Hello and Welcome back to the second part of Obstacle
Avoiding Robot Tutorial
. In the last
we studied the drive system and the mechanical construction of our
robot. In this part we will make the sensor part. The sensors will help our
robot detect obstacle in its path. The sensor system is of very basic type of
infrared(IR) reflectance sensor. It is made up of an IR
Transmitter and an IR receiver
. The IR transmitter is an IR LED
which emits light in IR spectrum that is invisible to human eye. The IR receiver
can detect those rays.

IR Pair for robotics sensor

A Matching Pair of IR Rx and Tx, The Blue One is Tx (LED).

The IR Sensor Element

The IR Sensor element is made up of an IR Tx,IR Rx and few resistors. The schematic
is given below. We need three such elements mounted in front of the robot to
sense obstacles in front of it.

Robotics IR Sensor schematic

IR Reflectance sensor schematic.

As you can see the sensor element has two pins for power supply and an Output
pin. The output is a variable voltage between 0 and 5v depending on the type
and distance of the obstacle. It tends to 5v as some obstacle comes near it.

IR Sensor Construction Guidelines.

The value of R1 is 150 ohms and R2 is 22K. The color
is visible in the image above. The values are very critical
so please use only the specified values. Also note that the Rx which is BLACK
in color (some times transparent glass like also) has its small leg connected
to the positive supply, its NOT an error, so connect it exactly that way only.

The IR Rx and Tx should be mounted in such a way that the IR rays from Tx falls
on the Obstacle and Return back and hit the Rx. A proper physical layout is
shown below.

ir rx tx for robotics

IR Rx Tx Mounted On PCB.

The output of the Sensor element is fed to the Analog
To Digital Converter
of AVR MCU. The ADC of avr will convert the
output to a 10bit digital value which ranges from 0-1024. Thus you can read
the ADC to check if the sensor has some obstacle in front of it. Reading ADC
is simple in xBoard
and is documented in details in this tutorial.

Say if we have connected the sensor to ADC0 then the following function will
get the status of the sensor.

int sensor_value;
sensor_value=ReadADC(0);	//Read Channel number 0

For the resistor value as show in the schematic I found out that if no obstacle
is in front of the sensor then the value of sensor_value is around 660
and when an Obstacle comes to about 6inches from it, the value goes
up to 745. And when the Obstacle comes near than 2.5 inches
the value saturates to 1023. It is the MAX value and even if
the obstacle comes nearer than that the value remains 1023 only.

Please note that these reading vary with the type of obstacle. Some Object
reflects more IR than others. Some reflects very less and then even cannot be
detected. The above result was for my palm as the obstacle. Some Object which
reflects IR poorly is wood which is painted dark (like a door).

Making the IR Obstacle Sensor

The IR Obstacle sensor for our robot will be made up of three such sensor elements.
The sensors will be mounted in front of the robot. One sensor is mounted in
the middle and other two on the left and right side respectively.

sensor placement in our robot

Three IR Reflectance Sensor in front of our Robot.

The whole sensor module is made on a piece of veroboard
which you can buy from your local electronic hobby dealer. We begin by cutting
the board into a required size. The board can be cut by a small hacksaw
which can be easily bought from a hardware and tool dealer.


A piece of veroboard.


A piece of veroboard cut to required size.

Now we need to drill two holes for mounting. Then we can use screws, nuts and
stand-offs to mount the board firmly on the
. I have used a powered drill to do the job in seconds but
if you don’t have it you can use a hand drill too.

veroboard drilled

Veroboard Drilled and Screws Mounted.

On the back side, we install spacers on the screws, this will keep some space
between the PCB and the chassis.

The black thing is the spacer.

Finally the PCB Can be Mounted on the chassis.

ir sensor board mounted on robot chassis

Sensor Board Mounted On the Chassis.

Construct the IR Sensor as shown in the schematic. The Final Board should look
like this.

ir sensor board

The Complete IR Sensor board.

Please note that I have use presets instead of the fixed 22K resistors. But
you should use fixed 22K resistors. The board is connected to the xBoard
using a standard 8 PIN Relimate Connector. The xBoard
has a 8 PIN sensor connector where you can connect a variety
of sensors. The sensor connector also provides 5v and GND as required by the
sensors. The pinout of the xBoard
sensor connector is shown below.

xboard's sensor port

Sensor Port of xBoard.

Connect the Right Sensor To ADC0, Center Sensor To ADC1 and Left Sensor to
ADC2. Now you are done with the sensor construction part. Lets move on to the
testing part.

Testing the IR Sensors.

Below is a small test program that reads and displays the value of all 3 sensors
in the LCD Screen. Before attempting the program please go through the LCD
Interfacing Routines of xAPI


                 xBoard(TM) v2.0 Sample Programs


Description :  Simple Utility to View 3 sensor readings on LCD Screen.
            Sensors Must be connected to ADC0,ADC1,ADC2

Author      : Avinash Gupta
Web         : www.eXtremeElectronics.co.in


            Copyright 2008-2010 eXtreme Electronics, India

#include <avr/io.h>

#include <util/delay.h>

#include "lcd.h"

void InitADC()
   ADMUX=(1<<REFS0);                                     // For Aref=AVcc;
   ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);    //Rrescalar div factor =128


uint16_t ReadADC(uint8_t ch)
   //Select ADC Channel ch must be 0-7

   //Start Single conversion

   //Wait for conversion to complete

   while(!(ADCSRA & (1<<ADIF)));

   //Clear ADIF by writing one to it

   //Note you may be wondering why we have write one to clear it
   //This is standard way of clearing bits in io as said in datasheets.
   //The code writes '1' but it result in setting bit to '0' !!!



void Wait()
   uint8_t i;

void main()
   uint16_t adc_result[3]; //Array

   //Wait for LCD to Startup

   //Initialize LCD


   //Initialize ADC

   //Welcome and Intro
   LCDWriteString("xBoard Sensor");
   LCDWriteStringXY(0,1,"Test Utility");


   LCDWriteString("S#1  S#2  S#3 ");

      adc_result[0]=ReadADC(0);           // Read Analog value from channel-0

     adc_result[1]=ReadADC(1);           // Read Analog value from channel-1
     adc_result[2]=ReadADC(2);           // Read Analog value from channel-2

      LCDWriteIntXY(0,1,adc_result[0],4); //Print the value 
     LCDWriteIntXY(5,1,adc_result[1],4); //Print the value 

     LCDWriteIntXY(10,1,adc_result[2],4); //Print the value 


Compile and burn the program to xBoard
. After That Connect LCD and Sensor Board You made and power
up the board. The screen should show the reading of the 3 sensors as shown below.

xboard sensor test utility

Screen Shot of xBoard Sensor Test Utility.

When you bring a Obstacle near one of the sensors the value should go up, bring
it nearer and value should approach 1023. Please note down the values of the
sensor when NO obstacle is in front of it and when an Obstacle is nearly 6inches
(15cm) from it. You will need these value for calibrating the master robot program.


If you don’t know how to compile and burn programs for xBoard
then please consult the following documents.

Otherwise I have also provided a HEX file ready to burn to the ATmega32 (or
ATmega16 MCU) and run the Utility in no time.

If the LCD Does not show any text please adjust the contrast

If the sensors don’t work as expected then please check the connections. To
check if the IR LEDs are working you can use a Handicam (or any other digital
camera like you mobile camera). The invisible IR rays are easily visible on
Cameras. If the LEDs are NOT emitting IR then check their connections.