Go Down

Topic: RF24 transmit problem (nRF24L01 module) (Read 1 time) previous topic - next topic

musaraf

Hello,

I'm trying to send data from an accelerometer (GY-80 module) connected to an Arduino Duemilanove, over a radio link (nRF24L01 module, using RF24 library), to an Arduino Uno. It works, but the connection is shaky, interrupts often, I receive only about one every five transmitted packets. I have tried using 9V batteries on both receiver and trasmitter (which used to solve the problems with nRF24), but this time it didn't help, so I guess the code is the problem.

On the transmitter:

Code: [Select]

// transmitter code

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

#define Register_ID 0
#define Register_2D 0x2D
#define Register_X0 0x32
#define Register_X1 0x33
#define Register_Y0 0x34
#define Register_Y1 0x35
#define Register_Z0 0x36
#define Register_Z1 0x37


const uint64_t pipe = 0xE8E8F0F0E1LL;
int ADXAddress = 0xA7 >> 1;  // the default 7-bit slave address
int reading = 0;
int val=0;
int X0,X1,X_out;
int Y0,Y1,Y_out;
int Z1,Z0,Z_out;
double Xg,Yg,Zg;

int dd[3];
int aa;
RF24 radio(9,10);



void setup()
{
  Wire.begin();               
  Serial.begin(9600);   
  delay(100);
  // enable to measute g data
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_2D);
  Wire.write(8);                //measuring enable
  Wire.endTransmission();     // stop transmitting
 
  radio.begin();
  radio.openWritingPipe(pipe);
 
  // optional
  // radio.setRetries(15,15);
  //radio.setPayloadSize(8);

}


void loop()
{
  //--------------X
  Wire.beginTransmission(ADXAddress); // transmit to device
  Wire.write(Register_X0);
  Wire.write(Register_X1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2)   
  {
    X0 = Wire.read();
    X1 = Wire.read();
    X1=X1<<8;
    X_out=X0+X1;   
  }

  //------------------Y
  Wire.beginTransmission(ADXAddress); // transmit to device
  Wire.write(Register_Y0);
  Wire.write(Register_Y1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2)   
  {
    Y0 = Wire.read();
    Y1 = Wire.read();
    Y1=Y1<<8;
    Y_out=Y0+Y1;
  }
  //------------------Z
  Wire.beginTransmission(ADXAddress); // transmit to device
  Wire.write(Register_Z0);
  Wire.write(Register_Z1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2)   
  {
    Z0 = Wire.read();
    Z1 = Wire.read();
    Z1=Z1<<8;
    Z_out=Z0+Z1;
  }
 
  Xg=X_out/256.0;
  Yg=Y_out/256.0;
  Zg=Z_out/256.0;
  Serial.print("X= ");
  Serial.print(X_out);
  Serial.print("       ");
  Serial.print("Y= ");
  Serial.print(Y_out);
  Serial.print("       ");
  Serial.print("Z= ");
  Serial.print(Z_out);
  Serial.println("  ");
  dd[0] = X_out;
  dd[1] = Y_out;
  dd[2] = Z_out;
  radio.write( dd, sizeof(dd) );
  delay(100);
 
}


On the receiver:

Code: [Select]

// receiver code

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

int dd[3];
int led = 7;


RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(void)
{
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
 
  // optional
  //radio.setRetries(15,15);
  //radio.setPayloadSize(8);

  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

void loop(void)
{
  if ( radio.available() )
  {
    // Dump the payloads until we've gotten everything
    bool done = false;
    while (!done)
    {
      // Fetch the payload, and see if this was the last one.
      done = radio.read( dd, sizeof(dd) );
 
        digitalWrite(led, HIGH);
        delay(10);
        digitalWrite(led, LOW);
 
      Serial.print(dd[0]);
      Serial.print(" ");
      Serial.print(dd[1]);
      Serial.print(" ");
      Serial.println(dd[2]);
    }
  }
  else
  {
    //Serial.println(".");
  }
}



Any help is appreciated.

crofter


Code: [Select]

  radio.write( dd, sizeof(dd) );



I would suggest you test the success of your radio.write as this shows the success at board to board level (the module acks the transmission) and doesn't depend on the receive program doing anything with the data.
Code: [Select]

bool ok = radio.write( dd, sizeof(dd) );
      if (ok)
        serial.println("ok\n\r");
      else
        serial.println("failed\n\r");


However, I originally had the same problem as you which was caused by the poor regulation of the 3.3v supply from the arduino boards. If you haven't already done it solder a capacitor (1uF to 10uF) directly across the supply pins on the nrf24l01 board.

Go Up