DC motor controlled by Gyro(MPU 6050) using Arduino & Transceiver - Wire...


Circuit Diagram:


Required Components:


Arduino uno/nano - 2 Nos
Battery - 2 Nos
Transceiver - 2 Nos
DC Motor - 2 Nos
L293D Motor Driver - 1 Nos
Gyro (MPU 6050) - 1 Nos
 

Program Code:

Transmitter Code:

#include <SPI.h>       
#include "RF24.h"      
#include "Wire.h"      
#include "I2Cdev.h"    
#include "MPU6050.h"  

MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;

int data[2];

RF24 radio(9,10);
                              
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(){
  Serial.begin(9600);
  Wire.begin();
  mpu.initialize();
  radio.begin();                    
  radio.openWritingPipe(pipe);
}

void loop(){
 
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);  //With this function, the acceleration and gyro values of the axis are taken.

  data[0] = map(ax, -17000, 17000, 300, 400 ); //Send X axis data
  data[1] = map(ay, -17000, 17000, 100, 200);  //Send Y axis data
  radio.write(data, sizeof(data));

  Serial.println(data[0]);
  Serial.println(data[1]);
}

Receiver Code:

#include <SPI.h>     
#include "RF24.h"    

const int Motorforward  = A1;  
const int Motorbackward = A2;  
const int Motorleft     = A3;   
const int Motorright    = A4;  

int data[2];

RF24 radio(9,10);

const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(){
  pinMode(Motorforward, OUTPUT);
  pinMode(Motorbackward, OUTPUT);
  pinMode(Motorleft, OUTPUT);
  pinMode(Motorright, OUTPUT);
 
  Serial.begin(9600);
  radio.begin();         
  radio.openReadingPipe(1, pipe);
  radio.startListening();            
  }

void loop(){
  if (radio.available()){
    radio.read(data, sizeof(data));

       if(data[0]>300 && data[0]<335){
    digitalWrite(Motorforward,LOW);
    digitalWrite(Motorbackward,HIGH);
  }
    if(data[0]>365 && data[0]<400){
    digitalWrite(Motorforward,HIGH);
    digitalWrite(Motorbackward,LOW);
  }
      if(data[0]>336 && data[0]<364){
    digitalWrite(Motorforward,LOW);
    digitalWrite(Motorbackward,LOW);
  }


   if(data[1]>100 && data[1]<135){
    digitalWrite(Motorleft,HIGH);
    digitalWrite(Motorright,LOW);
  }
    if(data[1]>165 && data[1]<200){
    digitalWrite(Motorleft,LOW);
    digitalWrite(Motorright,HIGH);
  }
     if(data[1]>136 && data[1]<164){
    digitalWrite(Motorleft,LOW);
    digitalWrite(Motorright,LOW);
  }
  }
}

Video:


Comments

Popular posts from this blog

Servomotor controlled by Gyro(MPU6050) using Arduino & Transceiver - Wir...

Servo motor controlled by Joystick using Arduino & Transceiver - Wireles...

DC motor controlled by Joystick using Arduino & L298N Motor driver - Wireless Connection