Tutor ListResources Electronics Resources

Design Of A Real-time Embedded System In A Line-following Robot

Date : 31/8/2016

Author Information


Uploaded by : Manish
Uploaded on : 31/8/2016
Subject : Electronics


An embedded system consisting of sensor inputs, LEDs, DC motors and a microcontroller is being designed for a self-operating line following robot which conforms to Birmingham TechFest rules. The line following robot must be self-guided and run on a racetrack which consists of a black background and a white line in the centre. In the robot’s embedded system, the sensors detect the color of the track using light reflected from accompanying LEDs (whether it is black or white) and therefore determine which direction to turn on the track. DC motors operated by PWM (pulse-width modulation) signals drive the robot forward on the track. The velocity of the motor depends on the voltage applied to the DC motor. A push button is used to start or calibrate the microcontroller, another push button is used to hard reset it.

DC Motors

Pulse-Width Modulation

Pulse-Width Modulation (PWM) is a technique used to control the power in an electrical device. In the DC motor, energy is delivered to the motor by switching the motor on and off rapidly (therefore creating a succession of pulses). This is controlled by the microcontroller in the robot and its associated program. The time taken for the motor to switch on and off, or the time taken for one single pulse is a period. The number of pulses in one second is the pulse frequency, measured in hertz (Hz). The duty cycle describes the time in which the pulse of the motor is high (switched on). It is measured as a percentage of the full period of the DC motor. The pulse width is the average voltage applied multiplied by the duty cycle.

Duty Cycle = (Time On/Time On + Time Off) x 100

Pulse Width = Duty Cycle × Voltage Applied

Below are PWM waveform showing duty cycle and pulse width for a DC motor operating at a maximum voltage of 3V. When the duty cycle is 25%, as a result of the pulses, the average voltage of the DC motor is 25% of 3V = 0.75V

In the case of our DC motor, the PWM waveforms can be produced using either software polling or time delay interrupts. In software polling, the DC motor is programmed to turn on for the duty cycle being used and turn off in a continuous loop via the microcontroller. However this method is inefficient as it only allows the microcontroller to perform only this task and no other tasks. In the time delay interrupts method, the hardware detects external events occurring before producing interrupts. Timer interrupts are generated accordingly to the time the motor is switched on and the motor is switched off. The interrupts alternate rapidly to produce pulses.

For example, in the microcontroller is a CPU which has a clock frequency of  11.0592 kHz. Each instruction cycle requires 4 CPU cycles to execute, therefore the timer uses 4 CPU cycles and the clock frequency is 2.764 kHz. For the time on and off, the clock frequency is 1.382kHz which is a rate of 723.4ns. For a 100µs interrupt rate, the timer constant, Tconstant shall be 100000/723.4 = 138ns. If the Duty Cycle, D is set at 40%, then the time on, Ton = D × Tconstant = 40 × 138 = 5520ns. Time off, nbsp  Toff = (100 – D) x Tconstant = (100 – 40) × 138 = 8280ns.

Thus a PWM method for the DC motor is established.

The chopper motor

The motor used in the DC motor is a RF500TB-14415 motor which is a chopping motor consisting of a high current IRF540 MOSFET which acts as a transistor switch, a flyback diode and the rotating motor.

The MOSFET accepts pulse width signals from the microcontroller when the timer is set to on (during the interrupt routine) and a voltage is applied to the gate channel of the MOSFET. This reduces the resistance of the drain-source channel of the MOSFET and allows current to flow. When the timer is set to off, no voltage is applied to the gate, the drain-source channel has high resistance and no current flows. To prevent damage to the MOSFET while switching, a flyback diode is used. The motor’s inductance produces high voltage when the switch is off (therefore “flybacking”), so the diode directs the flow of the current back to the inductor preventing any damage on the MOSFET switch.

The RF500TB-14415 motor operates at a range of 1.5V to 9V. At maximum efficiency, the current used is 0.12A and the motor’s wheel rotates at 2540rpm. The diameter of the wheel is 50mm. Therefore radius, r = 25mm.

The maximum velocity, V = ωr = ((2π/60) × 2540 × (25/1000)) = 6.649m/s

The torque, τ of the motor is 1.23 mN.m = 0.00123 N.m

The mass of the motor, m = 300g = 0.3kg

Therefore, maximum acceleration, a = τrmsinѲ  = ((1.23/1000)/((25/1000) × (300/1000) × sin 30)) = 0.328 m/s2


The line-following robot uses optical sensors. A basic optical sensor can consist of an emitter (LED) and a detector (phototransistor). In the line-following robot, a TEPT4400 phototransistor and a L-934 LED is used. The LED consists of doped Gallium Arsenide Phosphide which contain holes. When the LED is in forward bias, electrons move through the holes and energy is released in the form of light of various wavelengths. The TEPT4400 phototransistor contains a doped silicon NPN bipolar transistor with an exposed base also containing holes. Light strikes the base causing electrons to move through the holes and into the emitter. The TEPT4400 phototransistor can detect light signals of wavelength of up to 560nm and the L-934 LED can emit light of up to 660nm. To prevent the effects of ambient lighting, the optical sensor has to have high intensity emission and low sensitivity reception. This is achieved by adding a load resistor for the phototransistor (47kΩ) and a current limiting resistor for the LED (180Ω). 

The microcontroller of the line-following robot consists of an analogue to digital converter (ADC). The light reflected off the racetrack from the LED is detected by the phototransistor which increases the voltage at the emitter. The increase in voltage is detected by the microcontroller which is sent to the ADC. Depending on the voltage, the ADC converts this to a PWM signal which causes the motors to run at a certain speed in a straight line.

Since the line following robot has 2 DC motors, 2 optical sensors are used. The 2 sensors detect the white line on the race track. As long as it detects the white line, the DC motors will run in a straight line. If the track curves then the voltage at one of the phototransistors will be higher than the other, therefore its corresponding motor will run at a higher speed and the other motor will run at a lower speed, allowing the robot to move along the curve accordingly.

nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp Microcontroller

The line following robot contains a PIC18F2455 microcontroller. This microcontroller is appropriate for the line following robot for the reasons below:

1. nbsp  It is relatively cheap (only costs £2.50 per unit)

2. nbsp  The crystal frequency operating at HS oscillating mode is close to the clock frequency that is required for the pulse width modulation to occur.

3. nbsp  It contains 28 pins which is enough to control the operations of the 2 optical sensors, 2 DC motors and 2 push buttons.

4. nbsp  The microcontroller is compatible with the MPLAB software being used to program the line following robot and requires simple instructions coded in C to perform the given tasks.                 

Building and Testing

In order to test if the DC motors and the sensors are functioning properly and as expected, they are tested on a prototype breadboard with the microcontroller fitted to it. The microcontroller is connected to a computer with the MPLAB software running.

DC Motors

1. nbsp  The chopping motor is connected to the breadboard with the gate connected to the microcontroller’s RC0 input.

2. nbsp  The Duty Cycle is set to 1%, time constant = 138. Therefore, time on Ton = (Duty Cycle x time constant) = 138 x 1 = 138

3. nbsp  The C code for the DC motor is compiled and the program is executed.

4. nbsp  It is observed that the wheel of the motor rotates extremely slowly as though it barely moves.

5. nbsp  The Duty Cycle is set to 10% and the code is compiled and executed again.

6. nbsp  There is a noticeable increase in the speed of the wheel’s rotation.

7. nbsp  The above procedure is repeated with higher duty cycles like 25%, 50%, 80% and so on.

8. nbsp  As the duty cycle is increased the speed of the wheel’s rotation. At 100% it reaches highest speed.

9. nbsp  The following waveforms show the pulse width modulation for the duty cycles used.



1. nbsp  The phototransistor and LED is wired to the breadboard with 7 segment display LEDs fitted onto it to give a reading from the phototransistor. The phototransistor is connected to the AN4 port of the microcontroller.

2. nbsp  The C code for the sensor to operate is compiled and the program is executed.

3. nbsp  The value on the 7 segment display LED is recorded.

4. nbsp  A small black and white strip from the racetrack is placed in front of the LED/phototransistor at position -5.

5. nbsp  The reading on the 7 segment display is recorded.

6. nbsp  The above procedure is repeated with the black and white strip shifted rightwards until position +5 and for each position the reading on the 7 segment display is recorded.

7. nbsp  The above procedure is again repeated, this time the black and white strip is shifted behind a few cm and all the readings are taken again.

This resource was uploaded by: Manish