Electronics Champ
Published © GPL3+

Gesture Controlled Car using NRF24L01 and MPU6050

This car can be controlled with hand gestures. It can move front, back, left and right.

IntermediateShowcase (no instructions)7,451
Gesture Controlled Car using NRF24L01 and MPU6050

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Arduino UNO
Arduino UNO
×1
nRF24L01
×2
MPU6050
×1
L298N Motor Driver
×1
DC Motor, 12 V
DC Motor, 12 V
×2
Breadboard (generic)
Breadboard (generic)
×1
9V battery (generic)
9V battery (generic)
×2
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic for Transmitter

Schematic for Receiver

Connections Between nRF24L01 and Arduino

Code

Code for Transmitter

Arduino
/*
  This program identifies the tilt position
  of MPU6050 and assigns a value based on
  the position.

  This value is sent to the receiver module
  through NRF24L01

  This program is made by Shreyas for
  Electronics Champ YouTube Channel.
  Please subscribe to this channel
  Thank You
*/

//Include the libraries
#include <Wire.h>
#include <TinyMPU6050.h>
#include <SPI.h>
#include <NRFLite.h>

MPU6050 mpu (Wire);

int message;

const static uint8_t RADIO_ID = 1;             // Our radio's id.
const static uint8_t DESTINATION_RADIO_ID = 0; // Id of the radio we will transmit to.
const static uint8_t PIN_RADIO_CE = 7;
const static uint8_t PIN_RADIO_CSN = 8;

struct RadioPacket { // Any packet up to 32 bytes can be sent.

  uint8_t FromRadioId;
  uint32_t Data;
  uint32_t FailedTxCount;

};

//Create NRF24 object
NRFLite _radio;
RadioPacket _radioData;

void setup() {

  // Initialization
  mpu.Initialize();
  // Calibration (wait for about 20s to calibrate)
  mpu.Calibrate();

  //start up
  Serial.begin(9600);
  Serial.println("Done Clabration");

  if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN)) {

    Serial.println("Cannot communicate with radio");
    while (1); // Wait here forever.
  }

  _radioData.FromRadioId = RADIO_ID;

}

void loop() {

  mpu.Execute();

  while (mpu.GetRawAccX() <= -8000) {

    //send msg to move front
    message = 1;
    _radioData.Data = message;
    sendData();
    Serial.println("front");
    mpu.Execute();

  }

  while (mpu.GetRawAccX() >= 8000) {

    //send msg to move back
    message = 2;
    sendData();
    _radioData.Data = message;
    Serial.println("back");
    mpu.Execute();

  }

  while (mpu.GetRawAccY() <= -8000) {

    //send msg to move left
    message = 3;
    sendData();
    _radioData.Data = message;
    Serial.println("left");
    mpu.Execute();

  }

  while (mpu.GetRawAccY() >= 8000) {

    //send msg to move right
    message = 4;
    sendData();
    _radioData.Data = message;
    Serial.println("right");
    mpu.Execute();

  }

  while (mpu.GetRawAccX() < 8000 and mpu.GetRawAccX() > -8000 and mpu.GetRawAccY() < 8000 and mpu.GetRawAccY() > -8000) {

    //send msg to stop
    message = 0;
    sendData();
    _radioData.Data = message;
    Serial.println("none");
    mpu.Execute();

  }

}

void sendData() {

  if (_radio.send(DESTINATION_RADIO_ID, &_radioData, sizeof(_radioData))) { // Note how '&' must be placed in front of the variable name.

  }

  else {

    Serial.println("Failed");
    _radioData.FailedTxCount++;

  }

  delay(500);
  mpu.Execute();

}

Code for Receiver

Arduino
/*
  This program receives the message from
  the transmitter and moves the car
  accordingly.

  This program is made by Shreyas for
  Electronics Champ YouTube Channel.
  Please subscribe to this channel
  Thank You
*/

//Include the libraries
#include <SPI.h>
#include <NRFLite.h>

//Initializing the variables
boolean x = 0;
int directionOfMovement = 0;
int leftMotorForward = 2;
int leftMotorBackward = 3;
int rightMotorForward = 4;
int rightMotorBackward = 5;
String message;
const static uint8_t RADIO_ID = 0;       // Our radio's id.  The transmitter will send to this id.
const static uint8_t PIN_RADIO_CE = 7;
const static uint8_t PIN_RADIO_CSN = 8;

struct RadioPacket { // Any packet up to 32 bytes can be sent.

  uint8_t FromRadioId;
  uint32_t Data;
  uint32_t FailedTxCount;

};

//Create NRF object
NRFLite _radio;
RadioPacket _radioData;

void setup() {

  Serial.begin(9600);

  //Set the pin modes
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);

  if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN)) {

    Serial.println("Cannot communicate with radio");
    while (1); // Wait here forever.

  }

}

void loop() {

  while (_radio.hasData()) {

    _radio.readData(&_radioData); // Note how '&' must be placed in front of the variable name.

    message = _radioData.Data;
    Serial.println(message);
    directionOfMovement = message.toInt();
    moveAccordingly();

  }

}

//this function moves the car according to the message
void moveAccordingly() {

  if (directionOfMovement == 1) {

    front();
    Serial.println("front");

  }

  else if (directionOfMovement == 2) {

    back();
    Serial.println("back");

  }

  else if (directionOfMovement == 3) {

    left();
    Serial.println("left");

  }

  else if (directionOfMovement == 4) {

    right();
    Serial.println("right");

  }

  else if (directionOfMovement == 0) {

    none();
    Serial.println("none");

  }

}

void front() {

  digitalWrite(leftMotorForward, HIGH);
  digitalWrite(rightMotorForward, HIGH);
  digitalWrite(leftMotorBackward, LOW);
  digitalWrite(rightMotorBackward, LOW);

}

void back() {

  digitalWrite(leftMotorBackward, HIGH);
  digitalWrite(rightMotorBackward, HIGH);
  digitalWrite(leftMotorForward, LOW);
  digitalWrite(rightMotorForward, LOW);

}

void left() {

  digitalWrite(leftMotorForward, LOW);
  digitalWrite(rightMotorForward, HIGH);
  digitalWrite(leftMotorBackward, LOW);
  digitalWrite(rightMotorBackward, LOW);

}
void right() {

  digitalWrite(leftMotorForward, HIGH);
  digitalWrite(rightMotorForward, LOW);
  digitalWrite(leftMotorBackward, LOW);
  digitalWrite(rightMotorBackward, LOW);

}

void none() {

  digitalWrite(leftMotorForward, LOW);
  digitalWrite(rightMotorForward, LOW);
  digitalWrite(leftMotorBackward, LOW);
  digitalWrite(rightMotorBackward, LOW);

}

Credits

Electronics Champ

Electronics Champ

3 projects • 10 followers
Projects based on breadboard electronics and Arduino with clear step-by-step instructions, circuit diagrams, schematics, and source code.

Comments