Introduction: Arduino Motorcar Control Using 6-Axis Sensor
MPU6050 is a 6-Axis (Gyro + Accelerometer) motion tracking device. This motion tracking device is designed for low power, low cost applications. For precision tracking of both fast and slow motions, the parts feature a user-programmable gyro full-scale range of ±250, ±500, ±1000, and ±2000 °/sec (dps ), and a user-programmable accelerometer full-scale range of ±2g, ±4g, ±8g, and ±16g. In this project, we are going to use an Ameba Arduino baord connected with this motion tracking device to control a motorcar which is also under controlled by another Ameba Arduino board.
Step 1: Needed Devices
There are two major components that we need to complete this project:
- Ameba Arduino - This Arduino compatible board can be found in eBay by searching for "Ameba Arduino". Since we need to use wifi to transfer the data from controller to motorcar, it's good to have a board that is already have wifi built-in.
- L298N H-Bridge - Use to control the speed and direction of motors. Can be easily found from either Amazon or eBay.
- 4-wheel motor car - Any motorcar can be used in this project. The one we used can be obtained by searching "Arduino 4wd robot car with speed encoder" in Amazon/eBay.
- Battery - This is used to power up both Ameba board and the motor car. We use cellphone power pack since the USB interface is handy to connect to Ameba.
2. The Controller
- Ameba Arduino - Same board as stated above.
- MPU6050 - This 6-axis sensor can also be found in Amzon/eBay
- Battery - For more options, we chose to use lithium polymer battery to power up Ameba board. Since this power line needs to be connected to 5V input, you need to make sure the input voltage is limited to under 6V. In this case, a cellphone power pack with USB interface is a much better selection.
Step 2: Wiring
For the motorcar side of detail wiring, please refer to step 3 of https://www.instructables.com/id/Control-Motorcars... We will just show the wiring about the controller.
For the motorcar remote control, we use the Ameba board equipped with a MPU6050 six-axis motion-tracking device. We use the Li-ion battery to supply power. In general, a Li-ion battery provides 3.7V voltage (4.2V when fully charged). Connect the positive pole of the battery to the 5V pin on Ameba board, the voltage would be converted to 3.3V by the voltage reduction module. The figure/picture above shows the wiring diagram. Please pay attention to the direction of the X-axis and the Y-axis of MPU6050.
Step 3: Control Description
For controlling the motor car, we use only “Pitch” and “Roll”. Rolling controls the X-axis motion, bending down decreases the value, and pulling down increases the value. Similarly, pitching controls the Y-axis motion. When converting the value to corresponding action, it is recommended to define a no-action range. In the sample code, the motor car has no action when the angle value is between -24 and +24, and only responds when the value is -24~-48 or +24~+48.
Step 4: Sample Code and Result
The Ameba motorcar control code can be downloaded from https://github.com/Ameba8195/Arduino/raw/master/li... and follow the Arduino tutorial, https://www.arduino.cc/en/Guide/Libraries#toc4, to install the library into Arduino IDE.
For motorcar control, open the sample code, “Files” -> “Examples” -> “AmebaMotors” -> “car2wd_mobile_control”. This sample code will set this Ameba baord as an AP and waiting for data/command to control this motorcar. You can run the code as is unless you want to change the AP's SSID, password and channel.
For the controller sample code, open the sample code in “File -> Examples -> AmebaMotor -> car2wd_mpu6050_control”. In this sample code, it will first try to connect with AP, the motorcar, and get data from MPU6050 and send to motorcar.
There are two steps to control the motor car:
- Activate Ameba remote control: To avoid triggering the mpu6050 6-axis sensor by mistake, we set an activate gesture on Ameba remote control. To activate, you have to turn Ameba board upside down and then turn it back twice in 3 seconds.
- Controlling motor car: For controlling the moving of motor car, when Ameba is inclined forward, the motor car would move forward, when Ameba is inclined backward, the motor car moves backward. For controlling the turning of motor car, incline Ameba board to the right to make the car turns right, and incline Ameba board to the left to make the car turns left.
Above is the demo video of this project.