English
Español
PCBWAY PCB service

PCBWAY PCB service

PCBONLINE PCB service






Transmitter code

NRF24 + Arduino



You can download the NRF24 library here

To install it we just go to Sketch -> Include library and we open the .zip file that we've just downloaded.



Code!

Transmitter code!


/* 4 channels transmitter
 * Install the NRF24 library to your IDE
 * Upload this code to the Arduino UNO
 * Connect a NRF24 module to it:
 
    Module // Arduino UNO
    
    GND    ->   GND
    Vcc    ->   3.3V
    CE     ->   D9
    CSN    ->   D10
    CLK    ->   D13
    MOSI   ->   D11
    MISO   ->   D12

This code should send 4 channels 8 bits each
Please, like share and subscribe : https://www.youtube.com/c/ELECTRONOOBS
*/


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


const uint64_t pipeOut = 0xE8E8F0F0E1LL; //This same code should be in the receiver as well
RF24 radio(9, 10); //select CE and CSN pins


//We can have up to 32 channels of 8 bits
struct MyData {
  byte throttle; //We define each byte to an analog input
  byte yaw;
  byte pitch;
  byte roll;
};
MyData data;

void resetData() 
{//We define the start value of our data
  data.throttle = 0;
  data.yaw = 127;
  data.pitch = 127;
  data.roll = 127;
}


void setup()
{
  radio.begin();
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);
  radio.openWritingPipe(pipeOut);
  resetData();
}

/**************************************************/



// We map the values from 0-1024 a 0-255, 
//because we have 8 bits channels and 8 bits = 254,
//once we receive the values in the receiver we can map the values once again so don't worry

int mapJoystickValues(int val, int lower, int middle, int upper, bool reverse)
{
  val = constrain(val, lower, upper);
  if( val < middle )
    val = map(val, lower, middle, 0, 128);
  else
    val = map(val, middle, upper, 128, 255);
  return ( reverse ? 255 - val : val );
}

void loop()
{
  // We read the analog input values
   //Set to "true" it will invert the value reading
    //Set to "false" it will use values from 0 to 1024
     //This part is just in case you connect the pontenciometers reversing the wires
      //and to adjust the values 
  data.throttle = mapJoystickValues( analogRead(0), 13, 524, 1015, true );
  data.yaw      = mapJoystickValues( analogRead(1),  1, 505, 1020, true );
  data.pitch    = mapJoystickValues( analogRead(2), 12, 544, 1021, true );
  data.roll     = mapJoystickValues( analogRead(3), 34, 522, 1020, true );
  radio.write(&data, sizeof(MyData));//Sending the data

  

}

Download the Transmitter sketch here