Remotely / Wireless Glove Controlled Arduino Robot Car | MPU6050 Gyro Accelerometer | nRF24L01+



In this tutorial, we will learn how to control the robot car using wireless gloves. We will use the MPU-6050 Gyro Accelerometer sensor for direction control. We will use the nRF24L01 + module to provide remote communication (data transfer). Of course, the heart of our project will be the Arduino board. --------------------- Required Hardwares Any two Arduino boards: Arduino Uno R3 -- https://goo.gl/o1zoSv Arduino Nano -- https://goo.gl/QbEmNL Arduino Mega -- https://goo.gl/Ao6n2z Any two nRF24L01 modules: nRF24L01 with Antenna -- https://goo.gl/FePZu9 nRF24L01 without Antenna -- https://goo.gl/4t3rkm nRF24L01 Power Adapter -- https://goo.gl/HSfTWq MPU 6050 Gyro and Acc. Sensor -- https://goo.gl/JCWW4S L298N H-Bridge Motor Driver -- https://goo.gl/D79XtE Gloves -- https://goo.gl/Jrykxx 18650 3.7v Li-Ion Battery with Charger -- https://goo.gl/UEJTff 18650 Battery Holder -- https://goo.gl/VU4dfb Mini Breadboard -- https://goo.gl/uvEhR1 3 in 1 120 Pcs Jumper Wires -- https://goo.gl/EGTafY Any Car Chassis: Car Chassis Kit 1 -- https://goo.gl/pPQ6mg Car Chassis Kit 2 -- https://goo.gl/ihAU41 DC Motor and Wheel -- https://goo.gl/isbFAZ Roller Bovine Wheel -- https://goo.gl/A3bGWV Acrylic Perspex Sheet -- https://goo.gl/EvJrmA Extra Hardwares: DANIU Screwdriver Wallet Set -- https://goo.gl/i75Au5 Capacitor Kit -- https://goo.gl/NHP4pi --------------------- Libraries (GitHub) RF24 -- https://goo.gl/qCXZJg I2Cdev -- https://goo.gl/Ke1Wg1 MPU6050 -- https://goo.gl/uHB7jX

---------------------

nRF24L01 Insufficient Power Problems | Tutorial https://youtu.be/bVpYhTBsOOE

---------------------

Source Code (Transmitter - Glove)
//Mert Arduino and Tech YouTube Channel -- https://goo.gl/ivcZhW

//Add the necessary libraries
//You can find all the necessary library links in the video description.
#include <SPI.h>        //SPI library for communicate with the nRF24L01+
#include "RF24.h"       //The main library of the nRF24L01+
#include "Wire.h"       //For communicate
#include "I2Cdev.h"     //For communicate with MPU6050
#include "MPU6050.h"    //The main library of the MPU6050


//Define the object to access and cotrol the Gyro and Accelerometer (We don't use the Gyro data)
MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;

//Define packet for the direction (X axis and Y axis)
int data[2];

//Define object from RF24 library - 9 and 10 are a digital pin numbers to which signals CE and CSN are connected.
RF24 radio(9,10);

//Create a pipe addresses for the communicate                                    
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(void){
  Serial.begin(9600);
  Wire.begin();
  mpu.initialize();              //Initialize the MPU object
  radio.begin();                 //Start the nRF24 communicate     
  radio.openWritingPipe(pipe);   //Sets the address of the receiver to which the program will send data.
}

void loop(void){
  
  //With this function, the acceleration and gyro values of the axes are taken. 
  //If you want to control the car axis differently, you can change the axis name in the map command.
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  //In two-way control, the X axis (data [0]) of the MPU6050 allows the robot to move forward and backward. 
  //Y axis (data [0]) allows the robot to right and left turn.
  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));
}
Source Code (Receiver - Robot Car)
//Mert Arduino and Tech YouTube Channel -- https://goo.gl/ivcZhW

//Add the necessary libraries
//You can find all the necessary library links in the video description
#include <SPI.h>      //SPI library for communicate with the nRF24L01+
#include "RF24.h"     //The main library of the nRF24L01+

//Define enable pins of the Motors
const int enbA = 3;
const int enbB = 5;

//Define control pins of the Motors
//If the motors rotate in the opposite direction, you can change the positions of the following pin numbers
const int IN1 = 2;    //Right Motor (-)
const int IN2 = 4;    //Right Motor (+)
const int IN3 = 7;    //Left Motor (+)
const int IN4 = 6;    //Right Motor (-)

//Define variable for the motors speeds
//I have defined a variable for each of the two motors 
//This way you can synchronize the rotation speed difference between the two motors
int RightSpd = 130;
int LeftSpd = 150;

//Define packet for the direction (X axis and Y axis)
int data[2];

//Define object from RF24 library - 9 and 10 are a digital pin numbers to which signals CE and CSN are connected
RF24 radio(9,10);

//Create a pipe addresses for the communicate
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(){
  //Define the motor pins as OUTPUT
  pinMode(enbA, OUTPUT);
  pinMode(enbB, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  
  Serial.begin(9600);
  radio.begin();                    //Start the nRF24 communicate            
  radio.openReadingPipe(1, pipe);   //Sets the address of the transmitter to which the program will receive data.
  radio.startListening();             
  }

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

    if(data[0] > 380){
      //forward            
      analogWrite(enbA, RightSpd);
      analogWrite(enbB, LeftSpd);
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, HIGH);
      digitalWrite(IN4, LOW);
    }
    
    if(data[0] < 310){
      //backward              
      analogWrite(enbA, RightSpd);
      analogWrite(enbB, LeftSpd);
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
    }
     
    if(data[1] > 180){
      //left
      analogWrite(enbA, RightSpd);
      analogWrite(enbB, LeftSpd);
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
    }

    if(data[1] < 110){
      //right
      analogWrite(enbA, RightSpd);
      analogWrite(enbB, LeftSpd);
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      digitalWrite(IN3, HIGH);
      digitalWrite(IN4, LOW);
    }

    if(data[0] > 330 && data[0] < 360 && data[1] > 130 && data[1] < 160){
      //stop car
      analogWrite(enbA, 0);
      analogWrite(enbB, 0);
    }
  }
}