I. Objective
II. Parts and Devices
III. Hardware Installation
IV. Software Installation
V. Functional Testing and Verification
VI. Troubleshooting
*****************************************************************************************************************
*****************************************************************************************************************
In this lesson, we will learn how to program a gyroscope with Arduino to optimize the Espro 2WD Robot Car’s performance.
This tutorial focuses on enhancing the Espro 2WD Robot Car’s ability to drive straight by using the MPU6050 Inertial Measurement Unit (IMU). The MPU6050, a 3-axis motion tracking device, provides angular velocity (gyroscope) and linear acceleration (accelerometer) data, enabling the robot to actively correct its heading. This lesson covers sensor fusion techniques and PID control algorithms for robust straight-line performance.
You must complete lesson 1 before you continue on with this lesson1.
OSOYOO ESPro Robot car chassis x1
OSOYOO Wheels x2
OSOYOO DC motors x2
OSOYOO ESPro motor driver board x1
OSOYOO Voltage meter x1
OSOYOO MPU6050 Gyro Module x1
OSOYOO Battery box x1
OSOYOO 3pin female to female jumper wire x1
OSOYOO 4pin female to female jumper wire x1
18650 Batteries (3.7 V) x2
Battery charger x1
Step1: Complete the basic hardware assembly as described in robot car Lesson 1. If you already finished Lesson 1, just keep everything as is.
Step 2: Mount the MPU6050 module securely on the robot chassis as shown below. (The MPU6050 communicates with the Arduino via the I2C protocol, which requires only two data lines: SDA (Serial Data) and SCL (Serial Clock), plus power (VCC) and ground (GND).

| Open-source Arduino Software(IDE) |
![]() |
Download Arduino IDE here: https://www.arduino.cc/en/Main/Software?setlang=en |
| 7 zip is a free zip utility that un-zips zip files |
![]() |
Download 7zip here for free https://www.7-zip.org/ |
Step 1. Arduino IDE Setup
Download and install the latest Arduino IDE from:https://www.arduino.cc/en/Main/Software?setlang=en.
Step 2.Download Sample Code
Download https://osoyoo.com/download/code/espro-2wd-car/espro-lesson3.zip, unzip the download zip file , you will see a folder called espro-lesson4-go-straight.ino,open the ino file in arudino IDE.
Step 3. Install Libraries
Method 1: Install from Zip File
First, download the library ZIP files from the links below:
https://osoyoo.com/download/code/espro-2wd-car/MPU6050_light.zip
https://osoyoo.com/download/code/espro-2wd-car/PID.zip
Next, do NOT unzip them. Open Arduino IDE -> Sketch -> Include Library -> Add .ZIP Library. Add the ZIP files to the Arduino IDE one by one.

Method 2 : Install from Arduino IDE
In Arduino IDE top menu -> Sketch ->Include Library->Manage Libraries
Search MPU6050_light , you will find a library “MPU6050_light by rfetick”,

Click Install, then to install MPU6050_light library.

Similarly, install PID library from Arduino IDE as following :

Step 4. Board and Port Selection
Connect the ESPro motor driver board to your computer via a USB cable (Important: make sure the robot car’s power switch is OFF and the battery is disconnected before connecting the board to your PC), Last select correct port which matches ESP32 board,upload the sketch to the board, upload the Code.

In Serial Monitor, set baud rate to 9600, then you should see:


Step 5.Check Whether Arduino Detects the MPU6050
Open the Serial Monitor (top-right corner of the Arduino IDE), set the baud rate to 9600, and you should see MPU6050 Found! as shown below. Congratulations! The Arduino is detecting the MPU6050 — you are ready to test the car.

Now you can put the car onto the ground, turn on the power. You will see the car will stay freeze for a while waiting the Arduino to initialize the gyro. Then the car will move straight forward . If you use your foot to force the car change direction, the car will automatically go back to its original direction.
Move() function control the car’s movement and steering?move(speedL, speedR) function has two parameters, speedL and speedR, which represent the rotational speed of the car’s left and right wheels, respectively. The speed value ranges from -255 to +255, where a positive value indicates forward rotation and a negative value indicates backward rotation. For example, move(-50, 100) means the left wheel rotates backward at a speed of 50, while the right wheel rotates forward at a speed of 100. In this case, the car should make a sharp left turn.After initialization, the gyroscope MPU6050 can consistently determine its orientation relative to its starting direction. When our robot car moves and deviates from this initial direction, the MPU6050 reports the amount of this deviation back to the Arduino.
In our code, the Direction variable represents the car’s orientation at the moment it was placed on the ground (we assume it’s facing forward, so we set Direction to 0).
After the calculations in the first few lines of the loop() function, we obtain the car’s current orientation in the currentAngle variable on line 129.
By combining the answers to these three questions, the PID controller makes a smooth and efficient steering correction.
PID_v1.h library and your code.setup()):// We tell the PID controller what to watch and what to control.
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
Direction = 0; // Our TARGET is to have an angle of 0 degrees.
myPID.SetMode(AUTOMATIC); // Turn on the controller.
loop()):// 1. We get the sensor reading and put it into the Input variable.
Input = currentAngle;
// 2. We call the compute function. This is where the PID magic happens!
myPID.Compute();
// 3. We use the result to control the motors.
int speedL = baseSpeed - Output;
int speedR = baseSpeed + Output;
move(speedL, speedR);
myPID.Compute() function does all the hard work internally. Every time it’s called (the library handles the timing based on SetSampleTime), it:Output is +15, it means the robot needs a correction of “15 units.” We apply this by slowing down one motor (baseSpeed - 15) and speeding up the other (baseSpeed + 15), causing the robot to turn and reduce its error. If the robot is perfectly straight, Input will be 0, the error will be 0, and Output will be 0, so both motors will run at baseSpeed.Ki = 0.0 and Kd = 0.0.Kp value that is stable and doesn’t oscillate (or barely oscillates). This will likely be lower than 1.5.deadband of around 0.5 degrees to your loop to ignore sensor noise.Kp is stable, slowly add Kd to dampen any remaining overshoot.Ki to eliminate any long-term, steady drift.
DownLoad Url osoyoo.com