RF24 Uno Joystick to L298N Motor and Servo contro (Working)
A full guide to controlling your own RC car with steering. Updates coming soon.
Components and supplies
Male/Female Jumper Wires
RF24L01 with Antenna
Dual H-Bridge motor drivers L298
Male/Male Jumper Wires
Any shield for Uno (optional)
SG90 Micro-servo motor
Analog joystick (Generic)
nRF24L01 Adaptor (optional)
ELEGOO UNO R3 Board ATmega328P ATMEGA16U2 with USB Cable
Geared motors
Project description
Code
RF24L01 - L298N - Motor with Servo for steering
arduino
Radio receiver controls DC motor Forwards and backwards. Steering is allowed.
1 2/* Radio Receiver 3 * Adeept UNO with Motor Shield V2 4 * Radio module: nRF24L01 + PA + LNA with antenna built in -USE 3V !!!! built in, 5V is fine ONLY with shield or adaptor, or bypass capacitor, see specs 5 * L298N - 7.4 V input - two 46850 batteries , use whatever you like . 6 * Can be done with any pair of UNOs - Written by Gallax 7 * 8 * L298N is grounded to Uno. 9 * 10 * Servo pin 2 or P4 on Adeept shield 11 * CE 9 CSN 10 for Adeept shield 12 * 13 * ////////////L298N CONNECTIONS ON ADEEPT SHIELD OR UNO //////////////// 14 * IN1 IN2 IN3 IN4 SPD1 SPD2 15 * 8 4 7 5 9 3 16 * 17 * I didnt use RF24 Adaptor, shield has one built in. So this is universal for Unos. 18 * * ////////////////////// RF24 Adaptor or use this pinout :) / 19 * __________________________________ 20 * || 3V || 10 || 11 || NC || NC - not connected. 21 * || VCC-||-CSN-||- MOSI-||- IRQ || 22 * ||_____||_____||_______||_______|| 23 * || GND-||-CE -||- SCK -||- MISO || 24 * || GND || 9 || 13 || 12 || 25 * ||_____||_____||_______||_______|| 26 * 27 28 * ///////////////////////////Servo connection ////////////////////////// 29 * 30 * Servo is connected to 5V, GND, pin is 2 (search for servo.attach for reference) 31 * Adeept shield requires pin 2 = P4 - yes this was quite a job. 32 * 33*/ 34 35#include <SPI.h> 36#include <nRF24L01.h> // include RF24 libraries 37#include <RF24.h> // This will need some tweaking. Excellent job. 38#include <Servo.h> // Include Servo library 39 40Servo servo; // creates servo object to control servo 41 // twelve servo objects can be created on most boards 42 43RF24 radio(9, 10); // CE, CSN 44const uint64_t pipe = 0xE8E8F0F0E1LL; // sets channel 45// const byte address[6] = "00001"; // alternative 46 47int data[8]; // create dataset array 8 bit 48 49 50int enA = 9; // speed control A 51int in1 = 8; // Motor A connections 52int in2 = 7; 53 54int enB = 3; // speed control B 55int in3 = 5; // Motor B connections 56int in4 = 4; 57 58int xDir = 90; // initial direction for servo later 59 60//////////////////////////////////////////////////////////////////////// 61 62void setup() { 63 64 Serial.begin(9600); // start serial monitor for debugging 65 66 radio.begin(); // starts the radio 67 radio.openReadingPipe(0, pipe); // sets this RF24 as receiver 68 radio.setPALevel(RF24_PA_MIN); // sets radio signal strength 69 radio.setDataRate(RF24_250KBPS); // sets datarate to 250 kbps 70 radio.startListening(); // starts listening for data 71 72 73 /// ///////////// Set all the motor control pins to outputs///////////////// 74 pinMode(enA, OUTPUT); // speed control motor A on L298N 75 pinMode(enB, OUTPUT); // speed control motor B on L298N 76 pinMode(in1, OUTPUT); // motor A output - will become forward high 77 pinMode(in2, OUTPUT); // motor A output - will become forward low 78 pinMode(in3, OUTPUT); // motor B output - will become forward high 79 pinMode(in4, OUTPUT); // motor B output - will become forward low 80 81 servo.attach(2); //sets servo pin to 2 = P4 on Adeept Shield 82 83} 84 85//////////////////LISTEN, READ, MAPPING AND PRINTING DATA//////////////////// 86 87void loop() { 88 89 radio.startListening(); // receiver starts listening 90 91 if (radio.available()) { // if the radio has connection 92 93 radio.read(&data, sizeof(data)); // read all in the dataset, sizeof is for numeric purposes 94 95 int x = map(data[0], 0, 1024, 0, 180); // creates integer x, maps data[0], 0 is the first integer i used for the array, other than starting at 1 96 int y = map(data[1], 0, 1024, 0, 255); // now data is next in line at 1. 97 // maps data from 0 - 1024 --- converted by me to 0 - n where n = 180 or 255 98 99 Serial.print("x:"); // serial print text for reading 100 Serial.println(data[0]); // (note only one with ln)print data, if fails, check connections. (note only one with ln) 101 Serial.print("y:"); //prints text 102 Serial.print(data[1]); // prints the next piece of data 103 Serial.print("\ "); // i dunno, youll see why i didnt change this 104 105 /////////////////////IF DATA RECEIVED - SERVO CONTROLS////////////////////// 106 107 if ( data[0] == 0 ) { // if data 0 is equal to 0. servo turns left 108 xDir = 180; // see unit circle 109 servo.write(xDir); // TURN 110 delay(15); // small wait 111 } 112 113 if ( data[0] > 600 ) { // if data is greater than or equal to 600 114 xDir = 0; // dir is 0 115 servo.write(xDir); // TURN 116 delay(15); // rinse repeat 117 } 118 119 if (data[0] == 329 or data[0] == 328 ) { // more if data for x axis, (steering) 120 xDir = 90; // 90 is straight 121 servo.write(xDir); // no turn 122 delay(15); // small wait 123 } 124 125/////////////////////IF DATA RECEIVED - MOTOR CONTROLS/////////////////////// 126 127if ( data[1] >= 650 ) { 128 129 analogWrite(enA, 255); 130 analogWrite(enB, 255); 131 // Set motors to maximum speed 132 // For PWM maximum possible values are 0 to 255 133 134 // Turn on motor A & B 135 digitalWrite(in1, HIGH); 136 digitalWrite(in2, LOW); // GO MOTORS! 137 digitalWrite(in3, HIGH); 138 digitalWrite(in4, LOW); 139 140 } 141 142/********************************************************************/ 143 144 else { 145 analogWrite(enA, 0); 146 analogWrite(enB, 0); 147 digitalWrite(in1, LOW); // STOP MOTORS 148 digitalWrite(in2, LOW); 149 digitalWrite(in3, LOW); 150 digitalWrite(in4, LOW); 151 } 152 153///////////////////////////////////////////////////////////////////// 154 155 if ( data[1] <= 10 ) { 156 analogWrite(enA, 255); 157 analogWrite(enB, 255); 158 digitalWrite(in1, LOW); // BACKWARDS MOTORS 159 digitalWrite(in2, HIGH); // if failed Check your wires, motor faces up (red wire on top) 160 digitalWrite(in3, LOW); 161 digitalWrite(in4, HIGH); 162 } 163 164///////////////////////////////////////////////////////////////////// 165 166 167 else { // waiting for connection, or NOT connected 168 169 Serial.println("Not connected"); // prints a line of text 170 delay(1000); // waits/repeats every second 171 } // delete this else statement if it becomes an eyesore 172 } 173} 174 175 176 177 178 179 //////////////// END /////////////////
RF24L01 Joystick Send
arduino
RF24 Radio transmitter controller with joystick
1 2/* Radio Transmitter 3 * Elegoo Uno with starter kit shield. 4 * nRF24L01 + PA + LNA with antenna - USE 3V!!! you can only use 5V with adaptor 5 * i used a 9v battery directly into the Uno. 6 * Can be done with any pair of UNOs - Written by Gallax 7 * 8 * ////////////////////////// Joystick connection ///////////////////////////// 9 * 10 * GND - 11 * 5V or 3V - 12 * X out - A0 13 * Y out - A1 14 * SW(button)- NC - not connected 15 * 16 * //////RF24 Adaptor or use this pinout directly into the Uno:) / //////// 17 * 18 * __________________________________ 19 * || 3V || 10 || 11 || NC || NC - not connected. 20 * || VCC-||-CSN-||- MOSI-||- IRQ || note: remember CE 9 CSN 10 21 * ||_____||_____||_______||_______|| 22 * || GND-||-CE -||- SCK -||- MISO || 23 * || GND || 9 || 13 || 12 || 24 * ||_____||_____||_______||_______|| 25 * 26 * 27 * 28 * 29*/ 30 31 32 33#include <SPI.h> 34#include <nRF24L01.h> // include RF24 libraries 35#include <RF24.h> 36 37RF24 radio(9, 10); // CE, CSN 38const uint64_t pipe = 0xE8E8F0F0E1LL; // set channel 39// const byte address[6] = "00001"; // also sets channel 40 41 int xPin = A0; // integer for joystick, x axis, analog pin A0 42 int yPin = A5; // integer for joystick, y axis, analog pin A5 43 44 // int x; 45// int y; 46 int data[8]; // forming datagroup, 8 bits is enough for car 47 48////////////////////////////////////////////////////////////////////////////// 49 50void setup() { 51 52 Serial.begin(9600); // start serial monitor for debugging 53 54 radio.begin(); // start radio 55 radio.openWritingPipe(pipe); // this is the controller 56 radio.setPALevel(RF24_PA_HIGH); // High power 57 radio.setDataRate(RF24_250KBPS); // data rate 250 kb/s 58 radio.stopListening(); // stops listening to transmit 59 60 } 61 62///////////////////////////////////////////////////////////////////////////// 63 64void loop() { 65 66 xPin = analogRead(A0); // read x pin from joystick 67 yPin = analogRead(A5); // read y pin from joystick 68 69 data[0] = xPin; // defines xPin which is A0 as data 70 data[1] = yPin; // defines yPin which is A1 as data 71 72 73 radio.write(&data, sizeof(data)); // write 8 bits of data to receiver 74 // no mapping required for transmitter 75 76 77 Serial.print("x:"); // text for debugging 78 Serial.println(data[0]); // prints data notice this value is print line 79 Serial.print("y:"); //print the values with to plot or view 80 Serial.print(data[1]); // prints the next piece of data 81 Serial.print("\ "); // i dunno it works 82} 83 84 85
RF24L01 Joystick Send
arduino
RF24 Radio transmitter controller with joystick
1 2/* Radio Transmitter 3 * Elegoo Uno with starter kit shield. 4 * nRF24L01 + PA + LNA with antenna - USE 3V!!! you can only use 5V with adaptor 5 * i used a 9v battery directly into the Uno. 6 * Can be done with any pair of UNOs - Written by Gallax 7 * 8 * ////////////////////////// Joystick connection ///////////////////////////// 9 * 10 * GND - 11 * 5V or 3V - 12 * X out - A0 13 * Y out - A1 14 * SW(button)- NC - not connected 15 * 16 * //////RF24 Adaptor or use this pinout directly into the Uno:) / //////// 17 * 18 * __________________________________ 19 * || 3V || 10 || 11 || NC || NC - not connected. 20 * || VCC-||-CSN-||- MOSI-||- IRQ || note: remember CE 9 CSN 10 21 * ||_____||_____||_______||_______|| 22 * || GND-||-CE -||- SCK -||- MISO || 23 * || GND || 9 || 13 || 12 || 24 * ||_____||_____||_______||_______|| 25 * 26 * 27 * 28 * 29*/ 30 31 32 33#include <SPI.h> 34#include <nRF24L01.h> // include RF24 libraries 35#include <RF24.h> 36 37RF24 radio(9, 10); // CE, CSN 38const uint64_t pipe = 0xE8E8F0F0E1LL; // set channel 39// const byte address[6] = "00001"; // also sets channel 40 41 int xPin = A0; // integer for joystick, x axis, analog pin A0 42 int yPin = A5; // integer for joystick, y axis, analog pin A5 43 44 // int x; 45// int y; 46 int data[8]; // forming datagroup, 8 bits is enough for car 47 48////////////////////////////////////////////////////////////////////////////// 49 50void setup() { 51 52 Serial.begin(9600); // start serial monitor for debugging 53 54 radio.begin(); // start radio 55 radio.openWritingPipe(pipe); // this is the controller 56 radio.setPALevel(RF24_PA_HIGH); // High power 57 radio.setDataRate(RF24_250KBPS); // data rate 250 kb/s 58 radio.stopListening(); // stops listening to transmit 59 60 } 61 62///////////////////////////////////////////////////////////////////////////// 63 64void loop() { 65 66 xPin = analogRead(A0); // read x pin from joystick 67 yPin = analogRead(A5); // read y pin from joystick 68 69 data[0] = xPin; // defines xPin which is A0 as data 70 data[1] = yPin; // defines yPin which is A1 as data 71 72 73 radio.write(&data, sizeof(data)); // write 8 bits of data to receiver 74 // no mapping required for transmitter 75 76 77 Serial.print("x:"); // text for debugging 78 Serial.println(data[0]); // prints data notice this value is print line 79 Serial.print("y:"); //print the values with to plot or view 80 Serial.print(data[1]); // prints the next piece of data 81 Serial.print("\ "); // i dunno it works 82} 83 84 85
RF24L01 - L298N - Motor with Servo for steering
arduino
Radio receiver controls DC motor Forwards and backwards. Steering is allowed.
1 2/* Radio Receiver 3 * Adeept UNO with Motor Shield V2 4 * Radio module: nRF24L01 + PA + LNA with antenna built in -USE 3V !!!! built in, 5V is fine ONLY with shield or adaptor, or bypass capacitor, see specs 5 * L298N - 7.4 V input - two 46850 batteries , use whatever you like . 6 * Can be done with any pair of UNOs - Written by Gallax 7 * 8 * L298N is grounded to Uno. 9 * 10 * Servo pin 2 or P4 on Adeept shield 11 * CE 9 CSN 10 for Adeept shield 12 * 13 * ////////////L298N CONNECTIONS ON ADEEPT SHIELD OR UNO //////////////// 14 * IN1 IN2 IN3 IN4 SPD1 SPD2 15 * 8 4 7 5 9 3 16 * 17 * I didnt use RF24 Adaptor, shield has one built in. So this is universal for Unos. 18 * * ////////////////////// RF24 Adaptor or use this pinout :) / 19 * __________________________________ 20 * || 3V || 10 || 11 || NC || NC - not connected. 21 * || VCC-||-CSN-||- MOSI-||- IRQ || 22 * ||_____||_____||_______||_______|| 23 * || GND-||-CE -||- SCK -||- MISO || 24 * || GND || 9 || 13 || 12 || 25 * ||_____||_____||_______||_______|| 26 * 27 28 * ///////////////////////////Servo connection ////////////////////////// 29 * 30 * Servo is connected to 5V, GND, pin is 2 (search for servo.attach for reference) 31 * Adeept shield requires pin 2 = P4 - yes this was quite a job. 32 * 33*/ 34 35#include <SPI.h> 36#include <nRF24L01.h> // include RF24 libraries 37#include <RF24.h> // This will need some tweaking. Excellent job. 38#include <Servo.h> // Include Servo library 39 40Servo servo; // creates servo object to control servo 41 // twelve servo objects can be created on most boards 42 43RF24 radio(9, 10); // CE, CSN 44const uint64_t pipe = 0xE8E8F0F0E1LL; // sets channel 45// const byte address[6] = "00001"; // alternative 46 47int data[8]; // create dataset array 8 bit 48 49 50int enA = 9; // speed control A 51int in1 = 8; // Motor A connections 52int in2 = 7; 53 54int enB = 3; // speed control B 55int in3 = 5; // Motor B connections 56int in4 = 4; 57 58int xDir = 90; // initial direction for servo later 59 60//////////////////////////////////////////////////////////////////////// 61 62void setup() { 63 64 Serial.begin(9600); // start serial monitor for debugging 65 66 radio.begin(); // starts the radio 67 radio.openReadingPipe(0, pipe); // sets this RF24 as receiver 68 radio.setPALevel(RF24_PA_MIN); // sets radio signal strength 69 radio.setDataRate(RF24_250KBPS); // sets datarate to 250 kbps 70 radio.startListening(); // starts listening for data 71 72 73 /// ///////////// Set all the motor control pins to outputs///////////////// 74 pinMode(enA, OUTPUT); // speed control motor A on L298N 75 pinMode(enB, OUTPUT); // speed control motor B on L298N 76 pinMode(in1, OUTPUT); // motor A output - will become forward high 77 pinMode(in2, OUTPUT); // motor A output - will become forward low 78 pinMode(in3, OUTPUT); // motor B output - will become forward high 79 pinMode(in4, OUTPUT); // motor B output - will become forward low 80 81 servo.attach(2); //sets servo pin to 2 = P4 on Adeept Shield 82 83} 84 85//////////////////LISTEN, READ, MAPPING AND PRINTING DATA//////////////////// 86 87void loop() { 88 89 radio.startListening(); // receiver starts listening 90 91 if (radio.available()) { // if the radio has connection 92 93 radio.read(&data, sizeof(data)); // read all in the dataset, sizeof is for numeric purposes 94 95 int x = map(data[0], 0, 1024, 0, 180); // creates integer x, maps data[0], 0 is the first integer i used for the array, other than starting at 1 96 int y = map(data[1], 0, 1024, 0, 255); // now data is next in line at 1. 97 // maps data from 0 - 1024 --- converted by me to 0 - n where n = 180 or 255 98 99 Serial.print("x:"); // serial print text for reading 100 Serial.println(data[0]); // (note only one with ln)print data, if fails, check connections. (note only one with ln) 101 Serial.print("y:"); //prints text 102 Serial.print(data[1]); // prints the next piece of data 103 Serial.print("\ "); // i dunno, youll see why i didnt change this 104 105 /////////////////////IF DATA RECEIVED - SERVO CONTROLS////////////////////// 106 107 if ( data[0] == 0 ) { // if data 0 is equal to 0. servo turns left 108 xDir = 180; // see unit circle 109 servo.write(xDir); // TURN 110 delay(15); // small wait 111 } 112 113 if ( data[0] > 600 ) { // if data is greater than or equal to 600 114 xDir = 0; // dir is 0 115 servo.write(xDir); // TURN 116 delay(15); // rinse repeat 117 } 118 119 if (data[0] == 329 or data[0] == 328 ) { // more if data for x axis, (steering) 120 xDir = 90; // 90 is straight 121 servo.write(xDir); // no turn 122 delay(15); // small wait 123 } 124 125/////////////////////IF DATA RECEIVED - MOTOR CONTROLS/////////////////////// 126 127if ( data[1] >= 650 ) { 128 129 analogWrite(enA, 255); 130 analogWrite(enB, 255); 131 // Set motors to maximum speed 132 // For PWM maximum possible values are 0 to 255 133 134 // Turn on motor A & B 135 digitalWrite(in1, HIGH); 136 digitalWrite(in2, LOW); // GO MOTORS! 137 digitalWrite(in3, HIGH); 138 digitalWrite(in4, LOW); 139 140 } 141 142/********************************************************************/ 143 144 else { 145 analogWrite(enA, 0); 146 analogWrite(enB, 0); 147 digitalWrite(in1, LOW); // STOP MOTORS 148 digitalWrite(in2, LOW); 149 digitalWrite(in3, LOW); 150 digitalWrite(in4, LOW); 151 } 152 153///////////////////////////////////////////////////////////////////// 154 155 if ( data[1] <= 10 ) { 156 analogWrite(enA, 255); 157 analogWrite(enB, 255); 158 digitalWrite(in1, LOW); // BACKWARDS MOTORS 159 digitalWrite(in2, HIGH); // if failed Check your wires, motor faces up (red wire on top) 160 digitalWrite(in3, LOW); 161 digitalWrite(in4, HIGH); 162 } 163 164///////////////////////////////////////////////////////////////////// 165 166 167 else { // waiting for connection, or NOT connected 168 169 Serial.println("Not connected"); // prints a line of text 170 delay(1000); // waits/repeats every second 171 } // delete this else statement if it becomes an eyesore 172 } 173} 174 175 176 177 178 179 //////////////// END /////////////////
Downloadable files
RF24 Joystick Wiring example - See code for actual pinout
See code for pinout.
RF24 Joystick Wiring example - See code for actual pinout
L298N Wiring example - see code for pinout
See code for more input voltage is about 7v
L298N Wiring example - see code for pinout
RF24 adaptor pinout.
You can also wire directly to the uno, use the comments in the code
RF24 adaptor pinout.
L298N Wiring example - see code for pinout
See code for more input voltage is about 7v
L298N Wiring example - see code for pinout
RF24 Joystick Wiring example - See code for actual pinout
See code for pinout.
RF24 Joystick Wiring example - See code for actual pinout
Comments
Only logged in users can leave comments