Send and receive Accelerometer Data from ADXL 335 through NRF24l01

The first and most difficult part is going to be getting the rf24 radios to communicate. Have you done that yet? If not, I suggest that you read and carefully follow Robin2's simple rf24 tutorial. That helped me a lot.

Here are examples that you may modify to do what you want. The sender will acquire 4 analog values from joysticks, put the values into a struct payload and transmit them. The other will receive the struct payload and separate those values.

Sender

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

const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

struct JoyValues
{
  int joy1x;
  int joy1y;
  int joy2x;
  int joy2y;
}joyValues;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second

const byte joy1xPin = A0;
const byte joy1yPin = A1;
const byte joy2xPin = A2;
const byte joy2yPin = A3;

void setup()
{
   Serial.begin(115200);
   Serial.println("SimpleTx Starting");
      
   radio.begin();
   radio.setChannel(76);  //76 library default
   //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   radio.setPALevel(RF24_PA_HIGH);
   radio.setDataRate( RF24_250KBPS );
   radio.setRetries(3, 5); // delay, count
   radio.openWritingPipe(slaveAddress);
}

void loop()
{
   currentMillis = millis();
   if (currentMillis - prevMillis >= txIntervalMillis)
   {
      send();
      Serial.print("JOY 1 X = ");
      Serial.print( joyValues.joy1x);
      Serial.print("  JOY 1 Y = ");
      Serial.print( joyValues.joy1y);
      Serial.print("  JOY 2 X = ");
      Serial.print( joyValues.joy2x);
      Serial.print("  JOY 2 Y = ");
      Serial.println( joyValues.joy2y);
      prevMillis = millis();
   }
}

//====================

void send()
{
   joyValues.joy1x = analogRead(joy1xPin);
   joyValues.joy1y = analogRead(joy1yPin);
   joyValues.joy2x = analogRead(joy2xPin);
   joyValues.joy2y = analogRead(joy2yPin);
   radio.write( &joyValues, sizeof(joyValues) );
}

Receiver:

// SimpleRx - the slave or the receiver

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

const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

struct JoyValues
{
  int joy1x;
  int joy1y;
  int joy2x;
  int joy2y;
}joyValues;

bool newData = false;

//===========
void setup()
{
   Serial.begin(115200);
   Serial.println("SimpleRx Starting");

   radio.begin();
   radio.setChannel(76);  //76 library default
   //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   radio.setPALevel(RF24_PA_HIGH);
   radio.setDataRate( RF24_250KBPS );
   radio.openReadingPipe(1, thisSlaveAddress);
   radio.startListening();
}

//=============

void loop()
{
   getData();
   showData();
}

//==============

void getData()
{
   if ( radio.available() )
   {
      radio.read( &joyValues, sizeof(joyValues) );
      newData = true;
   }
}

void showData()
{
   if (newData == true)
   {
      Serial.print("Data received >> ");
       Serial.print("JOY 1 X = ");
      Serial.print( joyValues.joy1x);
      Serial.print("  JOY 1 Y = ");
      Serial.print( joyValues.joy1y);
      Serial.print("  JOY 2 X = ");
      Serial.print( joyValues.joy2x);
      Serial.print("  JOY 2 Y = ");
      Serial.println( joyValues.joy2y);
      newData = false;
   }
}
1 Like