E-Racer Spring 2019

Line Following using a QRE1113 IR Sensor

Author: Lundao Gao


This blogpost is intended for keeping a record on IR Sensor Testing and Coding with Sparkfun QRE1113 Digital Breakout Board in the process of E-Racer Robot Project in EE-400D Class at CSULB.

Requirements need to be met are:

L1.28 All 3DoT robots shall incorporate the 3DoT v8 or v9 series of boards.

L1.29 Software shall be written in the Arduino De facto Standard scripting language and/or using the GCC C++ programming language, which is implements the ISO C++ standard (ISO/IEC 14882:1998) published in 1998, and the 2011 and 2014 revisions. Required exceptions to this standard can be found here.

L1.1  The E-Racer will drive on horizontal surfaces made of linoleum(classroom floor).

L1.3 The E-Racer shall be able to follow a course drawn on the 12’x5’ whiteboard in a classroom.

L1.7 The E-Racer shall begin moving forward anywhere within the defined start-finish area on the whiteboard.

L2.4.1 The PCB shall contain the IR sensors used for line following.

L2.6.1 The E-Racer shall implement PID control to autonomously follow the line.

IR Sensor Testing

The reasons for choosing QRE1113 are the following:

In EE444 class at CSULB, students used four(4) Sparkfun QRE1113 Digital Breakout Board for line following purpose and performed well. Since the project manager had experience with this sensor, and in order to follow Professor Gary Hill’s instruction “Don’t reinvent the wheels,” the project team decided to use Sparkfun QRE1113 Digital Breakout Board for initial testing and later applying QRE1113 sensor to the IR Sensor Shield Design.

Following paragraph is quoted from Sparkfun’s website, explaining how Sparkfun QRE1113 Digital Breakout Board works:

“This version of the QRE1113 breakout board features a digital output, using a capacitor discharge circuit to measure the amount of reflection. This tiny board is perfect for line sensing when only digital I/O is available and can be used in both 3.3V and 5V systems. The board’s QRE1113 IR Reflectance Sensor is comprised of two parts – an IR emitting LED and an IR sensitive phototransistor. When you apply power to the VCC and GND pins the IR LED inside the sensor will illuminate. A 100Ω resistor is on-board and placed in series with the LED to limit current. The output of the phototransistor is tied to a 10nF capacitor. The faster that capacitor discharges, the more reflective the surface is. These sensors are widely used in line following robots. White surfaces reflect more light than black, so, when directed towards a white surface, the capacitor will discharge faster than it would when pointed towards a black surface.” [1]

There is another version of the Sparkfun QRE1113 Breakout Board, which is the analog version. The difference between the Digital version and the Analog version is that digital version give output according to the time for the capacitor to discharge, and the analog version has a 10kΩ resistor that pulls the output pin high, as the light from the LED is reflected back onto the phototransistor, the output which is voltage measurement will begin to go lower.

Testing Sensors

The first task once one has access to a sensor breakout board is to test if they work and learn how to read data from them.

The analog version uses very simple codes to get results:

Figure 1.1 Code for testing Sparkfun QRE1113 Analog Breakout Board [2]

Figure 1.1 Code for testing Sparkfun QRE1113 Analog Breakout Board [2]

while the digital version uses more complicated codes:

Figure 1.2 Code for testing Sparkfun QRE1113 Digital Breakout Board[2]

Figure 1.2 Code for testing Sparkfun QRE1113 Digital Breakout Board[2]

Since the breakout boards that the project team decided to use are digital versions, the following tasks are performed with the digital version. Shown below are the codes for initial testing:

Figure 2.1 Arduino Code for IR Sensor testing

Figure 2.1 Arduino Code for IR Sensor testing

In the initial testing, one breakout board was connected on Arduino, and as it was moving across a black line that was drawn on the whiteboard, the board outputted numbers like these:

Figure 2.2 Reading from IR Sensor Testing

Figure 2.2 Reading from IR Sensor Testing

The numbers in the window indicate how much reflection the sensor is detecting. The larger the number, the darker the surface is.

After several testing, a conclusion is derived that the threshold of detecting a black line is varied by factors like lighting conditionshadows, and surface material.

The threshold is very important and will be used later when designing codes for line following to determine whether sensors are detecting line and if the robot needs to make adjustments.

IR Sensors Code

After proving that the breakout boards work, the next step is to implement them into a robot to make it completes line-following tasks.

The robot used for IR sensor code testing is from EE444, it contains two motors, two encoders(not needed), four IR breakout boards, one microcontroller board, and one motor driver.

Note that, it is needed to pay attention to the choice of microcontroller board for testing before having access to a 3Dot board. Commonly used boards like Arduino Nano have ATmega328 microcontroller chip. However, when designing a robot that will eventually use a 3Dot board,  a board with ATmega32U4 microcontroller chip is needed, for example, Sparkfun Pro Micro is a good choice, as it is small in size and it uses ATmega32U4 microcontroller chip. In this project, Sparkfun Pro Micro is used for code testing. The connection of all parts is shown in Figure 3:

Figure 3 Fritzing Diagram for Test Robot Connections

Figure 3 Fritzing Diagram for Test Robot Connections

Code created for line following tasks:

Figure 4 Code for Test Robot Line Following with Updated Control Loop

Figure 4 Code for Test Robot Line Following with Updated Control Loop

These codes contain PID calculation, sensor reading, PWM writing for motors, and all the necessary setups.

The code “PIDright” and “PIDleft” are code needed to tune PID program to give better preference in following the line, these parameters have relations to how PID method works, which was explained in EE370, EE470, and EE471 class in CSULB. To find out the best parameters, we had to test their performances in reality. Theoretically, a higher P will give a faster and more precise reaction, and I term is to limit the speed of the steering response and reduce oscillation, and D term is to add more steering action in case of the error persists for too long.PID tuning is very important to make the robot work after determining the set point for white and black.

Figure 5.2 shows the flowchart explaining how the code works.

Figure 5 Flowchart for Line Following Control Loop

Figure 5 Flowchart for Line Following Control Loop

Here is a video of the robot doing line following using code designed above.


In this blogpost, QRE1113 Digital Breakout Board is used to test the performance of PID and line following code.

All requirements are met during the testing process, and the robot successfully performed line following task following an 8 figure on a whiteboard.


[1]”SparkFun Line Sensor Breakout – QRE1113 (Digital) – ROB-09454 – SparkFun Electronics”, Sparkfun.com, 2019. [Online]. Available: https://www.sparkfun.com/products/9454

[2]”bildr » Line Sensing. QRE1113 Reflectance Sensor + Arduino”, bildr, 2019. [Online]. Available: http://bildr.org/2011/06/qre1113-arduino/

[3]”3Dot V7.0 – Arxterra”, Arxterra.com, 2019. [Online]. Available: https://www.arxterra.com/3dotproducts/3dot-v7/?v=7516fd43adaa#tab-id-3