Devices & Components
Arduino Uno Rev3
nRF24L01 tranceiver
Chassis Car 4wd
9V Battery Connector Snap
MPU6050
Wires generic
DC Gear Motor
DOIT ESP32 DevKit v1
Motor driver L298N
Jumper wires (generic)
9v Battery
Hardware & Tools
Wire cutters/stripper
Software & Tools
Arduino IDE
Project description
Code
Receiver code for arduino UNO
cpp
Also install required libraries.(RF24, TinyMpu6050
1#include <SPI.h> 2#include <nRF24L01.h> 3#include <RF24.h> 4int motor1r = 2; 5int motor1l = 3; 6int motor2r = 4; 7int motor2l = 5; 8 9RF24 radio(7, 8); // CE, CSN 10 11const byte address[6] = "00001"; 12 13void setup() { 14 15 Serial.begin(9600); 16 pinMode(motor1r, OUTPUT); 17 pinMode(motor1l, OUTPUT); 18 pinMode(motor2r, OUTPUT); 19 pinMode(motor2l, OUTPUT); 20 21 radio.begin(); 22 radio.openReadingPipe(0, address); 23 radio.setPALevel(RF24_PA_MIN); 24 radio.startListening(); 25} 26 27void loop() { 28 29 if (radio.available()) { 30 char text[32] = ""; 31 radio.read(&text, sizeof(text)); 32 Serial.println(text); 33 34 if(strcmp(text,"right") == 0){ 35 digitalWrite(motor1r, LOW); 36 digitalWrite(motor1l, HIGH); 37 digitalWrite(motor2r, HIGH); 38 digitalWrite(motor2l, LOW); 39 delay(70); 40 digitalWrite(motor1r, LOW); 41 digitalWrite(motor1l, LOW); 42 digitalWrite(motor2r, LOW); 43 digitalWrite(motor2l, LOW); 44 Serial.println("CAr turning right"); 45 } 46 else if(strcmp(text,"left") == 0){ 47 digitalWrite(motor1r, HIGH); 48 digitalWrite(motor1l, LOW); 49 digitalWrite(motor2r, LOW); 50 digitalWrite(motor2l, HIGH); 51 delay(70); 52 digitalWrite(motor1r, LOW); 53 digitalWrite(motor1l, LOW); 54 digitalWrite(motor2r, LOW); 55 digitalWrite(motor2l, LOW); 56 Serial.println("CAr turning left"); 57 } 58 else if(strcmp(text,"forward") == 0){ 59 digitalWrite(motor1r, HIGH); 60 digitalWrite(motor1l, LOW); 61 digitalWrite(motor2r, HIGH); 62 digitalWrite(motor2l, LOW); 63 delay(70); 64 digitalWrite(motor1r, LOW); 65 digitalWrite(motor1l, LOW); 66 digitalWrite(motor2r, LOW); 67 digitalWrite(motor2l, LOW); 68 69 70 Serial.println("CAr going forward"); 71 } 72 else if(strcmp(text,"backward") == 0){ 73 digitalWrite(motor1r, LOW); 74 digitalWrite(motor1l, HIGH); 75 digitalWrite(motor2r, LOW); 76 digitalWrite(motor2l, HIGH); 77 delay(70); 78 digitalWrite(motor1r, LOW); 79 digitalWrite(motor1l, LOW); 80 digitalWrite(motor2r, LOW); 81 digitalWrite(motor2l, LOW); 82 Serial.println("CAr going backward"); 83 } 84 } 85 86 87}
Transmitter code for esp32
cpp
To program, use the arduino IDE.
1#include <SPI.h> 2#include <Wire.h> 3#include <TinyMPU6050.h> 4#include <nRF24L01.h> 5#include <RF24.h> 6 7MPU6050 mpu (Wire); 8RF24 radio(2, 15); // CE, CSN 9 10const byte address[6] = "00001"; 11 12void setup() { 13 14 Serial.begin(115200); 15 Serial.println("Starting program!"); 16 mpu.Initialize(); 17 mpu.Calibrate(); 18 Serial.println("Calibrated!"); 19 radio.begin(); 20 21 radio.openWritingPipe(address); 22 radio.setPALevel(RF24_PA_MIN); 23 radio.stopListening(); 24} 25 26void loop() { 27 mpu.Execute(); 28 const char t1[]="right"; 29 const char t2[]="left"; 30 const char t3[]="forward"; 31 const char t4[]="backward"; 32 const char t5[]="do nothing"; 33 while (mpu.GetRawAccX() <= -6000) { 34 radio.write(&t3, sizeof(t3)); 35 Serial.println("Forward"); 36 mpu.Execute(); 37 } 38 while (mpu.GetRawAccX() >= 6000) { 39 radio.write(&t4, sizeof(t4)); 40 Serial.println("Backward"); 41 mpu.Execute(); 42 } 43 while (mpu.GetRawAccY() >= 6000) { 44 radio.write(&t1, sizeof(t1)); 45 Serial.println("right"); 46 mpu.Execute(); 47 } 48 while (mpu.GetRawAccY() <= -6000) { 49 radio.write(&t2, sizeof(t2)); 50 Serial.println("left"); 51 mpu.Execute(); 52 } 53 while (mpu.GetRawAccX() < 6000 and mpu.GetRawAccX() > -6000 and mpu.GetRawAccY() < 8000 and mpu.GetRawAccY() > -8000) { 54 radio.write(&t5, sizeof(t5)); 55 Serial.println("nothing"); 56 mpu.Execute(); 57 58 } 59 60}
Downloadable files
Receiver car diagram
using arduino uno
Screenshot 2023-09-09 225620.png

Transmitter part
using esp32
Screenshot 2023-09-09 230428.png

Comments
Only logged in users can leave comments