Gallax
Published

RF24 Uno Joystick to L298N Motor and Servo contro (Working)

A full guide to controlling your own RC car with steering. Updates coming soon.

BeginnerProtip4,497
RF24 Uno Joystick to L298N Motor and Servo contro (Working)

Things used in this project

Hardware components

ELEGOO UNO R3 Board ATmega328P ATMEGA16U2 with USB Cable
ELEGOO UNO R3 Board ATmega328P ATMEGA16U2 with USB Cable
×2
RF24L01 with Antenna
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Male/Male Jumper Wires
×1
Any shield for Uno (optional)
×1
nRF24L01 Adaptor (optional)
×2
Geared motors
×2
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1
Analog joystick (Generic)
×1

Story

Read more

Schematics

RF24 Joystick Wiring example - See code for actual pinout

See code for pinout.

RF24 adaptor pinout.

You can also wire directly to the uno, use the comments in the code

L298N Wiring example - see code for pinout

See code for more
input voltage is about 7v

Code

RF24L01 Joystick Send

Arduino
RF24 Radio transmitter controller with joystick
/* Radio Transmitter
 *  Elegoo Uno with starter kit shield. 
 *  nRF24L01 + PA + LNA with antenna - USE 3V!!! you can only use 5V with adaptor 
 *  i used a 9v battery directly into the Uno. 
 *  Can be done with any pair of UNOs - Written by Gallax 
 *  
 * ////////////////////////// Joystick connection /////////////////////////////
 *  
 *  GND       - 
 *  5V or 3V  -  
 *  X out     - A0
 *  Y out     - A1
 *  SW(button)- NC - not connected
 *   
 *   //////RF24 Adaptor or use this pinout directly into the Uno:) / ////////
 *  
 *  __________________________________   
 *  || 3V  ||  10 ||   11  ||  NC   ||           NC - not connected. 
 *  || VCC-||-CSN-||- MOSI-||- IRQ  ||         note: remember CE 9  CSN 10 
 *  ||_____||_____||_______||_______||                      
 *  || GND-||-CE -||- SCK -||- MISO ||
 *  || GND || 9   ||  13   ||   12  ||
 *  ||_____||_____||_______||_______||
 *  
 *  
 *  
 *
*/



#include <SPI.h>
#include <nRF24L01.h>                    // include RF24 libraries
#include <RF24.h>

RF24 radio(9, 10);                      // CE, CSN
const uint64_t pipe = 0xE8E8F0F0E1LL;   // set channel
// const byte address[6] = "00001";     // also sets channel
  
  int xPin = A0;                       // integer for joystick, x axis, analog pin A0
  int yPin = A5;                       // integer for joystick, y axis, analog pin A5

 // int x;               
//  int y; 
  int data[8];                         // forming datagroup, 8 bits is enough for car 
  
//////////////////////////////////////////////////////////////////////////////
  
void setup() {
  
  Serial.begin(9600);                  // start serial monitor for debugging
 
  radio.begin();                       // start radio
  radio.openWritingPipe(pipe);         // this is the controller
  radio.setPALevel(RF24_PA_HIGH);      // High power
  radio.setDataRate(RF24_250KBPS);     // data rate 250 kb/s
  radio.stopListening();               // stops listening to transmit

  }

/////////////////////////////////////////////////////////////////////////////

void loop() {                     

  xPin = analogRead(A0);                     // read x pin from joystick 
  yPin = analogRead(A5);                    //  read y pin from joystick 

  data[0] = xPin;                 // defines xPin which is A0 as data
  data[1] = yPin;               // defines yPin which is A1 as data
  
  
  radio.write(&data, sizeof(data));          // write 8 bits of data to receiver
                                             // no mapping required for transmitter                       

 
  Serial.print("x:");          // text for debugging 
  Serial.println(data[0]);    // prints data  notice this value is print line
  Serial.print("y:");        //print the values with to plot or view
  Serial.print(data[1]);     // prints the next piece of data 
  Serial.print("\t");       // i dunno it works 
}

RF24L01 - L298N - Motor with Servo for steering

Arduino
Radio receiver controls DC motor Forwards and backwards. Steering is allowed.
 
/* Radio Receiver
 *  Adeept UNO with Motor Shield V2
 *  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
 *  L298N - 7.4 V input - two 46850 batteries , use whatever you like .
 *  Can be done with any pair of UNOs - Written by Gallax 
 *  
 *  L298N is grounded to Uno. 
 *  
 *  Servo pin 2 or P4 on Adeept shield 
 *  CE 9  CSN 10 for Adeept shield  
 *  
 *    ////////////L298N CONNECTIONS  ON ADEEPT SHIELD OR UNO ////////////////
 *                   IN1    IN2    IN3    IN4       SPD1    SPD2 
 *                    8      4      7      5         9       3
 *  
 *  I didnt use RF24 Adaptor, shield has one built in. So this is universal for Unos.  
 *   *   ////////////////////// RF24 Adaptor or use this pinout :) / 
 *  __________________________________   
 *  || 3V  ||  10 ||   11  ||  NC   ||           NC - not connected. 
 *  || VCC-||-CSN-||- MOSI-||- IRQ  ||
 *  ||_____||_____||_______||_______||  
 *  || GND-||-CE -||- SCK -||- MISO ||
 *  || GND || 9   ||  13   ||   12  ||
 *  ||_____||_____||_______||_______||
 *  

 *  ///////////////////////////Servo connection //////////////////////////
 *  
 *  Servo is connected to 5V, GND, pin is 2 (search for servo.attach for reference)
 *  Adeept shield requires pin 2 = P4 - yes this was quite a job. 
 *  
*/

#include <SPI.h>
#include <nRF24L01.h>       // include RF24 libraries
#include <RF24.h>           // This will need some tweaking. Excellent job. 
#include <Servo.h>          // Include Servo library 

Servo servo;               // creates servo object to control servo 
                           // twelve servo objects can be created on most boards   

RF24 radio(9, 10);                           // CE, CSN
const uint64_t pipe = 0xE8E8F0F0E1LL;       // sets channel 
// const byte address[6] = "00001";         // alternative     
 
int data[8];                   // create dataset array 8 bit 


int enA = 9;   // speed control A
int in1 = 8;                  // Motor A connections
int in2 = 7;

int enB = 3;   // speed control B
int in3 = 5;                  // Motor B connections           
int in4 = 4; 
 
int xDir = 90;          // initial direction for servo later  

////////////////////////////////////////////////////////////////////////

void setup() {
  
  Serial.begin(9600);            // start serial monitor for debugging 

  radio.begin();                     // starts the radio 
  radio.openReadingPipe(0, pipe);    // sets this RF24 as receiver
  radio.setPALevel(RF24_PA_MIN);    // sets radio signal strength 
  radio.setDataRate(RF24_250KBPS);   // sets datarate to 250 kbps 
  radio.startListening();            // starts listening for data 

 
 /// ///////////// Set all the motor control pins to outputs/////////////////
  pinMode(enA, OUTPUT);     // speed control motor A on L298N
  pinMode(enB, OUTPUT);     // speed control motor B on L298N
  pinMode(in1, OUTPUT);     // motor A output - will become forward high    
  pinMode(in2, OUTPUT);     // motor A output - will become forward low 
  pinMode(in3, OUTPUT);     // motor B output - will become forward high
  pinMode(in4, OUTPUT);     // motor B output - will become forward low 
    
  servo.attach(2);        //sets servo pin to 2 = P4 on Adeept Shield

}

//////////////////LISTEN, READ, MAPPING AND PRINTING DATA////////////////////
 
void loop() {

   radio.startListening();             // receiver starts listening 
   
   if (radio.available()) {            // if the radio has connection 
   
   radio.read(&data, sizeof(data));    // read all in the dataset, sizeof is for numeric purposes 
  
   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 
   int y = map(data[1], 0, 1024, 0, 255);   // now data is next in line at  1. 
                                            // maps data from 0 - 1024 --- converted by me to 0 - n where n = 180 or 255 
  
  Serial.print("x:");       // serial print text  for reading       
  Serial.println(data[0]);  // (note only one with ln)print data, if fails, check connections. (note only one with ln)
  Serial.print("y:");       //prints text
  Serial.print(data[1]);    // prints the next piece of data 
  Serial.print("\t");       // i dunno, youll see why i didnt change this
  
 /////////////////////IF DATA RECEIVED - SERVO CONTROLS//////////////////////
 
 if ( data[0] == 0 )  {          // if data 0 is equal to 0. servo turns left
  xDir = 180;                    // see unit circle
  servo.write(xDir);             // TURN  
  delay(15);                     // small wait 
 }
 
 if ( data[0] > 600 )  {           // if data is greater than or equal to 600
  xDir = 0;                       // dir is 0 
  servo.write(xDir);             // TURN
  delay(15);                     // rinse repeat     
 }

 if (data[0] == 329 or data[0] == 328 ) {      // more if data for x axis, (steering)
  xDir = 90;                                   // 90 is straight 
  servo.write(xDir);                           // no turn 
  delay(15);                                  // small wait 
 }

/////////////////////IF DATA RECEIVED - MOTOR CONTROLS///////////////////////

if ( data[1] >= 650 )  {
  
  analogWrite(enA, 255);
  analogWrite(enB, 255);
  // Set motors to maximum speed
  // For PWM maximum possible values are 0 to 255
 
  // Turn on motor A & B
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);           //  GO MOTORS!
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);

 }
 
/********************************************************************/

 else {                               
  analogWrite(enA, 0);
  analogWrite(enB, 0); 
  digitalWrite(in1, LOW);            //  STOP MOTORS
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);          
  digitalWrite(in4, LOW);
 }
 
/////////////////////////////////////////////////////////////////////

 if ( data[1] <= 10 )  {
  analogWrite(enA, 255);
  analogWrite(enB, 255);
  digitalWrite(in1, LOW);                   //  BACKWARDS MOTORS
  digitalWrite(in2, HIGH);                // if failed Check your wires, motor faces up (red wire on top) 
  digitalWrite(in3, LOW);                 
  digitalWrite(in4, HIGH);               
 }

/////////////////////////////////////////////////////////////////////

   
 else {         // waiting for connection, or NOT connected
 
 Serial.println("Not connected");  // prints a line of text
 delay(1000);                      // waits/repeats every second   
  }                               // delete this else statement if it becomes an eyesore
 }
}





  ////////////////      END       /////////////////

Credits

Gallax

Gallax

4 projects • 3 followers

Comments