i'm trying to read two analog inputs and transmit them with a nRF24L01 using the RF24 and Nrf24L01 libraries.
on another arduino, i have the same RF chip and i'm trying to print the received data to the serial and display on the screen.
i got everything working and responding EXCEPT! the data is incorrect.
when i attach A1 to 3.3v of the Arduino, i get the number 667 on the transmitting arduino (this is good) but on the receiving arduino i get 832 (4.01v).
this is the TX code:--------------------------------------------------------------------------------------------------------------
/* YourDuinoStarter Example: nRF24L01 Transmit Joystick values
- WHAT IT DOES: Reads Analog values on A0, A1 and transmits
them over a nRF24L01 Radio Link to another transceiver.
- SEE the comments after "//" on each line below
- CONNECTIONS: nRF24L01 Modules See:
http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
1 - GND
2 - VCC 3.3V !!! NOT 5V
3 - CE to Arduino pin 9
4 - CSN to Arduino pin 10
5 - SCK to Arduino pin 13
6 - MOSI to Arduino pin 11
7 - MISO to Arduino pin 12
8 - UNUSED
-
Analog Joystick or two 10K potentiometers:
GND to Arduino GND
VCC to Arduino +5V
X Pot to Arduino A0
Y Pot to Arduino A1
- V1.00 11/26/13
Based on examples at http://www.bajdi.com/
Questions: terry@yourduino.com */
/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
int JOY1 = A0;
int JOY2 = A1;
int JOY1VAL = 0;
int JOY2VAL = 0;
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
short joystick[2]; // 2 element array holding Joystick readings
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
JOY1VAL = analogRead(JOY1);
delay(20);
JOY2VAL = analogRead(JOY2);
delay(20);
joystick[0] = JOY1VAL;
joystick[1] = JOY2VAL;
radio.write( joystick, sizeof(joystick) );
Serial.print(joystick[0]);
Serial.print(" ");
Serial.println(joystick[1]);
delay(100);
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
//NONE
//*********( THE END )***********
this is the RX code:------------------------------------------------------------------------------------------------------------
/* YourDuinoStarter Example: nRF24L01 Receive Joystick values
- WHAT IT DOES: Receives data from another transceiver with
2 Analog values from a Joystick or 2 Potentiometers
Displays received values on Serial Monitor
- SEE the comments after "//" on each line below
- CONNECTIONS: nRF24L01 Modules See:
http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
1 - GND
2 - VCC 3.3V !!! NOT 5V
3 - CE to Arduino pin 9
4 - CSN to Arduino pin 10
5 - SCK to Arduino pin 13
6 - MOSI to Arduino pin 11
7 - MISO to Arduino pin 12
8 - UNUSED
- V1.00 11/26/13
Based on examples at http://www.bajdi.com/
Questions: terry@yourduino.com */
/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int joystick[2]; // 2 element array holding Joystick readings
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
delay(1000);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();;
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
if ( radio.available() )
{
// Read the data payload until we've received everything
bool done = false;
while (!done)
{
// Fetch the data payload
done = radio.read( joystick, sizeof(joystick) );
Serial.print("X = ");
Serial.print(joystick[0]);
Serial.print(" Y = ");
Serial.println(joystick[1]);
}
}
else
{
//Serial.println("No radio available");
}
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
//NONE
//*********( THE END )***********
forgot to mention that when i force the variable to a value of 667 instead of analog read it works fine.
BUT! when i look at the serial of the transmitting arduino, i see that the values are correct while using the analog read.
what is going on?
That chip needs the packet length programmed into it, or else dynamic packet lengths
enabled - you've done neither so the library defaults the packet size to 32 bytes as it
happens.
Then when you come to read the payload you keep reading from the packet over and
over till its exhausted, overwriting your buffer several times (8 I think).
Use setPayloadSize(sizeof(joystick)) at both transmit and receive sides, or use
a buffer of length 32 in your code.
thanks
the problem is that i dont know what is the packet length.
some time it will be "14" and some times "1021"..
how can i solve this? should i declare it every time i send the packet?
what about the receiving side.. there is no way to know in advance
.Then set buffer 32 (which is defalut).
and send info in chunks of 32bytes.
(if you somtimes send less - thats OK - those extra bytes contains 'empty space')
To inform receiver of valid bytes sent, use e.g. byte 0 to tell nbr of valid bytes in 'this' packet.
..several other ways are also possible..
Hey guys
I have been working along the same lines with the same radios and have a few questions. below is my code
Tx Side
/*
1 - GND
2 - VCC 3.3V !!! NOT 5V
3 - CE to Arduino pin 9
4 - CSN to Arduino pin 10
5 - SCK to Arduino pin 13
6 - MOSI to Arduino pin 11
7 - MISO to Arduino pin 12
8 - UNUSED
/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
int Val0 = 255;
int Val1 = 1;
int Val2 = 1;
int Val3 = 1;
int Val4 = 1;
int Val5 = 1;
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
short Array[6]; // 2 element array holding Joystick readings
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(57600);
radio.begin();
radio.openWritingPipe(pipe);
radio.setPayloadSize(32);
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
delay(20);
Array[0] = Val0;
Array[1] = Val1;
Array[2] = Val2;
Array[3] = Val3;
Array[4] = Val4;
Array[5] = Val5;
radio.write( Array, sizeof(Array) );
Serial.print(Array[0]);
Serial.print(" ");
Serial.print(Array[1]);
Serial.print(Array[2]);
Serial.print(" ");
Serial.print(Array[3]);
Serial.print(Array[4]);
Serial.print(" ");
Serial.println(Array[5]);
delay(10);
}//--(end main loop )---
Rx side
/* 1 - GND
2 - VCC 3.3V !!! NOT 5V
3 - CE to Arduino pin 9
4 - CSN to Arduino pin 10
5 - SCK to Arduino pin 13
6 - MOSI to Arduino pin 11
7 - MISO to Arduino pin 12
8 - UNUSED
/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int Array[6]; // 2 element array holding Joystick readings
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(57600);
delay(500);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();;
radio.setPayloadSize(32);
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
if ( radio.available() )
{
// Read the data payload until we've received everything
bool done = false;
while (!done)
{
// Fetch the data payload
done = radio.read( Array, sizeof(Array) );
Serial.print("Slot0 = ");
Serial.print(Array[0]);
Serial.print(" Slot1 = ");
Serial.print(Array[1]);
Serial.print(" Slot2 = ");
Serial.print(Array[2]);
Serial.print(" Slot3 = ");
Serial.print(Array[3]);
Serial.print(" Slot4 = ");
Serial.print(Array[4]);
Serial.print(" Slot5 = ");
Serial.println(Array[5]);
delay(10);
}
}
else
{
//Serial.println("No radio available");
}
}
//*********( THE END )***********
this code works great I get all info sent over with no problem.
My Problem is with the range and reliability. the on-board antenna seems to work but it almost acts like a patch antenna. if you point them towards each other I get 20 meters. let both uno's just lie flat on the table the range is maybe 3-5 meters. it was my understanding that these units range far out performs the cheep 433mhx Virtual wire units ( 90 meter range).
how can i get them to this range ? the defualt baud rate is 1mbps how do you define a lower buad rate with this library ?
Rustie0125:
how can i get them to this range ? the defualt baud rate is 1mbps how do you define a lower buad rate with this library ?
My experience with the non-amplified transceivers is that the effective range is meters rather than tens of meters. Perhaps you could achieve 90 meters with direct line of sight under ideal conditions but it seems very optimistic to me. Given that the aerials are not symmetrical I'd expect them to work better in some directions - if your transceivers are fixed then orienting them correctly could help increase the range.
The RF24 library comes with examples that show how to configure the speed, number of retransmission attempts and delay between retransmission attempts for maximum reliability.