Advanced Line Following Robot
Advanced Line Following Robot
by midhun_s
This is an advanced line following robot based on Teensy 3.6 and QTRX line sensor that I have built and have been
working on for quite some time. There are some major improvements in the design and performance from my
earlier line following robot. The speed and response of the robot has improved. The overall structure is compact
and lightweight. The components are arranged close to the wheel axis so as to minimize angular momentum. High
power micro metal gear motors provide the adequate torque and aluminium hub silicone wheels o er much-
needed traction at high speeds. Prop shield and wheel encoders enable the robot to determine its position and
orientation. With Teensyview mounted on board, all the relevant information can be visualized and important
program parameters can be updated using pushbuttons.
To begin building this robot, you will need the following supplies (and a lot of time and patience at your disposal).
Supplies:
Electronics
Hardware
Tools
Multimeter
Soldering iron
Solder wire
Wire stripper
Wire cutter
https://www.youtube.com/watch?v=qrzdEw3NMq0
Line sensor : Pololu's QTRX-MD-16A 16-channel Power supply : 3.7V, 750mAh lipo battery as power
analog output line sensor array in medium-density source. 3.3V step-up/down regulator powers
arrangement (8mm sensor pitch). microcontroller, sensors, and display device.
Adjustable step-up regulator powers the two motors.
Drive : 6V, 1580rpm, high power micro metal gear
motors with magnetic wheel encoder and silicone User interface : Teensyview for displaying information.
wheels tted on aluminium hubs. Three-pushbutton breakout for accepting user inputs.
Two numbers of 10mm diameter green LEDs for
Odometry : Magnetic wheel encoder pairs for status indication while running.
estimating the coordinates and distance covered.
We will be implementing the above circuit on the perfboard. We have to rst keep our breakout boards ready by
soldering headers on them. The video will provide an idea regarding which headers should be soldered on which
breakout boards.
After soldering headers on breakout boards, stack the Teensyview and pushbutton breakout on top of Teensy.
https://www.youtube.com/watch?v=0vdxtk5EIHE
Get the 15x20cm double side prototype perfboard We will begin our prototyping by soldering the
and mark the boundary with a permanent marker as header pins and sockets on the perfboard. The
shown in the picture. Drill M2 size holes for mounting breakout boards will be later inserted on these
the sensor array, caster wheel, and micro metal gear headers. Give careful attention to the position of the
motors on locations marked with a white circle. We headers on the perfboard. We will be connecting all
will later cut the perfboard along the boundary after wires based on this layout of headers.
soldering and testing all components.
9 6
4 5
3
7 13 4
3 12
2 5
8 2
6
10 1
11 1
1. Prop shield with motion sensors 1. Female headers for prop shield
2. Teensy 3.6 2. Female headers for Teensy 3.6
3. Teensyview 3. Female headers for line sensor
4. 3.3V step-up/down voltage regulator 4. Female headers for voltage regulators
5. Adjustable step-up voltage regulator 5. Male headers for motor driver
6. QTRX-MD-16A line sensor 6. Male headers for battery
7. DRV8833 motor driver carrier
8. Switch
9. 24 pin FFC to DIP breakout
10. Micro metal gear motor
11. 10mm Green LED
12. Pushbutton breakout
13. Electrolytic capacitor
We will rst solder the connections to the prop shield. Since we are using only the motion sensors of the prop
shield, we need to connect only SCL, SDA and IRQ pins apart from the 3V and ground pins of prop shield.
Once the connection is complete, insert Teensy and prop shield and calibrate the motion sensors by following the
steps mentioned here.
Solder all the power and ground connections referring to the picture. Insert all breakout boards in place and
ensure continuity using a multimeter. Verify the di erent voltage levels on board.
The DRV8833 dual motor driver carrier board can deliver 1.2A continuous and 2A peak currents per channel. We will
connect the two channels in parallel to drive one motor. Solder the connections by following the steps below.
Parallel the two inputs and the two outputs of the motor driver carrier as shown in picture.
Connect input control wires to the motor driver.
Connect a 1000uF electrolytic capacitor and a 0.1uF ceramic capacitor across the Vin and Gnd
terminals of the two carrier boards.
Connect a 0.1uF ceramic capacitor across motor driver output terminals.
1. Electrolytic capacitor
Teensy 3.6 has two ADCs - ADC0 and ADC1 that are multiplexed to 25 accessible pins. We can access any two pins
from the two ADCs at the same time. We will connect eight line sensors each to ADC0 and ADC1. The even number
sensors will be connected to ADC1 and odd number sensors to ADC0. Solder the connections by following the steps
below. We will later connect the line sensor using FFC to DIP adapter and cable.
Connect all even sensor pins (16,14,12,10,8,6,4,2) as shown in picture. Route the wire for connecting
sensor pin 12 through the reverse side of perfboard.
Connect emitter control pin (EVEN) to Teensy pin 30.
Connect all odd sensor pins (15,13,11,9,7,5,3,1) as shown in picture.
Connect a 470uF electrolytic capacitor across Vcc and Gnd.
If you closely observe the line sensor pins and their corresponding header pins on the perboard, you will notice
that the top row of the line sensor maps to the bottom row of the header on the perboard and vice versa. This is
because when we connect the line sensor to the perfboard using dual row right-angled headers, the rows will align
correctly. It took me quite some time to gure this out and correct the pin assignments in the program.
1
1 3
1. QTRX-MD-16A pins
Fix the micro metal gear motor with encoder using N20 motor mounts.
Connect the motor and encoder wires as shown in the picture.
Left encoder - Teensy pins 4 & 0
Right encoder - Teensy pins 9 & 27
The two LEDs indicate whether the robot has detected a turn or not. I have used a 470-ohm series resistor to
connect the LEDs to Teensy.
Insert all the breakout boards in their respective sockets. For inserting the FFC-DIP breakout and for xing the
QTRX-MD-16A line sensor, refer to the video.
https://www.youtube.com/watch?v=kw5KtEXQipE
We will program the Teensy in Arduino IDE. We will need some libraries before we begin. The libraries that we will
use are:
Encoder
Teensyview
EEPROM
ADC
NXPMotionSense
And some that have been written speci cally for this robot,
PushButton
LineSensor
TeensyviewMenu
Motors
The libraries speci c to this robot are discussed in detail and are available for download in the next steps.
This library is for interfacing the pushbutton breakout board with the Teensy. The functions used are
Calling this constructor by creating an object con gures the pushbutton pins to INPUT_PULLUP mode.
int8_t waitForButtonPress(void);
This function waits until a button is pressed and released and returns the key code.
int8_t getSingleButtonPress(void);
This function checks if a button is pressed and released. If yes, returns the key code else returns zero.
Download
https://www.instructables.com/ORIG/FY3/OI4S/KF2JEJX6/FY3OI4SKF2JEJX6.cpp
…
Download
https://www.instructables.com/ORIG/FMQ/S725/KF2JEJX7/FMQS725KF2JEJX7.h
…
LineSensor is the library for interfacing the line sensor array with Teensy. The following are the functions used.
LineSensor(void);
Calling this constructor by creating an object initializes ADC0 and ADC1, reads threshold, minimum and maximum
values from EEPROM and con gures the sensor pins to input mode and emitter control pin to output mode.
Reads sensor array in any of the three modes passed as argument. The mode is the state of the emitters and can be
ON, OFF or TOGGLE. TOGGLE mode compensates the sensor readings of re ectance due to ambient light. The
sensors connected to ADC0 and ADC1 are read synchronously.
Calculates the position of the sensor array over the line by the weighted average method.
Returns a 16-bit representation of the state of the sensors. A binary one indicates that the sensor is over the line
and a binary zero indicates that the sensor is o the line.
Passing the 16-bit representation of sensor values to this function returns the number of sensors that are over the
line.
Reads the sensor values and constrains each sensor value to its corresponding min and max values. The sensor
values are then mapped from their corresponding min to max range to 0 to 1000 range.
Download
https://www.instructables.com/ORIG/FOY/8KAV/KF2JEJX2/FOY8KAVKF2JEJX2.cpp
…
Download
https://www.instructables.com/ORIG/FOK/SOJ7/KF2JEJX3/FOKSOJ7KF2JEJX3.h
…
TeensyviewMenu is the library where the functions for the display menu can be accessed. The following are the
functions used.
TeensyViewMenu(void);
Calling this constructor creates an object of class LineSensor, PushButton and TeensyView.
void intro(void);
This is called internally within the menu when the line sensor values are to be displayed on Teensyview for testing.
Download
https://www.instructables.com/ORIG/FS1/O6H0/KF2JEJX8/FS1O6H0KF2JEJX8.cpp
…
Download
https://www.instructables.com/ORIG/F1Q/H6DF/KF2JEJX9/F1QH6DFKF2JEJX9.h
…
Motors is the library used for driving the two motors. The following are the functions used.
Motors(void);
Calling this constructor by creating an object con gures the motor direction control and PWM control pins to
output mode.
Calling this function drives the two motors at speeds passed as arguments. The value of speed can range from -255
to +255 with a negative sign indicating that the direction of rotation is reversed.
Download
https://www.instructables.com/ORIG/FSN/QS95/KF2JEJX4/FSNQS95KF2JEJX4.cpp
…
Download
https://www.instructables.com/ORIG/FZU/C4X4/KF2JEJX5/FZUC4X4KF2JEJX5.h
…
We will test the magnetic wheel encoders and display displays the position of the robot in terms of x-y
the position and distance covered by the robot. coordinates, displays the total distance covered in
centimeter and the angle turned in degrees.
Upload the DualEncoderTeensyview.ino. The program
displays the encoder ticks on Teensyview. The encoder I have referred the Implementing Dead Reckoning by
ticks increment if you move the robot forward and Odometry on a Robot with R/C Servo Di erential
decrement if you move it backward. Drive by Seattle Robotics Society for determining
position from encoder ticks.
Now upload the EncoderOdometry.ino. This program
Download
https://www.instructables.com/ORIG/F2Q/W2DD/KF13YIRB/F2QW2DDKF13YIRB.ino
…
Download
https://www.instructables.com/ORIG/F2I/LCG7/KF13YKUD/F2ILCG7KF13YKUD.ino
…
Make sure you have calibrated the motion sensors by following the steps mentioned here.
Now upload the PropShieldTeensyView.ino. You should be able to see the accelerometer, gyro and magnetometer
values of all three axes on the Teensyview.
Download
https://www.instructables.com/ORIG/FP3/T4HR/KF140VZC/FP3T4HRKF140VZC.ino
…
The program for the advanced line follower is written in Arduino IDE. The program works in the following sequence
explained below.
The program at present does not measure orientation values from prop shield. This is a work in progress and is
being updated.
Upload TestRun20.ino. We will see how to navigate the menu, adjust settings and how to calibrate the line sensors
in the next steps following which we will test our robot.
Download
https://www.instructables.com/ORIG/FYV/R80R/KF2JEJV2/FYVR80RKF2JEJV2.ino
…
The menu has the following settings which can be navigated using the left and right pushbuttons and selected
using the center pushbutton. The settings and their functions are described below.
https://www.youtube.com/watch?v=TsayV45mARk
Line sensor calibration is the process by which the threshold value of each of the 16 sensors is determined. This
threshold value is used to decide whether a particular sensor is over the line or not. To determine the threshold
values of 16 sensors, we use either of the two methods.
MEDIAN FILTER: In this method, the line sensors are placed above the white surface and a pre-de ned number of
sensor readings are taken for all 16 sensors. The median values of all 16 sensors are determined. The same process is
repeated after placing the line sensors over the black surface. The threshold value is the average of median values
of black and white surfaces.
MIN MAX: In this method, the sensor values are read repeatedly until the user prompts for a stop. The maximum
and minimum values encountered by each sensor are stored. The threshold value is the average of minimum and
maximum values.
The calibration of line sensors by the MIN MAX method is shown in the video. After calibrating the line sensors, the
data can be visualized as shown in the picture. The following information is displayed.
A 16-bit binary representation of line position with a binary 1 indicating that the corresponding line
Advanced Line Following Robot: Page 15
sensor is over the line and a binary 0 indicating that the line sensor is o the line.
A count of the total number of sensors that are over the line.
Minimum, maximum and sensor values (raw and normalized) of the 16 sensors, one sensor at a time.
Line position in the range -7500 to +7500.
The minimum and maximum line sensor values are then stored in EEPROM.
https://www.youtube.com/watch?v=w7dpLYpsPn4
1 2
3 4 5 6
7 8
The video is of a test run in which the robot is programmed to stop after it completes one lap.
https://www.youtube.com/watch?v=04sliTk3wYA