Arduino + NRF24L01 Radio Module RC Control Tutorial Code

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1335
    RC Tractor Guy
    Keymaster

    Here is the code for the NRF24 tutorial. If someone figures out how to add servo control make sure to post an updated version for others to find.

    Controller Code

    /*
    NRF24L01 Module Demo Controller Code
    
    Pin Assignment
    D0 
    D1 
    D2 
    D3 
    D4 
    D5 
    D6 
    D7 Joystick Push Button
    D8 NRF24 CE
    D9 
    D10 NRF24 CSN
    D11 NRF24 MOSI
    D12 NRF24 MISO
    D13 NRF24 SCK
    A0 Joystick Y Axis
    A1 Joystick X Axis
    A2 
    A3 
    A4 
    A5 
    */
    
    // Define variables
    int Right_Joy_X = 0x00;
    int Right_Joy_Y = 0x00;
    int Right_Joy_X_Old = 0x00;
    int Right_Joy_Y_Old = 0x00;
    int Right_Joy_Button = LOW;
    int Head_Lights = 0;
    
    // Include Libraries
    #include <SPI.h>
    #include <RH_NRF24.h>
    
    RH_NRF24 nrf24; // Create NRF24 object
    
    uint8_t command[11]; // Create array for data to be sent
    
    uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; // Create an array buffer for the NRF24 module
    
    void setup()
    {
    pinMode(7, INPUT_PULLUP); // Right joystick button
    
    // initialize the radio module
    nrf24.init();
    nrf24.setChannel(1);
    nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm); 
    
    command[0] = 0x4A;
    command[1] = 0x44;
    command[2] = 0x38;
    command[3] = 0x33;
    command[4] = 0x06;
    command[5] = 0x01;
    
    }
    
    void loop() {
    Check_Vehicle_ID();
    Check_Buttons();
    Check_Pots(); 
    }
    
    void Check_Vehicle_ID(){ 
    
    command[0] = 0x4A; // J
    command[1] = 0x44; // D
    command[2] = 0x38; // 8
    command[3] = 0x33; // 3
    
    }
    
    void Check_Buttons(){
    
    // Check Joystick Push Button
    Right_Joy_Button = digitalRead(7);
    
    if (Right_Joy_Button==LOW)
    {
    
    if (Head_Lights == 0){
    Head_Lights = 1;
    command[10]=1;
    }
    else if (Head_Lights == 1){
    Head_Lights = 0;
    command[10]=0;
    }
    
    // Wait for switch to be released
    while (Right_Joy_Button == LOW)
    {
    Right_Joy_Button = digitalRead(7);
    }
    
    Send_Data();
    
    }
    
    }
    
    void Check_Pots(){
    Right_Joy_X = analogRead(A1);
    Right_Joy_X= map(Right_Joy_X, 0, 1024, 0xFF, 0x00);
    Right_Joy_Y = analogRead(A0);
    Right_Joy_Y= map(Right_Joy_Y, 0, 1024, 0xFF, 0x00);
    
    // Only send data if something has changed, to try an minimise interference
    if(Right_Joy_X != Right_Joy_X_Old || Right_Joy_Y != Right_Joy_Y_Old){
    command[8]=Right_Joy_X;
    command[9]=Right_Joy_Y;
    
    // Send the data
    Send_Data();
    
    // Store the sent values
    Right_Joy_X_Old=Right_Joy_X;
    Right_Joy_Y_Old=Right_Joy_Y;
    }
    
    }
    
    // Function to send the data
    void Send_Data(){
    uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    nrf24.send(command, sizeof(command));
    nrf24.waitPacketSent(); 
    }

    Receiver Code

    /*
    NRF24L01 Module Demo Receiver Code
    
    Pin Assignment
    D0 
    D1 
    D2 
    D3 
    D4 LED Pin
    D5 PWMA
    D6 AIN1
    D7 AIN2
    D8 NRF24 CE
    D9 
    D10 NRF24 CSN
    D11 NRF24 MOSI
    D12 NRF24 MISO
    D13 NRF24 SCK
    A0 
    A1 
    A2 
    A3 
    A4 
    A5 
    */
    
    // This section describes the vehicle (this ID is JD83)
    byte ID1 = 0x4A; // Enter a four byte ID for the vehicle
    byte ID2 = 0x44; // for example F936 for the Fendt 936 model
    byte ID3 = 0x38; // would be ID1 = 0x46; ID2 = 0x39; ID3 = 0x33; ID4 = 0x36
    byte ID4 = 0x33; // because asci character F is 46 in hexidecimal and so on
    
    // Variables used in the program
    int Move_1 = 125; // Variable used to determine the motor direction
    byte Command_Val = 0x00; // Stores the command value
    byte Data_Val_1 = 0x00; // Stores the data value
    byte Data_Val_2 = 0x00; // Stores the data value
    byte Data_Val_3 = 0x00; // Stores the data value
    byte Data_Val_4 = 0x00; // Stores the data value
    byte Data_Val_5 = 0x00; // Stores the data value
    byte Data_Val_6 = 0x00; // Stores the data value
    
    // Include Libraries
    #include <SPI.h> 
    #include <RH_NRF24.h>
    
    RH_NRF24 nrf24; // Create NRF24 object
    
    void setup() 
    {
    pinMode(4, OUTPUT); // Set digital pin 4 as an output
    
    nrf24.init(); // Initiailize NRF24 radio module
    nrf24.setChannel(1); // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
    nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm);
    }
    
    void loop()
    {
    if (nrf24.available()) // If the NRF24 has recieved data
    {
    uint8_t buf[11]; // Create array to store it in
    uint8_t len = sizeof(buf); 
    if (nrf24.recv(buf, &len)) // Store the data
    { 
    // We recieved a string of ten bytes, four ID, one command and six data values but they may not be for this tractor
    if (buf[0] == ID1 && buf[1] == ID2 && buf[2] == ID3 && buf[3] == ID4){ // Confirm that the correct vehicle ID has been recieved
    Command_Val = buf[4]; // Store the new command value
    Data_Val_1 = buf[5]; // Store the new data value
    Data_Val_2 = buf[6]; // Store the new data value
    Data_Val_3 = buf[7]; // Store the new data value
    Data_Val_4 = buf[8]; // Store the new data value
    Data_Val_5 = buf[9]; // Store the new data value
    Data_Val_6 = buf[10]; // Store the new data value
    }
    
    }
    
    // Servo not simple, NRF24 and servo libraries trying to use the same timer 
    
    // Motor Control
    // Data recieved is between 0 and 255. The center is 255/2=127.5 and we want a buffer zone around here to stop the tractor from moving unexpectedly.
    if (Data_Val_5 < 120){ // This if function create a buffer zone of 7.5 bits below 127.5
    Move_1 = map(Data_Val_5, 120, 0, 0,255); // PWM is 0% at 0 and 100% at 255 so scale the data value for use with the motor
    analogWrite(5,Move_1); // Set the PWM value on pin 5 to move the motor
    digitalWrite(6, HIGH); // Set the direction with pins 6 and 7
    digitalWrite(7, LOW); 
    }
    else if (Data_Val_5 > 135){ // This if function create a buffer zone of 7.5 bits above 127.5
    Move_1 = map(Data_Val_5, 135, 255, 0,255); // PWM is 0% at 0 and 100% at 255 so scale the data value for use with the motor
    analogWrite(5,Move_1); // Set the PWM vlue on pin 5 to move the motor
    digitalWrite(7, HIGH); // Set the direction with pins 6 and 7
    digitalWrite(6, LOW); 
    
    }
    else{ // Every other value is in the buffer zone
    Move_1=0; 
    analogWrite(5,Move_1); // Set the PWM to 0%
    digitalWrite(6, LOW); // Set the direction with pins 6 and 7 but not necessary
    digitalWrite(7, LOW);
    }
    
    // LED Control
    if(Data_Val_6==1){ 
    digitalWrite(4, HIGH); // Turn LED on
    }
    else {
    digitalWrite(4, LOW); // Turn LED off
    }
    }
    }

    #1336
    RC Tractor Guy
    Keymaster

    I have changed to using the RF24 library in my tractors instead of the library used above as the above library interfered with the servo library. To see the new RF24 library in use you should take a look at the Massey 8680 code here.

    #1415
    TynanErto
    Participant

    Thanks for sharing it here. I want to know about its hardware.
    What are its hardware components and what are their specifications?
    Also what about its firmware, how i can upload it?
    Can you please share its whole setup?

    #1417
    RC Tractor Guy
    Keymaster

    Have you seen this video, you can see the set up there.

    I use an Arduino pro mini to control the NRF24L01 radio module and I use pins 9 and 10 for the CE and CS. I just uses the RF24 library to control it and here is a video about using that.

    #1733
    lacoste2500
    Participant

    want add an servo motor ,is possible ?

    #1740
    RC Tractor Guy
    Keymaster

    Yes, write one of the data values to a servo and it should work.

    #2793
    Jaco9307
    Participant

    if i wanted to send the imput and only datalog it on the other board, how would i go about that?

    #3560
    Benb
    Participant

    Guys,

    I’m having a real struggle with this code, I’m using an uno with fundino joypad and NRF24L01 the above code but dont have the RH_NRF24 library

    this is very new to me and my head is spinning at the moment, please help

    Ben

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.