Components and supplies
DC Motor, 12 V
MPU6050
L298N Motor Driver
nRF24L01
9V battery (generic)
Arduino Nano R3
Breadboard (generic)
Jumper wires (generic)
Arduino UNO
Apps and platforms
Arduino IDE
Project description
Code
Code for Transmitter
arduino
1/* 2 This program identifies the tilt position 3 of MPU6050 and assigns a value based on 4 the position. 5 6 This value is sent to the receiver module 7 through NRF24L01 8 9 This program is made by Shreyas for 10 Electronics Champ YouTube Channel. 11 Please subscribe to this channel 12 Thank You 13*/ 14 15//Include the libraries 16#include <Wire.h> 17#include <TinyMPU6050.h> 18#include <SPI.h> 19#include <NRFLite.h> 20 21MPU6050 mpu (Wire); 22 23int message; 24 25const static uint8_t RADIO_ID = 1; // Our radio's id. 26const static uint8_t DESTINATION_RADIO_ID = 0; // Id of the radio we will transmit to. 27const static uint8_t PIN_RADIO_CE = 7; 28const static uint8_t PIN_RADIO_CSN = 8; 29 30struct RadioPacket { // Any packet up to 32 bytes can be sent. 31 32 uint8_t FromRadioId; 33 uint32_t Data; 34 uint32_t FailedTxCount; 35 36}; 37 38//Create NRF24 object 39NRFLite _radio; 40RadioPacket _radioData; 41 42void setup() { 43 44 // Initialization 45 mpu.Initialize(); 46 // Calibration (wait for about 20s to calibrate) 47 mpu.Calibrate(); 48 49 //start up 50 Serial.begin(9600); 51 Serial.println("Done Clabration"); 52 53 if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN)) { 54 55 Serial.println("Cannot communicate with radio"); 56 while (1); // Wait here forever. 57 } 58 59 _radioData.FromRadioId = RADIO_ID; 60 61} 62 63void loop() { 64 65 mpu.Execute(); 66 67 while (mpu.GetRawAccX() <= -8000) { 68 69 //send msg to move front 70 message = 1; 71 _radioData.Data = message; 72 sendData(); 73 Serial.println("front"); 74 mpu.Execute(); 75 76 } 77 78 while (mpu.GetRawAccX() >= 8000) { 79 80 //send msg to move back 81 message = 2; 82 sendData(); 83 _radioData.Data = message; 84 Serial.println("back"); 85 mpu.Execute(); 86 87 } 88 89 while (mpu.GetRawAccY() <= -8000) { 90 91 //send msg to move left 92 message = 3; 93 sendData(); 94 _radioData.Data = message; 95 Serial.println("left"); 96 mpu.Execute(); 97 98 } 99 100 while (mpu.GetRawAccY() >= 8000) { 101 102 //send msg to move right 103 message = 4; 104 sendData(); 105 _radioData.Data = message; 106 Serial.println("right"); 107 mpu.Execute(); 108 109 } 110 111 while (mpu.GetRawAccX() < 8000 and mpu.GetRawAccX() > -8000 and mpu.GetRawAccY() < 8000 and mpu.GetRawAccY() > -8000) { 112 113 //send msg to stop 114 message = 0; 115 sendData(); 116 _radioData.Data = message; 117 Serial.println("none"); 118 mpu.Execute(); 119 120 } 121 122} 123 124void sendData() { 125 126 if (_radio.send(DESTINATION_RADIO_ID, &_radioData, sizeof(_radioData))) { // Note how '&' must be placed in front of the variable name. 127 128 } 129 130 else { 131 132 Serial.println("Failed"); 133 _radioData.FailedTxCount++; 134 135 } 136 137 delay(500); 138 mpu.Execute(); 139 140} 141 142
Code for Transmitter
arduino
1/* 2 This program identifies the tilt position 3 of MPU6050 and assigns a value based on 4 the position. 5 6 This value is sent to the receiver module 7 through NRF24L01 8 9 This program is made by Shreyas for 10 Electronics Champ YouTube Channel. 11 Please subscribe to this channel 12 Thank You 13*/ 14 15//Include the libraries 16#include <Wire.h> 17#include <TinyMPU6050.h> 18#include <SPI.h> 19#include <NRFLite.h> 20 21MPU6050 mpu (Wire); 22 23int message; 24 25const static uint8_t RADIO_ID = 1; // Our radio's id. 26const static uint8_t DESTINATION_RADIO_ID = 0; // Id of the radio we will transmit to. 27const static uint8_t PIN_RADIO_CE = 7; 28const static uint8_t PIN_RADIO_CSN = 8; 29 30struct RadioPacket { // Any packet up to 32 bytes can be sent. 31 32 uint8_t FromRadioId; 33 uint32_t Data; 34 uint32_t FailedTxCount; 35 36}; 37 38//Create NRF24 object 39NRFLite _radio; 40RadioPacket _radioData; 41 42void setup() { 43 44 // Initialization 45 mpu.Initialize(); 46 // Calibration (wait for about 20s to calibrate) 47 mpu.Calibrate(); 48 49 //start up 50 Serial.begin(9600); 51 Serial.println("Done Clabration"); 52 53 if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN)) { 54 55 Serial.println("Cannot communicate with radio"); 56 while (1); // Wait here forever. 57 } 58 59 _radioData.FromRadioId = RADIO_ID; 60 61} 62 63void loop() { 64 65 mpu.Execute(); 66 67 while (mpu.GetRawAccX() <= -8000) { 68 69 //send msg to move front 70 message = 1; 71 _radioData.Data = message; 72 sendData(); 73 Serial.println("front"); 74 mpu.Execute(); 75 76 } 77 78 while (mpu.GetRawAccX() >= 8000) { 79 80 //send msg to move back 81 message = 2; 82 sendData(); 83 _radioData.Data = message; 84 Serial.println("back"); 85 mpu.Execute(); 86 87 } 88 89 while (mpu.GetRawAccY() <= -8000) { 90 91 //send msg to move left 92 message = 3; 93 sendData(); 94 _radioData.Data = message; 95 Serial.println("left"); 96 mpu.Execute(); 97 98 } 99 100 while (mpu.GetRawAccY() >= 8000) { 101 102 //send msg to move right 103 message = 4; 104 sendData(); 105 _radioData.Data = message; 106 Serial.println("right"); 107 mpu.Execute(); 108 109 } 110 111 while (mpu.GetRawAccX() < 8000 and mpu.GetRawAccX() > -8000 and mpu.GetRawAccY() < 8000 and mpu.GetRawAccY() > -8000) { 112 113 //send msg to stop 114 message = 0; 115 sendData(); 116 _radioData.Data = message; 117 Serial.println("none"); 118 mpu.Execute(); 119 120 } 121 122} 123 124void sendData() { 125 126 if (_radio.send(DESTINATION_RADIO_ID, &_radioData, sizeof(_radioData))) { // Note how '&' must be placed in front of the variable name. 127 128 } 129 130 else { 131 132 Serial.println("Failed"); 133 _radioData.FailedTxCount++; 134 135 } 136 137 delay(500); 138 mpu.Execute(); 139 140} 141 142
Code for Receiver
arduino
1/* 2 This program receives the message from 3 the transmitter and moves the car 4 accordingly. 5 6 This program is made by Shreyas for 7 Electronics Champ YouTube Channel. 8 Please subscribe to this channel 9 Thank You 10*/ 11 12//Include the libraries 13#include <SPI.h> 14#include <NRFLite.h> 15 16//Initializing the variables 17boolean x = 0; 18int directionOfMovement = 0; 19int leftMotorForward = 2; 20int leftMotorBackward = 3; 21int rightMotorForward = 4; 22int rightMotorBackward = 5; 23String message; 24const static uint8_t RADIO_ID = 0; // Our radio's id. The transmitter will send to this id. 25const static uint8_t PIN_RADIO_CE = 7; 26const static uint8_t PIN_RADIO_CSN = 8; 27 28struct RadioPacket { // Any packet up to 32 bytes can be sent. 29 30 uint8_t FromRadioId; 31 uint32_t Data; 32 uint32_t FailedTxCount; 33 34}; 35 36//Create NRF object 37NRFLite _radio; 38RadioPacket _radioData; 39 40void setup() { 41 42 Serial.begin(9600); 43 44 //Set the pin modes 45 pinMode(9, OUTPUT); 46 pinMode(10, OUTPUT); 47 pinMode(12, OUTPUT); 48 pinMode(13, OUTPUT); 49 50 if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN)) { 51 52 Serial.println("Cannot communicate with radio"); 53 while (1); // Wait here forever. 54 55 } 56 57} 58 59void loop() { 60 61 while (_radio.hasData()) { 62 63 _radio.readData(&_radioData); // Note how '&' must be placed in front of the variable name. 64 65 message = _radioData.Data; 66 Serial.println(message); 67 directionOfMovement = message.toInt(); 68 moveAccordingly(); 69 70 } 71 72} 73 74//this function moves the car according to the message 75void moveAccordingly() { 76 77 if (directionOfMovement == 1) { 78 79 front(); 80 Serial.println("front"); 81 82 } 83 84 else if (directionOfMovement == 2) { 85 86 back(); 87 Serial.println("back"); 88 89 } 90 91 else if (directionOfMovement == 3) { 92 93 left(); 94 Serial.println("left"); 95 96 } 97 98 else if (directionOfMovement == 4) { 99 100 right(); 101 Serial.println("right"); 102 103 } 104 105 else if (directionOfMovement == 0) { 106 107 none(); 108 Serial.println("none"); 109 110 } 111 112} 113 114void front() { 115 116 digitalWrite(leftMotorForward, HIGH); 117 digitalWrite(rightMotorForward, HIGH); 118 digitalWrite(leftMotorBackward, LOW); 119 digitalWrite(rightMotorBackward, LOW); 120 121} 122 123void back() { 124 125 digitalWrite(leftMotorBackward, HIGH); 126 digitalWrite(rightMotorBackward, HIGH); 127 digitalWrite(leftMotorForward, LOW); 128 digitalWrite(rightMotorForward, LOW); 129 130} 131 132void left() { 133 134 digitalWrite(leftMotorForward, LOW); 135 digitalWrite(rightMotorForward, HIGH); 136 digitalWrite(leftMotorBackward, LOW); 137 digitalWrite(rightMotorBackward, LOW); 138 139} 140void right() { 141 142 digitalWrite(leftMotorForward, HIGH); 143 digitalWrite(rightMotorForward, LOW); 144 digitalWrite(leftMotorBackward, LOW); 145 digitalWrite(rightMotorBackward, LOW); 146 147} 148 149void none() { 150 151 digitalWrite(leftMotorForward, LOW); 152 digitalWrite(rightMotorForward, LOW); 153 digitalWrite(leftMotorBackward, LOW); 154 digitalWrite(rightMotorBackward, LOW); 155 156} 157 158
Downloadable files
Connections Between nRF24L01 and Arduino
Connections Between nRF24L01 and Arduino
Schematic for Transmitter
Schematic for Transmitter
Schematic for Receiver
Schematic for Receiver
Schematic for Receiver
Schematic for Receiver
Connections Between nRF24L01 and Arduino
Connections Between nRF24L01 and Arduino
Schematic for Transmitter
Schematic for Transmitter
Comments
Only logged in users can leave comments
Gesture Controlled Car using NRF24L01 and MPU6050 | Arduino Project Hub