I began work on my first robot about two years ago. For no particular reason, I decided to begin with a line following robot and truth be told, my first attempt at building it was a complete failure. Looking back at my efforts, I believe, for a beginner, I was rather too ambitious. The circuit that I had designed had a bunch of unnecessary stuff which I then believed would give my robot an edge over the others. But it never worked and I had to start all over from scratch.
In my second attempt, I managed to get the robot on track. As delighted as I was with my first robot, it was nowhere near where I wanted it to be. I took it to a couple of competitions and not much to my surprise, it failed in both of them. I knew it was time to make some major changes in the design.
In my third attempt, (actually, it wasn’t the third, it was a revision of my second attempt) I updated the firmware and came up with a much more stable and accurate version. It performed well when tested and much to my delight, it finished first in two competitions and second in another. Though I was proud with what I had achieved, I felt that the robot was visually rather unappealing. And you’ll come to know why from its picture given above. It had wires running all over it and I had no other option but to rebuild it. A month or so later, I had the robot all ready and that is the current version of my first robot.
In this instructable I will guide you through the steps that I've followed in building the current version of my robot. It’s one of those robots which belong to the “scratch-built” category. You might find it difficult to find all the parts that I have used. So I insist you to read through the steps that I have followed, and then implement it in your own way with the parts that you've found.
This instructable requires that you are familiar with the following:
• Soldering and related equipments
• Hand tools like screwdrivers, wire cutters and strippers
• Reading schematics and connection diagrams
• C/C++ programming for AVR microcontrollers (optional)
Step 1: Gather the parts
• Atmega32 microcontroller
• 16x2 alphanumeric LCD
• LM2576 Step-Down Voltage Regulator
• L298 Motor Driver Module
• Lithium-Ion Rechargeable Battery Pack 11.1V 3000mAh 2C
• 9 x IR Transmitter and Receiver pair (5mm diameter)
• 10K resistor array (9 pin)
• 2N2222 transistor (or any other npn type)
• Resistors and Capacitors
• 2 x Centre Shaft Economy Series DC Motor (100 RPM)
• Switches and Push Buttons
• Dot Matrix Prototype PCB
• Plastic Enclosure
• Single-strand Wire
• 40-pin DIP socket
• 6 pin FRC right angle male connector
• Metal Studs
• Plastic Spacers
• Nuts and Bolts
• Ball Caster Wheel
• 2 x Motor Mounting Clamp
• 2 x Wheel (50mm diameter)
• Cables and Connectors
• Soldering Iron
• Screw Driver
• Wire Cutter and Stripper
• Something to cut and drill plastic
In addition to the above mentioned components, you will also require an AVR programmer and a battery charger.
Now, let’s get started shall we?
Step 2: How many sensors?
The robot presented here has a total of nine sensors - eight of them arranged in a line and the other in front of the line at the center. Choosing the number 8 has the advantage that you can represent the status of all eight sensors using a single byte with each bit representing the state of one sensor. This will be discussed in detail in later steps.
Step 3: Batteries...Which one to choose?
The one shown in the picture is what I have used. It is a Lithium-ion battery pack rated at 11.1V 3000mAh 2C. It has separate cables for charging and discharging. I suggest you read this excellent article on batteries written by ladyada. You can find it here.
Step 4: The Voltage Regulator
SELECTING THE COMPONENTS
From the datasheet of LM2576 switching regulator (Page 12, Figure 4), you will find the value of the inductor that is to be used. It depends on the maximum input voltage and maximum output current. In my instance, the maximum input voltage is about 12V and maximum output current does not exceed 200mA. So I must have chosen a 680uH inductor. But unfortunately I couldn’t find one. All I got was a 390uH and it worked just fine.
The value of the input and output capacitors aren’t that critical. Any value above 100uF, 25V should work. The diode should be a fast recovery diode – preferably a Schottky. The one that I have used is 1N5822. The heatsink is not necessary, however I have added one as a measure of safety.
Given below are the schematic and board layout files of the voltage regulator designed in Cadsoft Eagle. You can also download the pdf files from below. You will have to get it ready on PCB or you can make it on a perfboard.
Step 5: The Sensor Array
The sensor array circuit can be considered as two parts-one for the emitter array and the other for the detectors. Let's see each of these in detail.
DESIGNING THE IR EMITTER ARRAY
When you open the schematic file, you will notice that the IR emitters are connected in a series-parallel form. I obtained this design from LED series/parallel array wizard. All I had to do was enter the specs of the LED and the array (given below) and the wizard showed me the arrangement that consumes the least amount of power.
Source voltage = 5V
Diode forward voltage = 1.5V
Diode forward current = 5mA
No. of LEDs in your array = 9
The transistor shown in the picture is used to turn the IR emitters ON and OFF. I have used a 2N2222 but you can use any other n-p-n transistor. The emitters are turned ON only when a reading is to be taken. This method reduces the total power consumed by the sensor array and most importantly, the effect of ambient light on sensor values is also reduced. This is accomplished by taking two readings - one with the emitters ON and the other with the emitters OFF. Subtracting the sensor values obtained in two cases, we will obtain a value that is independent of ambient light.
DESIGNING THE IR DETECTOR ARRAY
The IR detector circuit is essentially a potential divider with the IR detector connected in series with a resistor. The eight sensor array is read using the ADC and the ninth sensor is read using Pin 29 (PC7) of Atmega32. For the eight detectors, I have used a 10K resistor array and for the ninth detector I have used a separate 10K resistor.
Step 6: The Motor Driver
The module that I have used is based on the L298 motor driver IC. It can drive two motors independently. The operating voltage is between 8V and 48V and the module can handle a maximum current of 2A per motor.
- I1 and I2 are logic input pins corresponding to output pins OP1 and OP2. These output pins are connected to the left motor.
- I3 and I4 are logic input pins corresponding to output pins OP3 and OP4. These output pins are connected to the right motor.
- EA is the enable input for OP1 and OP2.
- EB is the enable input for OP3 and OP4.
- VCC and GND are the supply and ground terminals for driving the motor.
- 5V and GN are the supply input to the logic pins.
- EA, EB and 5V pins are connected to 5V supply.
- VCC is connected to battery output.
- I1 is connected to Pin 19 (PWM pin)
- I2 is connected to Pin 20
- I3 is connected to Pin 18 (PWM pin)
- I4 is connected to Pin 21
SPEED AND DIRECTION CONTROL
The microcontroller is programmed to generate a square wave of 500Hz frequency on pins 18 and 19. The dutycycle of these signals can be controlled independently.
When a logic 0 is written to Pin 20 and if dutycycle on Pin 19 is 100%, the left motor rotates in forward direction at full speed. When a logic 1 is written to Pin 21 and if dutycycle on Pin 18 is 0%, the left motor rotates in the opposite direction at full speed.
So the logic level at pins 20 and 21 determines the direction of rotation and dutycycle on Pins 18 and 19 determines the speed of rotation.
Step 7: Wiring the "Brain"
Referring to the pin diagram of Atmega32, the connections to be made are given below:
Pins 33 to 40 - sensor array (analog pins)
Pins 31 to 28 - sensor array (gnd, vcc, ninth sensor and emitter ON/OFF pins)
Pin 25 - Top Push Button Pin
Pin 24 - Right Push Button Pin
Pin 23 - Middle Push Button Pin
Pin 22 - Left Push Button Pin
Pins 18 to 21 - motor control pins
Pins 1 to 3 - LCD Control Pins (RS, R/W and Enable)
Pins 14 to 17 - LCD Data pins (DB4 to DB7)
Pins 6 to 11 - ISP header
Step 8: Charging and Programming Headers
Step 9: Attaching the LCD and the Push Buttons
Step 10: Attaching the Battery and the Switch
Step 11: Attaching the Motors and the Wheels
Step 12: Fixing the Caster Wheel
Step 13: Mounting the Sensor Array
Step 14: Connecting the Motor Driver
Step 15: Connecting the Voltage Regulator
Step 16: Let's Complete the Hardware
Step 17: Setting Up the Programmer
• AVR programmer - the one that I have used is USBasp. If you have already got another programmer for AVR you can use that one too.
• USB cable - you will need it to connect the programmer with your PC or laptop.
• winAVR - this is a software bundle that comes with a compiler, a programmer and a debugger for the AVR series of microcontrollers. You will have to download and install it.
• The winAVR comes with an editor - the Programmer's Notepad. But the one that I have used is Notepad++ Portable with winAVR plug-in installed in it. You can download it from below.
Now the programmer is connected as shown in the picture. Before uploading the program, we will have to configure the AVR. This is done by writing a pair of bytes called fusebits. This is discussed in detail in the next step.
You can download all the libraries and the program from below.
Step 18: Writing the Fusebits
I have attached an MS-DOS Batch file which you can download. To write the fusebits, connect the programmer to the AVR and run this batch file. You should be able to see the DOS screen shown above.
In the next few steps, we will see some of the important commands used in the program. There are a total of four libraries, one each for the LCD, the sensor, the motors and the push buttons.
Step 19: Push Button Library Commands
init_pushbutton() - activates internal pull-up on push button pins. Call this function once before using any other function of this library.
get_single_button_press() - checks whether any button is pressed. If it detects any keypress, it waits for that particular key to be released and then returns the corresponding keycode.
wait_for_button_press() - waits until a key is pressed. Upon detecting a keypress, it waits for that particular key to be released. Then it returns the corresponding keycode.
Step 20: Motor Library Commands
init_motors() - this function sets the motor driver control pins to output mode. Speed control is achieved by Pulse Width Modulation (PWM) technique. This function should be called once before using the function given below.
set_motors() - sets the speed and direction of the two motors. Speed is any value between +250 and -250. A negative value indicates that the direction of rotation is backwards.
set_motors(250, 250) – move forward at full speed
set_motors(0, 0) – stop the motors
set_motors(-125, -125) – move backward at half the full speed
Step 21: LCD Library Commands
init_lcd() - sets lcd data and control pins to outpout mode and initializes the LCD. This function should be called once prior to using any other function in this library.
print_string() - prints a text at the specified location.
print_string(2, 2, "Hello World") displays "Hello World" (without the double quotes) starting from the 2nd position of the 2nd row of the LCD.
print_integer() - prints an integer of specified number of digits at the specified location.
print_integer(2, 1, number, 3) displays the value present in 'number' starting from the 1st position of 2nd row. The value can have a maximum of 3 digits.
clear_screen() - clears the display.
Step 22: Sensor Library Commands
init_sensors() - initializes 10-bit ADC of Atmega32 and also sets the IR emitter control pin in output mode. This function should be called before using any other function in this library.
emitters_on() - turns the IR emitters ON.
emitters_off() - turns the IR emitters OFF.
get_sensors_binary() - reads the state of all 8 sensors and return the value as a byte.
count_binary() - takes a byte as argument and returns the number of bits that are set to 1. This is used along with get_sensors_binary() to find out the number of sensors that are over the line.
get_front_sensor() - reads the status of the 9th sensor. This function returns a '1' if the sensor is over the line, otherwise it returns a '0'.
The function calibrate_sensors() will be discussed separately. The remaining functions are not used in the main program. I will leave it to you to find out what their purpose is.
Step 23: Algorithm...that's what matters!
• at any intersection, if there exists a forward path, then the robot should take that path and if it doesn't exist, then the robot is free to choose any path.
• if there is any discontinuity in the line, then the robot has to move forward until it detects the line.
• there will not be any dead ends in the track.
Given below are three steps which is repeatedly executed when the robot moves over the line.
The sensor readings are taken and stored in a byte with each bit representing the status of one particular sensor. If a bit is 1, then the corresponding sensor is over the line and if the bit is 0, then it is not on the line. The following examples will make it clear.
If the value of the byte is 00011000, only the two sensors in the middle are over the line and if the value of the byte is 10000000, only the left most sensor is over the line.
A count of the number of sensors that are over the line is taken. The count may be any value between 0 and 8.
For the count obtained, the sensor reading is compared with a set of values. If a compare match is found, then the speed and direction of both motors are adjusted accordingly.
Given below are some of the values with which the sensor reading is compared. Given next to each value is the action that is to be taken.
0000000 - no line detected. Continue previous action.
10000000 - turn sharply to the left.
00000001 - turn sharply to the right.
11000000 - turn left
01100000 - turn left
00110000 - turn left
00011000 - move straight forward at full speed
00001100 - turn right
00000110 - turn right
00000011 - turn right
11100000 - turn left
01110000 - turn left
00111000 - turn left
00011100 - turn right
00001110 - turn right
00000111 - turn right
10011000 - acute angle turn towards left
00011001 - acute angle turn towards right
11110000 - 90 degree turn towards left
00001111 - 90 degree turn towards right
11001100 or 11011000 - acute angle turn towards left
00110011 or 00011011 - acute angle turn towards right
11111000 - 90 degree turn towards left
00011111 - 90 degree turn towards right
11011100 - acute angle turn towards left
00111011 - acute angle turn towards right
11111100 - 90 degree turn towards left
00111111 - 90 degree turn towards right
11111110 or 01111111 or 11111111 - turn either left or right
In the next step, we will see how the robot reacts when it detects an intersection or a sharp turn.
Step 24: Line Detection and Response
Picture 2 shows six different sections that are tough to handle. The next six steps perform a detailed analysis on how the robot detects each of these six sections and how it reacts to it.
Picture 3 is a flowchart that depicts the series of events that takes place when a turn is detected.
Step 25: Sharp 90 Degree Turn
Step 26: Acute Angle Turn
Step 27: 90 Degree Intersection
Step 28: T- Shaped Section
Step 29: Discontinuity in Line
Step 30: Acute Angle Intersection
Step 31: Let's Upload The Code
To compile the code, open the main.c file in Notepad++ Portable. Select Macro and click on winAVR Compile. Alternately, you can type Ctrl+R to compile. After compiling the code, the console window will be as shown above.
Now to upload the code, go to Macro and select winAVR Program (or type Ctrl+U). Observe the console window.
Now that the program is uploaded, the next step is to calibrate the line sensors.
Step 32: Line Sensor Calibration
For the robot to detect the line accurately, its sensors should be calibrated. The method that I have used is the median-filter technique. It is explained in detail below. The calibration code can be obtained from the calibrate_sensors() function under the header file <sensors.h>.
The array of eight sensors is placed over the bright surface (here it’s the white line). When the calibration command is received, a set of 100 readings is taken. For each sensor, the median of their 100 readings represents its 'max value'. Next, the sensor array is placed over the dark surface (here it’s the black background) and the same procedure is repeated. This time, the median represents the 'min value'. The threshold value of each sensor is determined from its ‘max value’ and ‘min value’ using the formula given below.
threshold = min value + (max value - min value) /2
If the value read by a sensor is greater than its threshold value, then that sensor is over the bright surface, otherwise it is over the dark surface.
The minimum, maximum and threshold values of each sensor are then written to the EEPROM memory of Atmega32 so that calibration values can be retrieved every time when the robot is turned on.
Note: The ninth sensor cannot be calibrated by software as it is connected to an input pin of Atmega32. The output from the ninth sensor is either a logic 1 or a logic 0 and it depends on the value of the resistor used along with its detector. I have picked a 10K resistor for this. But I recommend that you use a potentiometer instead.
Step 33: Testing the Line Sensors
Step 34: Optimizing the Code
Step 35: A Few Notes...
If the color of the line is dark (usually black) when compared to that of the background (usually white), you will have to edit the <sensors.h> library as shown in the picture above.
There are two parameters that define the speed at which the robot moves. MAXSPEED determines the maximum speed of the robot on a scale from 0 to 250. You can change this value to set the desired speed of the robot. ROTATE_SPEED determines the speed with which the robot takes a turn. These two parameters can be adjusted in the main.c file.
That's it. After 35 steps, this instructable is complete. Now all you got to do is build one on your own and explore the tracks. Good Luck!