yt_link
insta_link
fb_link
twitter_link

Gyro controller
page 1/2


Gyro radio controller

Help me by sharing this post



This is a radio controller that has 2 analog channels and the data is out from a MPU6050 gyro module. So, we could control a toy car for example just by rotating the controller. I usually use the NRF24 module, but in this project I also want to show you how to use the HC12 module. You will learn how to get the IMU data, how to use the HC12 radio connection and how to control 2 DC motors using PWM signals and an H-bridge.

arduino radio hc12 MPU6050


See components we need here:






PART 1 - Radio transmitter

Schematic

The radio transmitter is simple. In the middle we haev the MPU6050 gyro mdoule so we could detect the angle of the controller. To control everything we have the Arduino NANO and as a supply, I'll use a 9V battery connected to a sliding switch so we could turn the controller ON/OFF. I also have a Joystick, but we won't use that in this first part of the tutorial, we will add 2 more channels later with that joystick. Finally, to send the data, i'll use the HC12 radio mdoule that uses a serial UART communication. It seems to have a better range than the NRF24 mdoules.


schematic gyro radio controller



Transmitter code

Now that we have the schematic, is time to code the transmitter. What I want is to read the X and Y angles and then send the data to the receiver. Maping the angles to a PWM signal shoudl control motors, LEDs, etc. So the first step is to read the Gyro and Accelerations data and get the real angles.


Transmitter code (2 channels):

Read the IMU

To read the IMU data we will use i2c comunication so for that we need the wire library. That's why in the code we first inport that library adn also the SoftwareSerial library for the HC12 UART communication. Uisng the wire functions, as seen in the example below, we get the gyro and acc data. Then using the formulas and some filters we finally get the real angle from -90º to 90º for both X and Y axis.



  Wire.beginTransmission(0x68);			//Begin, Send the slave adress (in this case 68) 
  Wire.write(0x43);				//First adress of the Gyro data
  Wire.endTransmission(false);			//The transmission is still open
  Wire.requestFrom(0x68,4,true);		//But now we ask for 4 registers (gyro data)        
  Gyr_rawX=Wire.read()<<8|Wire.read();		//Once again we shif and sum for X gyro data
  Gyr_rawY=Wire.read()<<8|Wire.read();		//Once again we shif and sum for Y gyro data			

Use the HC12 radio module

Now, the Arduino has a UART serial port on pins 0 and 1 which are the TX and RX pins. But we want to use different pins as a UART port. For taht we use the Software serial and create the TX and RX pins on digital pins of the ARduino D11 and D10. Now we could connect the HC12 to these pins and send data using the Serial.print() or Serial.write() functions.



#include <SoftwareSerial.h>		//Inport the software serial library
SoftwareSerial HC12(11, 10);              // D11 is HC-12 TX Pin, D10 is HC-12 RX Pin		

void setup() {
  Serial.begin(9600);                     //Start serial com in order to print values on monitor
  HC12.begin(9600);                       //Start the HC12 serial communication at 9600 bauds. Change bauds if needed
  begin_MPU6050();                        //Call this function and start the MPU6050 module    
}//end of setup void	

When you buy the HC12 module, it usually works at a baud rate of 9600. You could change that if you want using the code below. Make the connections as in the scheamtic below and also connect the set pin for this example. Without the set pin connected we can't put the module into config mode.

Change HC12 baud rate code:

schematic gyro radio controller

After you upload the code above, open serial monitor and in that window below, set the baud rate to 9600. Now type AT+___ where ____ is the command that you want. Below you ahve some examples. For example, to set the baud rate to 115200 type AT+B115200 and press enter and the new baud rate is set to the HC-12. That's it.

1. Change Baud Rate:
Enter “AT+Bxxxx” to change baud rate to xxxx bps.
Example: Enter “AT+B19200” and the module will work at baud rate 19200 bps and return “OK+B19200”.
The available values of xxxx are: 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200.
The default baud rate is 9600 bps.
2. Change Communication Channel:
Enter “AT+Cyyy” to change communication channel to yyy.
Example: Enter “AT+C021” and the module will change to channel 021 and return “OK+C021”.
The available values of yyy are: 001, 002, 003, …, 127.
The default channel is 001.
3. Factory Reset:
Enter “AT+DEFAULT” and the module will reset to default settings and return “OK+DEFAULT”.



Finally, in the code we use the HC12.write functions to send the X and Y data for the angles. First we send the "X" and "Y" character, so in the receiver we would be able to know which data is for the X and which for Y. We place a delay if 50ms after each data send. Make sure the receiver delay is superior to these 50ms.


 HC12.print(x_send);HC12.write("X"); //Send the x angle value and then the "X" character
 HC12.print(y_send);HC12.write("Y"); //Send the y angle value and then the "Y" character
 delay(50); //Add a delay of 50. This might affect the transmission  
}//end of setup void	






PART 2 - Radio receiver

Schematic

The radio receiver is also simple. We receive the data with the HC-12 module. Then we map the angles from -90 to 90 to a range of 0 to 200 PWM signal. That signal is applied to and H-bridge module that will control the speed and direction of rotaion ot two DC motors for our "toy car" example. As supply, once again, I'll use a 9V abttery connected to the Vin pin.


schematic HC12 radio controller arduino

Receiver code

The code is simple as well. We need once again the SoftwareSerial library so we could get data from the HC12 module. We create 4 PWM signals to control the H-bridge. As we can see in the example below, we store the incoming bytes in a buffer than we will divide that buffer at the X and Y characters. For example, if we receive X23Y55 than we cut the X and Y and we get the 22º and 55º angles for each axis. Finally, we detect the range of the angles and change the value of the PWM signals, and by that control the car speed and direction of rotation of the motors.

Receiver code (2 channels):



while (HC12.available()> 0) {               // If the HC-12 has data in
    incomingByte = HC12.read();             // Store the data byte by byte
    readBuffer += char(incomingByte);       // Add each byte to ReadBuffer total string variable
  }

Ok, uplaod this code to the receiver scheamtic and our project is ready. Move the controller around and you will see the car moving. The bigger is the inclination angle of the controller the higher is the speed as you can see in the video below. I hope that you've learned something new with this simple sutorial. Next page will show you how to change the receiver and get 4 PWM signals out of the receiver pins just as a normal PWM radio receiver.








Next part →

Help me by sharing this post








ADVERTISERS
PCBWAY PCB Manufacturing

PCBWAY Christas PCB Manufacturing

Advanced PCB Manufacturing
Advanced PCB Manufacturing

AFFILATE

Gearbest SG - 700 Satellite Navigation Foldable RC Drone Quadcopter - WHITE 720P WIFI FPV CAMERA
SG - 700 Satellite Navigation Foldable RC Drone Quadcopter - WHITE 720P WIFI FPV CAMERA only $46.99

Gearbest Xiaomi AMAZFIT Heart Rate Smartwatch - BLACK INTERNATIONAL VERSION
Xiaomi AMAZFIT Heart Rate Smartwatch - BLACK INTERNATIONAL VERSION only $89.99

Gearbest FeiLun FT011 2.4GHz Brushless RC Racing Boat - BLACK AND WHITE AND RED
FeiLun FT011 2.4GHz Brushless RC Racing Boat - BLACK AND WHITE AND RED only $96.99