| Buy from OSOYOO | Buy from US | Buy from UK | Buy from DE | Buy from IT | Buy from FR | Buy from ES | Buy from JP |
Content
This tutorial focuses on enhancing the OSOYOO V3 Robot Car’s autonomous capabilities by maintaining a desired path utilizing the MPU6050 Inertial Measurement Unit (IMU). The MPU6050, a 3-axis motion tracking device, provides critical angular velocity (gyroscope) and linear acceleration (accelerometer) data, enabling the robot to actively correct its heading and maintain a desired path. This lesson will delve into sensor fusion techniques and advanced control algorithms to achieve robust straight-line performance.
For the initial assembly phase, the following components are required. Please ensure all parts are accounted for prior to commencing the build
| No. | Picture | Device | Qty. | Accessories | Link |
| 1 | ![]() |
MPU6050 3 Axis Gyroscope | 1 | Click here to buy |
The MPU6050 integrates a 3-axis gyroscope and a 3-axis accelerometer. The gyroscope measures angular velocity, providing information about the robot’s rotational speed around its X, Y, and Z axes. Integrating this data over time yields angular displacement (e.g., yaw angle). However, raw gyroscope data is susceptible to drift, accumulating errors over time. The accelerometer measures linear acceleration, which can be used to determine the device’s orientation relative to gravity. While accelerometers are stable over long periods, they are highly sensitive to vibrations and transient linear movements, making their raw orientation data noisy.

Step 1: Install basic framework as robot car Lesson 1 . If you have already completed hardware installation in Lesson 1 , just keep it as is.

Step 2: Mount the MPU6050 module securely on the robot chassis as following. (The MPU6050 communicates with the Arduino microcontroller via the I2C (Inter-Integrated Circuit) protocol. This requires only two data lines: SDA (Serial Data) and SCL (Serial Clock), in addition to power (VCC) and ground (GND).)
OSOLINK Board Pin MPU6050 +-----------+ +---------+ | | | | | 5V |------------| VCC | | | | | | GND |------------| GND | | | | | | A5 |------------| SCL | | | | | | A4 |------------| SDA | | | | | +-----------+ +---------+

| Open-source Arduino Software(IDE) |
![]() |
Download software here: https://www.arduino.cc/en/Main/Software?setlang=en |
| 7 zip is a free zip utility that unzips zip files |
![]() |
Download 7zip here for free https://www.7-zip.org/ |
1. Arduino IDE Setup: Download and install the latest version of the Arduino Integrated Development Environment (IDE) from https://www.arduino.cc/en/Main/Software?setlang=en.

2. Code Acquisition: Download the provided sample code for Lesson 2 from here. Extract the contents and you will get a v3car-lesson2.ino in the folder v3car-lesson2.
3. Board and Port Selection: Connect the OSOYOO Basic Board (compatible with Arduino UNO) to your computer via a USB cable(Crucially, ensure the robot car’s power switch is OFF and the battery is disconnected before connect the board to your PC). Launch the Arduino IDE. Navigate to Tools > Board and select Arduino Uno. Then, go to Tools > Port and select the appropriate serial port. If unsure, check your operating system’s device manager for the assigned port.

4. Install Libraries:
Firstly, download Libraries zip files from following links:
https://osoyoo.com/driver/2wd/MPU6050_light.zip
https://osoyoo.com/driver/2wd/PID.zip
Second, do NOT unzip them, open Arduino IDE ->Sketch ->Include Library ->Add Zip Library. Then upload above zip files to Arduino One by One.

5. Code Upload: Open the v3car-lesson2.ino sketch in the Arduino IDE. Click the “Upload” button (right arrow icon) to compile and transfer the sketch to OSOYOO Basic Board.


6. Check whether Arduino detect MPU6050
Click Serial Monitor (at right upper corner), set baud rate to 9600, then you should see: MPU6050 Found! as following. Congrats! This means Arduino detects MPU6050 and you can put the car in the ground and test the performance.

Note: If you see “Failed to find MPU6050 chip“, it means MPU wire connection has some problem. You should make sure:
1)MPU6050 VCC/GND/SDA/SCL are properly connected to OSOYOO Wifi/BLE I/O shield as per our instruction
2)Arduino A4/A5 pin and SDA/SCL pins have no other wires(In line tracking projects, A4/A5 might be occupied by tracking sensor wires)
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.By meticulously following these guidelines and understanding the underlying principles, you can transform your OSOYOO V3 Robot Car into a highly capable platform for
autonomous navigation, laying the groundwork for more advanced robotics projects.
DownLoad Url osoyoo.com