MEASUREMENT LAB
FACULTY OF ELECTRICAL
& ELECTRONICS ENGINEERING
ROBOTICS LAB
For internal use only
TON DUC THANG UNIVERSITY - 2024
Robotics Lab Updated 18/11/2024
CONTENTS
Lab 1. INTRODUCTION OF QBOT..............................................................................1
1.1 Arduino installation.....................................................................................................1
1.2 Overview.....................................................................................................................2
1.3 Control board...............................................................................................................3
Lab 2. MOTORS, ENCODERS, AND DRIVES.............................................................6
2.1 Motors and driver........................................................................................................6
2.1.1 Motors..................................................................................................................6
2.1.2 Driver...................................................................................................................8
Add the wiring diagram for the motor connection.................................................................9
2.2 Sensors.........................................................................................................................9
2.2.1 Line detection sensors..........................................................................................9
2.2.2 Distance sensors.................................................................................................10
2.2.3 LCD....................................................................................................................11
2.2.4 Button.................................................................................................................12
2.3 Encoder......................................................................................................................13
2.4 Exercise.....................................................................................................................14
2.4.1 Exercise 1...........................................................................................................14
2.4.2 Exercise 2...........................................................................................................15
2.4.3 Exercise 3...........................................................................................................15
2.4.4 Exercise 4...........................................................................................................15
2.4.5 Exercise 5...........................................................................................................15
Lab 3. OBSTACLE AVOIDANCE................................................................................16
3.1 Objectives..................................................................................................................16
3.2 Motors control...........................................................................................................16
3.3 Distance sensors........................................................................................................17
3.4 Control algorithm......................................................................................................18
3.5 Exercies.....................................................................................................................20
3.5.1 Exercise 1...........................................................................................................20
3.5.2 Exercise 2...........................................................................................................20
3.5.3 Exercise 3...........................................................................................................20
3.5.4 Exercise 4...........................................................................................................20
3.5.5 Exercise 5...........................................................................................................20
TON DUC THANG UNIVERSITY
Robotics Lab Updated 18/11/2024
3.5.6 Exercise 6...........................................................................................................20
3.5.7 Exercise 7...........................................................................................................21
Lab 4. LINE FOLLOWING...........................................................................................22
4.1 Objectives..................................................................................................................22
4.2 Line sensors...............................................................................................................22
4.3 Control algorithm......................................................................................................22
4.4 Exercises....................................................................................................................23
4.4.1 Exercise 1...........................................................................................................23
4.4.2 Exercise 2...........................................................................................................23
4.4.3 Exercise 3...........................................................................................................23
4.4.4 Exercise 4...........................................................................................................23
4.4.5 Exercise 5...........................................................................................................24
TON DUC THANG UNIVERSITY
Robotics Lab Updated 18/11/2024
LIST OF FIGURES
Figure 1-1 Download and install arduino IDE...........................................................................1
Figure 1-2 ESP32 boards installation.........................................................................................1
Figure 1-3 ESP32 boards installation.........................................................................................2
Figure 1-4 QBOT v2..................................................................................................................2
Figure 1-5 QBOT overview.......................................................................................................3
Figure 1-6 DOIT ESP32 DEVKIT V1.......................................................................................4
Figure 1-7 Select ESP32 board..................................................................................................4
Figure 1-8 Select COM port.......................................................................................................5
Figure 1-9 DC gear motor..........................................................................................................6
Figure 1-10 PWM method.........................................................................................................7
Figure 1-11 Correlation between wheels speed and robot direction..........................................8
Figure 1-12 L298N motor driver................................................................................................9
Figure 1-13 Light reflection of different contrasting surfaces.................................................10
Figure 1-14 Using a set of line sensors....................................................................................10
Figure 1-15 TCRT5000 IR sensor............................................................................................10
Figure 1-16 Ultrasonic sensor principle...................................................................................11
Figure 1-17 HC-SR04 ultrasonic sensor..................................................................................11
Figure 1-20 LCD 2004.............................................................................................................12
Figure 1-18 Buttons set............................................................................................................12
Figure 1-19 Analog button schematic......................................................................................13
Figure 1-21 Encoder structure..................................................................................................14
Figure 1-22 Encoder module....................................................................................................14
Figure 2-1 Obstacle avoidance algorithm illustration..............................................................18
Figure 2-2 Simple obstacle avoidance control algorithm........................................................19
Figure 3-1 Line following algorithm illustration.....................................................................23
Figure 3-2 Basic line following control algorithm...................................................................24
TON DUC THANG UNIVERSITY
Robotics Lab Updated 18/11/2024
LIST OF TABLE
Table 1-1 DOIT ESP32 DEVKIT V1 specifications.................................................................4
Table 1-2 DC gear motor specifications....................................................................................6
Table 1-3 L298N motor driver specifications............................................................................9
Table 1-4 Ultrasonic sensor specifications..............................................................................11
Table 1-5 LCD 2004 specifications.........................................................................................12
Table 1-6 Encoder V2 specifications.......................................................................................14
TON DUC THANG UNIVERSITY
Robotics Lab Updated 18/11/2024
Lab 1. INTRODUCTION OF QBOT
1.1 Arduino installation
Goto [Link] to download and install Arduino IDE.
Figure 1-1 Download and install arduino IDE
In your Arduino IDE, go to File > Preferences and enter the following into the “Additional
Board Manager URLs” field, then click OK:
[Link]
Figure 1-2 ESP32 boards installation
Goto Tools > Board > Boards Manager, search for ESP32 and click install button:
TON DUC THANG UNIVERSITY
Page 1 of 24
Robotics Lab Updated 18/11/2024
Figure 1-3 ESP32 boards installation
1.2 Overview
QBOT is a wheels robot, which is designed and manufactured by the Faculty of Electrical
and Electronics Engineering, TDTU.
The structure of QBOT includes:
- 03 wheels: 02 motorized wheels and
01 omnidirectional wheel.
- 01 set of 08 line sensors.
- 03 distance sensors.
- 01 buzzer.
- 01 LCD.
- 09 push buttons.
- Power switch, motors switch and
USB C plug.
- Control board.
- Motor driver board. Figure 1-4 QBOT v2
- A battery pack of 02 li-ion cells.
TON DUC THANG UNIVERSITY
Page 2 of 24
Robotics Lab Updated 18/11/2024
ESP32 DEVKIT V1 Encoders
Motors Power
switch switch
Motors
LCD IR sensors
Buttons Distance sensor
IR sensors
Figure 1-5 QBOT overview
USB C plug is used to charge the battery. The DC adapter must have a voltage of about
5V.
1.3 Control board
Arduino is open-source electronic prototyping platform enabling users to create interactive
electronic objects.
QBOT use DOIT ESP32 DEVKIT V1 as a control board.
TON DUC THANG UNIVERSITY
Page 3 of 24
Robotics Lab Updated 18/11/2024
Figure 1-6 DOIT ESP32 DEVKIT V1
Table 1-1 DOIT ESP32 DEVKIT V1 specifications
Tensilica 32-bit Single/Dual core CPU Xtensa
Processors
LX6
Operating voltage 3.3V
Input voltage (recommended) 5V
Digital I/O Pins 25
Analog input pins (ADC) 6
Analog output pins (DAC) 2
DC current for 3.3V Pin 50 mA
Flash memory 4 MB
SRAM 520 kB
Bluetooth v4.2 BR/EDR and BLE
Wifi 802.11 b/g/n
Clock speed 240 MHz
Open Arduino IDE and test the LED blinking program:
Figure 1-7 Select ESP32 board
TON DUC THANG UNIVERSITY
Page 4 of 24
Robotics Lab Updated 18/11/2024
Turn on the power switch (keep the motor switch turned off), connect the robot to
computer with a USB C cable. Go to Tool > Port > Choose the COM port (you may need to
install the CH340 driver), then upload the program to the robot.
Figure 1-8 Select COM port
Pin 2 (defined as LED) of the arduino is connected to the onboard LED, so we can control
the LED by using the following program: LED_blinking.ino
#define robot_name "Robot_1"
//#define Bluetooth_EN
//#define PULSE_COUNTER_EN
#include "QBOT2.h"
void setup()
{
QBOT_init();//QBOT initialization
}
void loop()
{
digitalWrite(LED,1); // Turn on LED
delay(500); // delay 500ms
digitalWrite(LED,0); // Turn off LED
delay(500); // delay 500ms
}
The onboard LED will blink after uploading the program to arduino.
We can make the buzzer beep by replace LED with buzzer.
#define robot_name "Robot_1"
TON DUC THANG UNIVERSITY
Page 5 of 24
Robotics Lab Updated 18/11/2024
//#define Bluetooth_EN
//#define PULSE_COUNTER_EN
#include "QBOT2.h"
void setup()
{
QBOT_init();//QBOT initialization
}
void loop()
{
digitalWrite(buzzer,1); // Turn on buzzer
delay(100); // delay 100ms
digitalWrite(buzzer,0); // Turn off buzzer
delay(2000); // delay 2000ms
}
For pin assignments, see QBOT2.h.
Note: The arduino program is case sensitive.
Lab 2. MOTORS, ENCODERS, AND DRIVES
2.1 Motors and driver
2.1.1 Motors
QBOT use 02 DC gear motors for driving 02 wheels.
Table 2-2 DC gear motor specifications
Voltage 3-6 V
Reduction ratio 1:90
Nominal current 1A
Nominal speed 120 rpm
Torque 0.15 – 0.6 N.m
Figure 2-9 DC gear motor
To control the robot forward, we let the two motors rotate forward in the same direction.
To control the robot backward, we let the two motors rotate backward in the same
direction.
TON DUC THANG UNIVERSITY
Page 6 of 24
Robotics Lab Updated 18/11/2024
To control the robot to turn left or right, let the two motors rotate in different directions or
speeds.
To control the motor speed, we use PWM method.
The PWM (Pulse Width modulation) method can changes the motor speed by adjusting the
average voltage supplied to the motor.
Avareage voltage is calculated by the following equation:
T ON
v average= ∗V
T
vaverage: Avareage voltage (V).
V: Supply voltage or peak voltage (V).
TON: High voltage duration (s).
TOFF: Low voltage duration (s).
T = TON + TOFF: Period of the voltage signal or switching cycle (s).
By adjusting the high voltage duration, we can change the average voltage supplied to the
motors.
Volt. Volt.
TOFF
V v(t) V v(t)
TO
N vaverage
vavera TO
TOFF ge N
0 t 0 t
Volt.
V v(t)
TOFF
vaverage
0 TO t
N
Figure 2-10 PWM method
TON DUC THANG UNIVERSITY
Page 7 of 24
Robotics Lab Updated 18/11/2024
Robot direction
Left Right
speed speed
Left Right
speed speed
Robot direction
Robot direction Robot direction
Left Right Left Right
speed speed speed speed
Robot direction Robot direction
Right Left
speed speed
Left Right
speed speed
Figure 2-11 Correlation between wheels speed and robot direction
2.1.2 Driver
TON DUC THANG UNIVERSITY
Page 8 of 24
Robotics Lab Updated 18/11/2024
The output current from the microcontroller pins is very small (<20mA), it is not enough to
feed the motor directly. Therefore, a power amplifier called a motor driver is required.
To control the robot motors (speed and direction), we use L298N motor driver.
L298N can control 02 motor independently. Motor A is connected to OUT1 and OUT2,
motor B is connected to OUT3 and OUT4. ENA and ENB is enable pins. When these pins are
HIGH, VCC is supply to the motors, and when these pins are LOW, 0V is supply to the
motors. We can change the HIGH time duration (TON) to adjust the average voltage supply to
the motors (PWM).
In reality, as we increase TON, we must decrease TOFF to keep the period T=TON+TOFF
constant.
IN1, IN2, IN3, IN4 is use to set the voltage (HIGH or LOW) of OUT1, OUT2, OUT3,
OUT4 respectively.
For example: If we supply IN1 5V, IN2 0V, the output OUT1 will be HIGH (connected to
Vs), OUT2 will be LOW (connected to GND), motor will rotate in one direction. If we supply
IN1 0V, IN2 5V, the output OUT1 will be LOW, OUT2 will be HIGH, motor will rotate in
another direction.
Table 2-3 L298N motor driver specifications
Model L298N
OUT 1 OUT 4 Supply voltage 5-35 V
OUT 2 OUT 3 Max. current per motor 2A
Logic voltage TTL 5 V
ENA ENB
GND
IN1-2-3-4
5V
Vs
Figure 2-12 L298N motor driver
Add the wiring diagram for the motor connection
2.2 Sensors
2.2.1 Line detection sensors
The infrared sensor is used to detect the change in the contrast of the surface.
Light surface reflect light better than dark surface, so we can detect the line (dark) on the
surface (light).
TON DUC THANG UNIVERSITY
Page 9 of 24
Robotics Lab Updated 18/11/2024
Emitter Receiver Emitter Receiver
Figure 2-13 Light reflection of different contrasting surfaces
Using a set of 06 line sensors helps us not only detect the line but also know the distance
between the robot center and the line, that help us control more accurately. The average
voltage supplied to the motors will vary depending on the distance between the robot center
and the line.
0 0 1 1 0 0
0 1 1 0 0 0
0 0 0 1 1 0
Figure 2-14 Using a set of line sensors
The TCRT5000 IR sensor is installed on the robot.
Figure 2-15 TCRT5000 IR sensor
2.2.2 Distance sensors
TON DUC THANG UNIVERSITY
Page 10 of 24
Robotics Lab Updated 18/11/2024
The ultrasonic sensors is used to calculate the distance from the robot to the obstacle.
The emitter emits ultrasonic waves that propagate through the air, when encountering
obstacle, the ultrasonic waves are reflected back to the receiver. To calculate the distance, we
need to measure the time interval between sending and receiving ultrasonic waves, as follow:
v∗t
D=
2
D: Distance between sensor and obstacle (cm).
v: Speed of sound in air ≈ 1/29 (cm/µs).
t: time interval between sending and receiving ultrasonic waves (µs).
The result is divided by 2 because the sound wave travels twice the distance.
The distance equation is rewritten:
t (μs)
D( cm)=
58
Emitter
Ultrasound Obstacle
Receiver
Figure 2-16 Ultrasonic sensor principle
Table 2-4 Ultrasonic sensor specifications
Model HC-SR04
Supply voltage 5V
Measurement range 2 – 450 cm
Operating current < 2mA
Logic voltage TTL 5 V
Figure 2-17 HC-SR04 ultrasonic
sensor
On the robot, there are 03 ultrasonic sensors: 01 front and 02 on the sides.
TON DUC THANG UNIVERSITY
Page 11 of 24
Robotics Lab Updated 18/11/2024
2.2.3 LCD
The robot has an I2C LCD module used to display values or parameters.
Table 2-5 LCD 2004 specifications
Model LCD 2004
Supply voltage 5V
Display 4 lines x 20 characters
Dot matrix 5×8 dots
Figure 2-18 LCD 2004
Example code:
#define robot_name "Robot_1"
//#define Bluetooth_EN
//#define PULSE_COUNTER_EN
#include "QBOT2.h"
void setup()
{
QBOT_init();
[Link](0,1);
[Link]("Hello world");
}
void loop()
{
}
2.2.4 Button
The Robot has 09 push buttons used to set or modify parameters.
Figure 2-19 Buttons set
TON DUC THANG UNIVERSITY
Page 12 of 24
Robotics Lab Updated 18/11/2024
Conventional, the buttons are connected to digital inputs. With
this approach, each button requires one digial input, hence to
connect 09 buttons, we need 9 digital inputs. To save the digital
input, in this work, the buttons are connected as shown in Figure 2-
20. In this case, only an analog input is required.
When a button is pressed, the Out pin voltage is calculated
according to the following equation:
10∗VDD
V OUT = , n=0 ¿ 8
11+ n
By measuring the Out pin voltage (or ADC value), we can
detect which button was pressed.
It is difficult to detect the button if there are more than one
button pressed at the same time
Figure 2-20 Analog
button schematic
The following program display ADC value when a button is pressed:
#define robot_name "Robot_1"
//#define Bluetooth_EN
//#define PULSE_COUNTER_EN
#include "QBOT2.h"
uint16_t adc_bt;
void setup()
{
QBOT_init();
}
void loop()
{
adc_bt=get_ADC_button();
[Link](0,1);
[Link]("ADC button: %5lu ", adc_bt);
delay(200);
}
2.3 Encoder
The robot has 2 encoders attached to each motor to measure the motors speed or measure
the robot moving distance. Each encoder has emitter diode, photo diode and slit plate. The
plate has 20 slits connected directly to the motor shaft. When the motor rotate once, the
encoder sends out 20 pulses.
TON DUC THANG UNIVERSITY
Page 13 of 24
Robotics Lab Updated 18/11/2024
Emitter Photo
diode diode
Slit plate
Figure 2-21 Encoder structure
Table 2-6 Encoder V2 specifications
Model Encoder V2
Supply voltage 3.3-5 V
Ouput current 15 mA max
Figure 2-22 Encoder module
The following codes will count the pulse of each encoder and display on the LCD.
The motor have the reduction ratio of 1:90, so when the wheel rotates once, we will
count 1800 pulses.
#define robot_name "Robot_2"
//#define Bluetooth_EN
#define PULSE_COUNTER_EN
#include "QBOT2.h"
void setup()
{
QBOT_init();
}
void loop()
{
[Link](0,1);
[Link]("L: %5lu, R: %5lu ", lcount, rcount);
delay(200);
}
2.4 Exercise
2.4.1 Exercise 1
TON DUC THANG UNIVERSITY
Page 14 of 24
Robotics Lab Updated 18/11/2024
Turn on the LED when pressing the BT0 button, turn off the LED when pressing the BT1
button.
2.4.2 Exercise 2
Display “TDTU robot” on the LCD.
2.4.3 Exercise 3
Counts the number of times the BT0 button is pressed and displays on the LCD.
2.4.4 Exercise 4
Make the LED blink 02 times when pressing BT0, make the LED blink 05 times when
pressing BT1.
2.4.5 Exercise 5
Counts the number of times the BT0 button is pressed and displays on the LCD. If the
number is above 10, make the buzzer beep twice.
TON DUC THANG UNIVERSITY
Page 15 of 24
Robotics Lab Updated 18/11/2024
Lab 3. OBSTACLE AVOIDANCE
3.1 Objectives
Build algorithm and control program for robot to move avoid obstacles.
3.2 Motors control
We use PWM to control the motor speed, thereby controlling the robot speed and direction.
To change the motors speed, we can use these functions:
left_wheel(<duty>, <direction>)
right_wheel(<duty>, <direction>)
duty: Duty has a value from 0 - 255 corresponding to the motor speed from 0 - 100%.
direction: direction = 1 mean the wheel will rotate in forward direction, direction = 2
mean the wheel will rotate in reverse direction.
For example:
left_wheel(128, 1);
right_wheel(255, 2);
The left wheel rotates forward at a speed of about 50%, and the right wheel rotates in the
reverse direction at maximum speed.
To stop the motor, simply set duty or direction = 0.
Use a combination of the duty and direction parameters to control the robot's speed and
direction.
Example code:
#define robot_name "Robot_1"
//#define Bluetooth_EN
//#define PULSE_COUNTER_EN
#include "QBOT2.h"
void setup()
{
QBOT_init();
}
void loop()
{
left_wheel(120, 1);//forward
right_wheel(120,1);
delay(1000);
left_wheel(255, 0);//stop
right_wheel(255,0);
delay(2000);
left_wheel(120, 2);//reverse
TON DUC THANG UNIVERSITY
Page 16 of 24
Robotics Lab Updated 18/11/2024
right_wheel(120,2);
delay(1000);
left_wheel(255, 0);//stop
right_wheel(255,0);
delay(2000);
}
3.3 Distance sensors
To get the distance between the robot and the surrounding obstacles, we can use the
following functions:
get_left_distance();
get_front_distance();
get_right_distance();
The returned value is in cm unit.
For example:
float ld, fd, rd;
ld = get_left_distance();
fd = get_front_distance();
rd = get_right_distance();
Example code:
#define robot_name "Robot_1"
//#define Bluetooth_EN
//#define PULSE_COUNTER_EN
#include "QBOT2.h"
float ld, fd, rd;
void setup()
{
QBOT_init();
}
void loop()
{
sonar_test();
delay(200);
}
void sonar_test()
{
ld = get_left_distance();
fd = get_front_distance();
rd = get_right_distance();
[Link](0,1);
[Link]("Front: %6.2f ", fd);
[Link](0,2);
TON DUC THANG UNIVERSITY
Page 17 of 24
Robotics Lab Updated 18/11/2024
[Link]("L:%6.2f, R:%6.2f ", ld, rd);
delay(200);
}
3.4 Control algorithm
First, we will measure the distances to the obstacles in front (front_dis) and in the sides
(left_dis and right_dis) of the robot.
If there is an obstacle ahead (front_dis < 20cm), depending on whether the left or right side
of the robot is available, the robot will turn left or right.
If there are obstacles on all sides, the robot will turn around to find another path.
Figure 3-23 Obstacle avoidance algorithm illustration
TON DUC THANG UNIVERSITY
Page 18 of 24
Robotics Lab Updated 18/11/2024
Begin
Measure the robot front_dis, left_dis
and right_dis
True
front_dis > 20cm Move forward
False
True
left_dis > 20cm Turn left
False
True
right_dis > 20cm Turn right
False
Rotate around
End
Figure 3-24 Simple obstacle avoidance control algorithm
Example code:
#include "QBOT2.h"
float ld, cd, rd;
void setup()
{
QBOT_init();
}
void loop()
{
obstacle_avoidance();
delay(200);
}
TON DUC THANG UNIVERSITY
Page 19 of 24
Robotics Lab Updated 18/11/2024
void obstacle_avoidance()
{
ld = get_left_distance();
fd = get_cent_distance();
rd = get_right_distance();
if (fd > 20.0)
{
forward();
}
else if(ld > 20.0)
{
rot_left();
}
else
{
rot_right();
}
}
3.5 Exercies
3.5.1 Exercise 1
Read all distance sensors and display on LCD.
3.5.2 Exercise 2
Let the robot move forward until the front distance below 30 cm.
3.5.3 Exercise 3
Let the robot move 80 cm forward and then stop.
3.5.4 Exercise 4
Let the robot rotate 90° and then stop.
3.5.5 Exercise 5
Let the robot move along the edge of an 80 cm square twice.
80 cm
3.5.6 Exercise 6
TON DUC THANG UNIVERSITY
Page 20 of 24
Robotics Lab Updated 18/11/2024
Let the robot move along the edge of a circle with a radius of 50 cm twice.
50 cm
3.5.7 Exercise 7
Write the obstacle avoidance program.
TON DUC THANG UNIVERSITY
Page 21 of 24
Robotics Lab Updated 18/11/2024
Lab 4. LINE FOLLOWING
4.1 Objectives
Build algorithm and control program for robot to move follow the line.
4.2 Line sensors
To read value of one IR sensor, use the following code:
bool val = 0;
val = digitalRead(line0);
val has value 1 means there is a dark line at the top of the sensor, otherwise it has a value
of 0.
#define robot_name "Robot_1"
//#define Bluetooth_EN
//#define PULSE_COUNTER_EN
#include "QBOT2.h"
bool l0, l1, l2;
void setup()
{
QBOT_init();
}
void loop()
{
l0=digitalRead(line0);
l1=digitalRead(line1);
l2=digitalRead(line2);
[Link](0,1);
[Link]("%u %u %u ",l2, l1, l0);
delay(100);
}
4.3 Control algorithm
First, we will get values from IR sensors set.
If the line is in the middle of the robot, it means the robot is going in the right path, keep
going forward.
If the line is skewed to the left of the robot, turn the robot to the left, if the line is skewed to
the right of the robot, turn the robot to the right to follow the path.
TON DUC THANG UNIVERSITY
Page 22 of 24
Robotics Lab Updated 18/11/2024
Figure 4-25 Line following algorithm illustration
4.4 Exercises
4.4.1 Exercise 1
Read all IR line sensors and display values on LCD.
4.4.2 Exercise 2
Read IR line sensor line0 and display value on LCD. If the value is 1, make the buzzer
beep for 200 ms.
4.4.3 Exercise 3
Read IR line sensor line0 and display value on LCD. If the value is 1, turn on the LED,
otherwise turn off the LED.
4.4.4 Exercise 4
Let robot move forward until all IR line sensors are activated.
TON DUC THANG UNIVERSITY
Page 23 of 24
Robotics Lab Updated 18/11/2024
Begin
Get values from IR sensors set
True
The line is in the midle? Move forward
False
True
The line is skewed to the left? Turn left
False
True
The line is skewed to the right? Turn right
False
End
Figure 4-26 Basic line following control algorithm
4.4.5 Exercise 5
Let robot move along the dark line.
Start End
TON DUC THANG UNIVERSITY
Page 24 of 24