Programs Guide ATMega8 PDF
Programs Guide ATMega8 PDF
N O TE S S P A CE
Learn basics of
robotics in a week!
Complete
Guide
Simple Robotic Programs
Dattaraj Vidyasagar
Vidyasagar Academy, Akola Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
www.vsagar.org
Page | 3 Page | 4
Do-it-yourself practical guide Simple robotic programs
Useful notes for the students of robotics
For fundamental & advanced course in robotics
(The notes includes do-it-yourself complete practical guide of learning robotics)
Published By:
Simple Robotic Programs Vidyasagar Academy,
Mrs. C.D. Vidyasagar
Complete guide on how to learn and enjoy robotic programming 42/3A, 772, Renuka, Ranpise Nagar, Akola
(With more than 30 programs for your single robotic kit of ATMega8/16/128) 99-60-06-45-64
These notes are the part of complete book, available
for viewing on iPhone, iPad or Android
Designed, Typed & Edited by: Use ISBN# or ASIN# for downloading
Acknowledgements:
The author extends his thanks and profound appreciation for all those who helped him directly or indirectly in
bringing this notes in present stature.
The author welcomes any suggestions, both from the teachers and the students, for further improvement of this
notes, at [email protected]. Visit his profile on www.vidyasagarsir.com to know more about him.
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 5 Page | 6
Contents INTRODUCTION
INTRODUCTION ................................................................................................................................................. 6
Learning robotics is a real fun and gaining logical knowledge. In this guide, you will
BASIC PROGRAMS USING LEDS .......................................................................................................................... 7 understand the step by step process of designing infinite combinations of different programs
in robotics.
PROGRAM OF BLINKING 4 LEDS.................................................................................................................................... 7
RUNNING EFFECT OF 4 LEDS ........................................................................................................................................ 8 Each program is carefully written with lots of comments within the program. This will help
RUNNING EFFECT OF LEDS USING FOR LOOP ................................................................................................................. 9 you understand the command lines, different syntax and logical structure of the programs.
UNDERSTANDING THE IR SENSORS ...................................................................................................................11 I suggest you to start from the very first program. It will give you the basic idea of controlling
the primary hardware of your robot – the LEDs. Then the next program will be more fun to
BASIC PROGRAM OF IR SENSOR ................................................................................................................................... 11
see that the LEDs produce running effect.
CONTROLLING 4 LEDS WITH 2 SENSORS ....................................................................................................................... 12
In running effect of LEDs, lots of command lines are needed to control the sequential on/off of
HOW TO CONTROL MOTORS? ...........................................................................................................................14
the LEDs. So in the next program using the ‘for loop’, you understand that how we can
MOVING THE ROBOT FORWARD & BACKWARD ........................................................................................................ 14 reduce the length of the program in just two to three steps.
TURNING THE ROBOT LEFT & RIGHT .................................................................................................................... 15
Then start with the basics of using IR sensors. A line following robot consists of an infrared
U-TURNING ROBOT ............................................................................................................................................. 16
light sensor and an infrared LED. It works by illuminating a surface with infrared light; the
MOTORS WITH SENSORS ..................................................................................................................................18 sensor then picks up the reflected infrared radiation and, based on its intensity, determines
the reflectivity of the surface in question.
CONTROLLING MOTORS WITH SENSORS ......................................................................................................................... 18
CONTROLLING 2 MOTORS WITH 2 SENSORS ................................................................................................................... 19 So in this next program, you will understand the use of sensor to control the LEDs first. Then
step by step you can go through to understand the basics of black line and white line following
LINE FOLLOWING ROBOTS ................................................................................................................................21
robot’s logic behind its simple working.
BLACK LINE FOLLOWING ROBOT (BLFR) ........................................................................................................................ 21
After that you can move to understand the use of different types of sensors as given in the
WHITE LINE FOLLOWING ROBOT (WLFR) ...................................................................................................................... 23
following programs.
STRAIGHT TRACK FOLLOWING U-TURN ROBOT ................................................................................................................ 25
CROSSED TRACK FOLLOWING ROBOT (USING 2 SENSORS).................................................................................................. 27 If you come across any difficulty or want to ask questions regarding the programs, please feel
DIVERTED TRACK FOLLOWING ROBOT ........................................................................................................................... 29 free to contact me at: www.vsagar.org/contact-us. I will reply you, ASAP.
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 7 Page | 8
HOW TO USE AND RUN THE PROGRAM IN YOUR KIT?
BASIC PROGRAMS USING LEDS
1. First read the program carefully. Understand the steps as taught to you.
2. Connect your kit to USB port.
Program of Blinking 4 LEDs 3. Burn the 'hex' file into your kit.
/*
4. All the LEDs connected to PB4-PB1 will glow for some time
Applicable to ATMega8/16/32/128 5. After some time all the LEDs will be OFF.
*** CONNECTION DETAILS OF KIT *** 6. This on/off will repeat continuously till the battery is connected.
PB4-PB1 : output pins of PORTB, connected to 4 LEDs 7. Is it working? Nice! You did it.
8. Now don't forget to visit www.vsagar.org/contact-us and give your feedback.
*/
Running effect of 4 LEDs
#define F_CPU 12000000UL // defining the crystal frequency 12MHz /*
// given on your dev. board of ATMega8 Applicable to ATMega8/16/32/128
*** CONNECTION DETAILS OF KIT ***
#include <avr/io.h> // including the input-output PB4-PB1 : output pins of PORTB, connected to 4 LEDs
// to define the input output ports and pins */
// this file is inside the AVR folder
#define F_CPU 12000000UL // defining the crystal frequency 12MHz
// given on your dev. board of ATMega8
#include <util/delay.h> // including the delay file #include <avr/io.h> // including the input-output
// this file is inside the // to define the input output ports and pins
// utilities (util) folder // this file is inside the AVR folder
int main() // starting the main function of program #include <util/delay.h> // including the delay file
// this file is inside the
{ // main function brace opened // utilities (util) folder
DDRB=0b00011110; // PB4-PB1 of PORTB are defined as output pins int main() // starting the main function of program
{ // main function brace opened
while(1) // starting the infinite loop to repeat the action infinitely DDRB=0b00011110; // PB4-PB1 of PORTB are defined as output pins
while(1) // starting the infinite loop to repeat the action infinitely
{ // while loop brace opened { // while loop brace opened
PORTB=0b00000010; // 1st LED at PB1 becomes ON
PORTB=0b00000000; // PB4-PB1 producing '0' output _delay_ms(300); // it remains ON for 300ms=0.3sec
// so the 4 LEDs are OFF PORTB=0b00000100; // 2nd LED at PB2 becomes ON
_delay_ms(1000); // the LEDs remain OFF for 1000ms=1sec _delay_ms(300); // it remains ON for 300ms=0.3sec
PORTB=0b00001000; // 3rd LED at PB3 becomes ON
PORTB=0b00011110; // PB4-PB1 producing '1' output _delay_ms(300); // it remains ON for 300ms=0.3sec
// so the 4 LEDs become ON PORTB=0b00010000; // 4th LED at PB4 becomes ON
_delay_ms(2000); // the LEDs remain ON for 1000ms=1sec _delay_ms(300); // it remains ON for 300ms=0.3sec
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 9 Page | 10
I M PO R T AN T C O M M E N T IMPORTANT NOTE
After this last step, the program will jump to first step and will repeat infinitely, since it is 1. The 'for loop' will start from '0'. At this condition, PB1=1 only.
within while(1) loop called as infinite loop. 2. When i=1, PB2=1 only.
3. When i=2, PB3=1 only.
You can also change the delay in milliseconds as required.
4. When i=3, PB2=4 only.
5. In this way, using 'for loop' you can get the same effect of running LEDs which we
Running effect of LEDs using FOR LOOP
saw in previous project of running LEDs.
/*
Applicable to ATMega8/16/32/128
*** CONNECTION DETAILS OF KIT ***
PB4-PB1 : output pins of PORTB, connected to 4 LEDs
*/
#define F_CPU 12000000UL // defining the crystal frequency 12MHz
// given on your dev. board of ATMega8
#include <avr/io.h> // including the input-output
// to define the input output ports and pins
// this file is inside the AVR folder
#include <util/delay.h> // including the delay file
// this file is inside the
// utilities (util) folder
1. First read the program carefully. Understand the steps as taught to you.
2. Connect your kit to USB port.
3. Burn the 'hex' file into your kit.
4. Now connect battery and switch on the kit.
5. The 4 LEDs will glow one-by-one in a particular direction.
6. Is it working? Nice! You did it.
7. Now don't forget to visit www.vsagar.org/contact-us and give your feedback.
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 11 Page | 12
HOW TO USE AND RUN THIS PROGRAM IN YOUR KIT?
UNDERSTANDING THE IR SENSORS
1. First read the program carefully. Understand the steps as taught to you.
2. Connect your kit to USB port.
Basic program of IR sensor 3. Burn the 'hex' file into your kit.
/*
4. Connect one IR sensor, to PC0 in your kit.
Applicable to ATMega8/16/32/128 5. Now connect battery and switch on the kit.
*** CONNECTION DETAILS OF KIT *** 6. Keep a white paper below the IR sensor.
1) The 4 LEDs in your kit, are internally connected to PB4-PB1 7. All the LEDs connected to PB4-PB1 will glow.
2) Connect one IR sensor to PC0 in your kit. 8. Remove the white paper, so that all the LEDs will be OFF.
*/ 9. Now don't forget to visit www.vsagar.org/contact-us and give your feedback.
#define F_CPU 12000000UL // defining the crystal frequency 12MHz Controlling 4 LEDs with 2 sensors
// given on your dev. board of ATMega8 /*
#include <avr/io.h> // including the input-output Applicable to ATMega8/16/32/128
// to define the input output ports and pins *** CONNECTION DETAILS OF KIT ***
// this file is inside the AVR folder 1) The 4 LEDs in your kit, are internally connected to PB4-PB1
int main() // starting the main function of program 2) Connect two IR sensors: LS to PC3 and RS to PC0.
{ // main function brace opened */
DDRB=0b00011110; // PB4-PB1 of PORTB are defined as output pins
DDRC=0b0000000; // PC6-PC0 of PORTC are defined as input pins #define F_CPU 12000000UL // defining the crystal frequency 12MHz
int s=0; // 's' is the variable to store sensor status // given on your dev. board of ATMega8
// when we write 'int s', #include <avr/io.h> // including the input-output
// it creates a location in memory of microcontroller. // to define the input output ports and pins
// initially '0' is stored into 's' memory location // this file is inside the AVR folder
while(1) // starting the infinite loop to repeat the action infinitely int main() // starting the main function of program
{ // while loop brace opened { // main function brace opened
s=PINC&0b0000001; // assigning the variable 's' to PC0 of PORTC DDRB=0b00011110; // PB4-PB1 of PORTB are defined as output pins
// so that the output status of sensor will be DDRC=0b0000000; // PC6-PC0 of PORTC are defined as input pins
// stored into the variable 's' int s=0; // 's' is the variable to store sensor status
// since one sensor in our kit is connected // when we write 'int s', it creates
// to PC0 and other to PC3 // a location in memory of microcontroller.
// Note: PC3 sensor is not used in this program // initially '0' is stored into 's' memory location
// only PC0 sensor is used while(1) // starting the infinite loop to repeat the action infinitely
if(s==0b0000001) // white surface below the sensor { // while loop brace opened
{ s=PINC&0b0001001; // masking the variable 's' to PC0 & PC3 of PORTC
PORTB=0b00011110; // all LEDs will be ON // so that the output status of sensors will be
} // recorded in variable 's'
else // black surface below the sensor // Note: PC3 is left sensor and PC0 is right sensor
{ // When it is black surface below a sensor, its output=0
PORTB=0b00000000; // all LEDs will be OFF // When it is white surface below it, its output=1
} if(s==0b0001001) // white surface below both sensors
} // while loop brace closed {
PORTB=0b00011110; // all LEDs ON
} // main function brace closed }
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 13 Page | 14
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 15 Page | 16
HOW TO USE AND RUN THIS PROGRAM IN YOUR KIT?
PORTB=0b00010010; // moving forward
1. First read the program carefully. Understand the steps as taught to you. _delay_ms(1000); // for 1sec
2. Connect your kit to USB port.
} // while loop closed
3. Burn the 'hex' file into your kit.
4. Now keep your robot on a plane surface and switch it on.
} // main function closed
5. Wait for 2sec. Now your robot will move forward for some time.
6. Then it will stop.
7. Now it will move in backward direction for some time. HOW TO USE AND RUN THIS PROGRAM IN YOUR KIT?
8. In this way, it will continue to move to-and-fro for infinite times.
9. When you finish testing the program switch it OFF. 1. First read the program carefully. Understand the steps as taught to you.
10. Is it working? Nice! You did it. 2. Connect your kit to USB port.
11. Now don't forget to visit www.vsagar.org/contact-us and give your feedback. 3. Burn the 'hex' file into your kit.
4. Connect both the IR sensors, to PC3 & PC0 in your kit.
TURNING THE ROBOT LEFT & RIGHT 5. Now connect battery and switch on the kit.
/* 6. First your robot will move forward.
Applicable to ATMega8/16/32/128 7. Then it will turn right.
*** CONNECTION DETAILS OF KIT *** 8. Then again it will move forward, the turn left.
The 2 motors in your kit, are connected to PB4-PB1, as follows: 9. Finally it will go forward and then stop. And repeat endlessly.
Left motor: PB4 -> (+) and PB3 -> (-) 10. Is it working? Nice! You did it.
Right motor: PB1 -> (+) and PB1 -> (-) 11. Now don't forget to visit www.vsagar.org/contact-us and give your feedback.
*/
U-TURNING ROBOT
#define F_CPU 12000000UL // defining the crystal frequency 12MHz
/*
// given on your dev. board of ATMega8
Applicable to ATMega8/16/32/128
*** CONNECTION DETAILS OF KIT ***
#include <avr/io.h> // including the input-output
The 2 motors in your kit, are connected to PB4-PB1, as follows:
// to define the input output ports and pins
Left motor: PB4 -> (+) and PB3 -> (-)
// this file is inside the AVR folder
int main() // starting the main function of program Right motor: PB1 -> (+) and PB1 -> (-)
{ // main function brace opened */
DDRB=0b00011110; // PB4-PB1 of PORTB are defined as output pins
while(1) // starting the infinite loop to repeat the action infinitely #define F_CPU 12000000UL // defining the crystal frequency 12MHz
{ // while loop brace opened // given on your dev. board of ATMega8
PORTB=0b00000000; // both motors OFF
_delay_ms(1000); // delay of 1000ms=1sec #include <avr/io.h> // including the input-output
PORTB=0b00010010; // both motors rotate FORWARD // to define the input output ports and pins
// so your robot will move forward for 3sec. // this file is inside the AVR folder
_delay_ms(2000); // delay of 2000ms=2sec. #include <util/delay.h> // including the delay file
PORTB=0b00010000; // turning right // this file is inside the
_delay_ms(300); // for 0.3sec // utilities (util) folder
PORTB=0b00010010; // moving forward int main() // starting the main function of program
_delay_ms(1000); // for 1sec { // main function brace opened
PORTB=0b00000010; // turning left DDRB=0b00011110; // PB4-PB1 of PORTB are defined as output pins
_delay_ms(300); // for 0.3sec
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 17 Page | 18
while(1) // starting the infinite loop to repeat the action infinitely
{ // while loop brace opened MOTORS WITH SENSORS
PORTB=0b00000000; // both motors OFF
_delay_ms(1000); // delay of 1000ms=1sec
PORTB=0b00010010; // both motors rotate FORWARD
Controlling motors with sensors
// so your robot will move forward for 3sec.
_delay_ms(2000); // delay of 2000ms=2sec. /*
PORTB=0b00010000; // turning right Applicable to ATMega8/16/32/128
_delay_ms(500); // for 0.5sec so that the robot will take U-turn *** CONNECTION DETAILS OF KIT ***
PORTB=0b00010010; // moving forward 1) The 2 motors in your kit, are connected to PB4-PB1, as follows:
_delay_ms(1000); // for 1sec Left motor: PB4 -> (+) and PB3 -> (-)
PORTB=0b00000010; // turning left Right motor: PB1 -> (+) and PB1 -> (-)
_delay_ms(500); // for 0.5sec so that the robot will take U-turn 2) Connect one IR sensor to PC0 in your kit.
// in opposite direction */
PORTB=0b00010010; // moving forward
_delay_ms(1000); // for 1sec #define F_CPU 12000000UL // defining the crystal frequency 12MHz
// given on your dev. board of ATMega8
} // while loop closed
#include <avr/io.h> // including the input-output
} // main function closed // to define the input output ports and pins
// this file is inside the AVR folder
HOW TO USE AND RUN THIS PROGRAM IN YOUR KIT? #include <util/delay.h> // including the delay file
// this file is inside the
1. First read the program carefully. Understand the steps as taught to you. // utilities (util) folder
2. Connect your kit to USB port. int main() // starting the main function of program
3. Burn the 'hex' file into your kit. { // main function brace opened
4. Connect both the IR sensors, to PC3&PC0 in your kit. DDRB=0b00011110; // PB4-PB1 of PORTB are defined as output pins
DDRC=0b0000000; // PC6-PC0 of PORTC are defined as input pins
5. Now connect battery and switch on the kit.
int s=0; // 's' is the variable to store the status value of sensor
6. First your robot will move forward.
// when we write 'int s', it creates a location in memory of
7. Then it will take U-turn. Adjust the delay if required.
// microcontroller. Initially '0' is stored.
8. Then again it will move forward, then left U-turn.
9. Finally it will go forward and then stop. And repeat endlessly.
while(1) // starting the infinite loop to repeat the action infinitely
10. Is it working? Nice! You did it.
{ // while loop brace opened
11. Now don't forget to give your feedback. s=PINC&0b0000001; // assigning the variable 's' to PC0 of PORTC
// so that the output status of sensor will be
Note: You can adjust the delay of 500ms for U-turn by trial and error. Try changing it to 700ms to 800ms.
// stored into the variable 's'
// since one sensor in our kit is connected
// to PC0 and other to PC3
Note: PC3 sensor is not used in this program only PC0 sensor is used
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 19 Page | 20
else // black surface below the sensor int s=0; // 's' is the variable to store the status value of sensor
{ // when we write 'int s', it creates
PORTB=0b00000000; // both motors are OFF // a location in memory of microcontroller.
} // initially '0' is stored into 's' memory location
while(1) // starting the infinite loop to repeat the action infinitely
} // while loop closed { // while loop brace opened
s=PINC&0b0001001; // assigning the variable 's' to PC0 of PORTC
} // main function closed // so that the output status of sensor will be
// stored into the variable 's'
// since one sensor in our kit is connected
HOW TO USE AND RUN THIS PROGRAM IN YOUR KIT? // to PC0 and other to PC3
1. First read the program carefully. Understand the steps as taught to you. Note: Left sensor is connected to PC3 and right sensor to PC0
2. Connect your kit to USB port. Burn the 'hex' file into your kit.
3. Connect both the IR sensors, to PC3 & PC0 in your kit. Now connect battery and switch if(s==0b0001001) // white surface below both sensors
on the kit. {
4. First keep white paper below the sensor. The robot will move in forward direction. PORTB=0b00010010; // both motors rotate in forward direction
5. Now keep black surface below it. Both motors will be OFF and the robot stops. // so robot moves forward
6. Is it working? Nice! You did it. }
7. Now don't forget to give your feedback.
else // black surface below the sensor
Controlling 2 motors with 2 sensors {
PORTB=0b00000000; // both motors are OFF
/*
}
Applicable to ATMega8/16/32/128
*** CONNECTION DETAILS OF KIT ***
} // while loop closed
1) The 2 motors in your kit, are connected to PB4-PB1, as follows:
Left motor: PB4 -> (+) and PB3 -> (-)
} // main function closed
Right motor: PB1 -> (+) and PB1 -> (-)
2) Connect the two IR sensors to PC3 & PC0 in your kit.
Connect LEFT SENSOR to PC3 and RIGHT SENSOR to PC0
HOW TO USE AND RUN THIS PROGRAM IN YOUR KIT?
*/
1. First read the program carefully. Understand the steps as taught to you.
#define F_CPU 12000000UL // defining the crystal frequency 12MHz
2. Connect your kit to USB port.
// given on your dev. board of ATMega8
3. Burn the 'hex' file into your kit.
#include <avr/io.h> // including the input-output
4. Connect both the IR sensors, to PC3&PC0 in your kit.
// to define the input output ports and pins
5. Now connect battery and switch on the kit.
// this file is inside the AVR folder
#include <util/delay.h> // including the delay file
6. First keep white paper below both the sensors.
// this file is inside the 7. The robot will move in forward direction.
// utilities (util) folder 8. Now keep black surface below the sensors.
int main() // starting the main function of program 9. Both motors will be OFF and the robot stops.
{ // main function brace opened 10. Is it working? Nice! You did it.
DDRB=0b00011110; // PB4-PB1 of PORTB are defined as output pins 11. Now don't forget to give your feedback on : http://www.vsagar.org/contact-us
DDRC=0b0000000; // PC6-PC0 of PORTC are defined as input pins
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 21 Page | 22
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 23 Page | 24
White line following robot (WLFR)
if(s==0b0000001) // white below RS and black below LS
/*
{
Applicable to ATMega8/16/32/128
PORTB=0b00000010; // only right motor rotates forward
*** CONNECTION DETAILS OF KIT ***
// so robot turns left
1) The 2 motors in your kit, are connected to PB4-PB1, as follows:
}
Left motor: PB4 -> (+) and PB3 -> (-)
else // white surface below both the sensors
Right motor: PB1 -> (+) and PB1 -> (-)
{
2) Connect the two IR sensors to PC3 & PC0 in your kit.
PORTB=0b00000000; // both motors are OFF and robot stops
Connect LEFT SENSOR to PC3 and RIGHT SENSOR to PC0
}
*/
} // while loop closed
#define F_CPU 12000000UL // defining the crystal frequency 12MHz
} // main function closed
// given on your dev. board of ATMega8
#include <avr/io.h> // including the input-output HOW TO USE AND RUN THIS PROGRAM IN YOUR KIT?
// to define the input output ports and pins
// this file is inside the AVR folder 1. First read the program carefully. Understand the steps as taught to you.
2. Connect your kit to USB port. Burn the 'hex' file into your kit.
int main() // starting the main function of program 3. Connect both the IR sensors, to PC3 & PC0 in your kit.
{ // main function brace opened 4. Now make a black track of oval shape. Make this track particularly on smooth surface.
DDRB=0b00011110; // PB4-PB1 of PORTB are defined as output pins 5. Now according to the width of track adjust the distance between two sensors.
DDRC=0b0000000; // PC6-PC0 of PORTC are defined as input pins
6. Keep them apart from each other as required. Place your robot on the track such that
int s=0; // 's' is the variable to store the status value of sensor
the two sensors will be on white. Now connect battery and switch on the kit. Your robot
// when we write 'int s', it creates
// a location in memory of microcontroller. will follow the track.
// initially '0' is stored into 's' memory location 7. Is it working? Nice! You did it.
while(1) // starting the infinite loop to repeat the action infinitely IMPORTANT NOTE
{ // while loop brace opened
1. If the robot is not following the track correctly, then adjust the sensitivity of the IR
s=PINC&0b0001001; // assigning the variable 's' to PC0 of PORTC
// so that the output status of sensor will be
sensor by turning adj. screw ANTICLOCKWISE to decrease sensitivity of the sensors.
// stored into the variable 's' 2. For this, first keep black surface below both sensors. The indicators of the sensors must
// since one sensor in our kit is connected remain OFF.
// to PC0 and other to PC3 3. So by trial and error, adjust the sensitivity and then check it on black track.
4. Remember, when you are done correctly with this adjustment, your robot must follow
Note: Left sensor is connected to PC3 and right sensor to PC0 the track correctly.
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 25 Page | 26
Straight track following U-turn robot
if((s==0b0011001)||(s==0b0011101))
/*
{
Applicable to ATMega8/16/32/128 Straight track
PORTB=0b00010000; // SOFT RIGHT
*** CONNECTION DETAILS OF KIT ***
}
1) The 2 motors in your kit, are connected to PB4-PB1, as follows:
if(s==0b0011111)
Left motor: PB4 -> (+) and PB3 -> (-)
{ 5 sensors array
Right motor: PB1 -> (+) and PB1 -> (-)
PORTB=0b00010100;
2) Connect the 5 sensors array to PC4 & PC0 in your kit.
_delay_ms(300);
Connect LEFTMOST SENSOR to PC4 and RIGHTMOST SENSOR to PC0
if(s==0b0011011)
*/ R
PORTB=0b00010010;
#define F_CPU 12000000UL
else
#include <avr/io.h>
continue;
int main()
5 sensors array }
{
} // while closed
DDRB=0b00011110;
} // main closed
DDRC=0b0000000;
int s=0; HOW TO USE AND RUN THIS PROGRAM IN YOUR KIT?
while(1) 1. First you will require the 5 sensor array card, which is readily available at us. You can
{ purchase it by contacting us at: http://www.vsagar.org/contact-us.
s=PINC&0b0011111; // masking the sensor status 2. Now connect the card to PC4 & PC0 in your kit. Connect LEFTMOST SENSOR to PC4
if(s==0b0000000) and RIGHTMOST SENSOR to PC0.
{ PC4 PC3 PC2 PC1 PC0 Value Action 3. Now keep a black paper or sheet below all the sensors. Switch on the robot battery
PORTB=0b00000000; // STOP
0 0 0 0 0 0 STOP
supply and adjust the sensitivity of all the sensors such that their indicator LEDs will
}
just turn OFF.
1 1 0 1 1 27 FORWARD 4. Once this adjustment is done, burn the code given above into your robot
if(s==0b0011011)
0 0 1 1 1 7 POWER LEFT microcontroller.
{
5. Now fix a long black track (approx. 1m length) on smooth floor or plywood using black
PORTB=0b00010010; // GO FORWARD 0 1 1 1 1 15 POWER LEFT
tape.
}
1 0 0 1 1 19 SOFT LEFT 6. Then keep the robot on the track such that its center IR sensor will be exactly on black
if((s==0b0000111)||(s==0b0001111)) track.
1 0 1 1 1 23 SOFT LEFT
{ 7. Switch on the battery supply of the robot. Your robot will start following the track up to
PORTB=0b00001010; // POWER LEFT 1 1 0 0 1 25 SOFT RIGHT the end.
} 8. When it reaches at the end of the track, all the sensor indicator LEDs will glow and the
1 1 1 0 0 28 POWER RIGHT
robot will take U-turn, until its middle sensor is again on black track.
if((s==0b0010011)||(s==0b0010111)) 1 1 1 0 1 29 SOFT RIGHT 9. Again it will follow the black track to the other end and will take U-turn.
{ 10. In this way, it will continue traversing the black track to-and-fro.
1 1 1 1 0 30 POWER RIGHT
PORTB=0b00000010; // SOFT LEFT 11. Is it working? Nice!
} 1 1 1 1 1 31 U-TURN 12. Now don’t forget to give your valuable feedback to us on our website:
http://www.vsagar.org/contact-us.
if((s==0b0011100)||(s==0b0011110))
{
PORTB=0b00010100; // POWER RIGHT
}
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 27 Page | 28
PORTB=18; // move forward - 00010010
Crossed track following robot (using 2 sensors)
}
/*
Applicable to ATMega8/16/32/128
} // while closed
*** CONNECTION DETAILS OF KIT ***
1) The 2 motors in your kit, are connected to PB4-PB1, as follows:
} // main closed
Left motor: PB4 -> (+) and PB3 -> (-)
Right motor: PB1 -> (+) and PB1 -> (-) HOW TO USE AND RUN THIS PROGRAM ON YOUR KIT?
2) Connect the 2 sensors to PC3 & PC0 in your kit.
Connect LEFT SENSOR to PC3 and RIGHT SENSOR to PC0 1. First read the program carefully and understand the logic used in it to take decision
*/ particularly on crossing track point.
2. Then burn your program in your kit and create a crossed track as shown in the above
#define F_CPU 12000000UL // defining clock diagram.
Crossed track
#include <avr/io.h> 3. Switch on your robot and keep it on the track such that the two IR sensors will be apart
#include <util/delay.h>
from each other and will have white surface below them.
4. Your robot will move forward. On the turns it will take the turns properly.
int main()
5. When it will arrive at the crossed track, it will stop and will take the necessary decision
{
and then will go straight.
DDRB=30; // PB4-PB1 set for motors
6. In this way, it will follow the track endlessly.
DDRC=0; // PORTC is set as input port
7. Now I will give you one task here:
int s=0; // variable to store sensor status
a. When your robot arrives at the crossing track, suppose we want to turn it
while(1) // infinite loop rather than going straight.
{ b. Then what will you do? What particular modifications are needed in the
s=PINC&0b0001001; // masking PC3 and PC0 to record sensor status above program? Can you work out the same…?
if(s==8)
{
PORTB=20; // power right turn - 00010100
}
if(s==1)
{
Simple robot used for the above program
PORTB=10; // power left turn - 00001010
}
if(s==9)
{
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 29 Page | 30
Diverted track following robot
if((LS==0b0001000)&(RS==0b0000000))
{
PORTB=0b00000010; // turn left
}
if((LS==0b0000000)&(RS==0b0000001))
continued on next page…
{
PORTB=0b00010000; // turn right
}
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 31 Page | 32
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 33 Page | 34
Edge avoider + obstacle avoider robot
if(S==8) // LS on white, RS is outside the edge
/*
{
Applicable to ATMega8/16/32/128
PORTB=0; // stop
_delay_ms(1000);
*** CONNECTION DETAILS OF KIT ***
PORTB=12; // move backward
1) Motors connected to PB4-PB1, as follows:
_delay_ms(500); // adjust it as per requirement
Left motor: PB4 -> (+) and PB3 -> (-)
PORTB=16; // turn right
Right motor: PB1 -> (+) and PB1 -> (-)
_delay_ms(300); // adjust as required
2) Connect the 3 sensors to PC3, PC1 & PC0 in your kit.
S=9;
See the note given at the end of this program.
}
*/
if(S==1) // RS on white, LS is outside the edge
#define F_CPU 1200000UL // defining clock frequency for accurate delay
{
#include <avr/io.h> // includes input/output header file
PORTB=0; // stop
#include <util/delay.h> // includes delay header file
_delay_ms(1000);
int main(void)
PORTB=12; // move backward
{
_delay_ms(500); // adjust it as per requirement
DDRB=0b00011110; //PORTB as output Port connected to motors
PORTB=2; // turn left **** NOTE THIS STEP ****
DDRC=0b0000000; //PORTC Input port connected to Sensors
_delay_ms(300); // adjust as required
int S=0;
S=9;
while(1) // infinite loop
}
{
} // while closed
S=0b00001011; // masking the status of both sensors
} // main closed
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org
Page | 35 Page | 36
Wall following robot
{ // edge avoiding logic
/*
if(S==0) // both sensors outside the edge
Applicable to ATMega8/16/32/128
{
PORTB=0; // stop
*** CONNECTION DETAILS OF KIT ***
_delay_ms(1000);
1) Motors connections to PB4-PB1:
PORTB=12; // move backward
Left motor: PB4 -> (+) and PB3 -> (-)
_delay_ms(500); // adjust it as per requirement
Right motor: PB1 -> (+) and PB1 -> (-)
PORTB=16; // turn right
2) Connect only one sensor (WS) to PC0 in your kit.
_delay_ms(300); // adjust as required
Fix the sensor vertically to detect the presence of wall.
S=9;
*/
}
#define F_CPU 1200000UL // defining clock frequency for accurate delay
#include <avr/io.h> // includes input/output header file
if(S==8) // LS on white, RS is outside the edge
#include <util/delay.h> // includes delay header file
{
int main()
PORTB=0; // stop
{
_delay_ms(1000);
DDRB=0b00011110; //PORTB as output port connected to motors
PORTB=12; // move backward
DDRC=0b0000000; //PORTC input port connected to Sensors
_delay_ms(500); // adjust it as per requirement
int WS=0;
PORTB=16; // turn right
_delay_ms(300); // adjust as required
while(1) // infinite loop
S=9;
{
}
WS=0b00000001; // masking the status of wall sensor
if(S==1) // RS on white, LS is outside the edge
if(WS==0) // there is no wall in front of sensor
{
{
PORTB=0; // stop
PORTB=18; // move forward
_delay_ms(1000);
}
PORTB=12; // move backward
_delay_ms(500); // adjust it as per requirement
if(WS==1)
PORTB=2; // turn left **** NOTE THIS STEP ****
{
_delay_ms(300); // adjust as required
PORTB=20; // power right turn
S=9;
_delay_ms(300);
}
if(WS==0)
} // end of edge avoiding logic
{
} // while closed
PORTB=18;
} // main closed
}
HOW TO USE AND RUN THIS PROGRAM ON YOUR KIT? else
continue;
1. You can test the working of this program on a smooth surface of a table. The table-top }
should be off-white or white, but should not be black.
} // while closed
Note: Connect LEFT SENSOR to PC3, MIDDLE SENSOR to PC1 and RIGHT SENSOR to PC0 of PORTC pins. Also fix } // main closed
middle sensor vertically and the other two horizontally as shown in the image above. So middle sensor will sense
the obstacles in between the path and LS & RS will detect the edges of the table-top.
Simple robotic programs with do-it-yourself practical guide, www.vsagar.org Simple robotic programs with do-it-yourself practical guide, www.vsagar.org