I am trying to communicate between two arduino using nrf24L01. I am using nrf24L01 first time. its not working. i am not getting what may be the problem.
//*****************************Arduino Code for Transmitter***********************
//This sketch is from a tutorial video on the ForceTronics YouTube Channel. The tutorial discusses how to build a
//shield and a prototyping board for the nRF24L01 Transceiver Module.
//the code was leverage from Ping pair example at http://tmrh20.github.io/RF24/pingpair_ack_8ino-example.html
//This sketch is free to the public to use and modify at your own risk
#include <SPI.h> //Call SPI library so you can communicate with the nRF24L01+
#include <nRF24L01.h> //nRF2401 libarary found at https://github.com/tmrh20/RF24/
#include <RF24.h> //nRF2401 libarary found at https://github.com/tmrh20/RF24/
const int pinCE = 9; //This pin is used to set the nRF24 to standby (0) or active mode (1)
const int pinCSN = 10; //This pin is used to tell the nRF24 whether the SPI communication is a command or message to send out
RF24 wirelessSPI(pinCE, pinCSN); // Create your nRF24 object or wireless SPI connection
const uint64_t pAddress = 0xB00B1E5000LL; // Radio pipe addresses for the 2 nodes to communicate.
void setup()
{
Serial.begin(57600); //start serial to communicate process
wirelessSPI.begin(); //Start the nRF24 module
wirelessSPI.setAutoAck(1); // Ensure autoACK is enabled so rec sends ack packet to let you know it got the transmit packet payload
wirelessSPI.setRetries(5,15); // Sets up retries and timing for packets that were not ack'd, current settings: smallest time between retries, max no. of retries
wirelessSPI.openWritingPipe(pAddress); // pipe address that we will communicate over, must be the same for each nRF24 module
wirelessSPI.stopListening();
}
void loop()
{
byte t = 145; //analogRead(0);//note that we can cast the ADC value to a byte because we know the temp sensor is not going to return a value higher than 255
if (!wirelessSPI.write(&t, 1 )){ //if the send fails let the user know over serial monitor
Serial.println("packet delivery failed");
}
delay(1000);
}
Receiver Code:
//This sketch is from a tutorial video on the ForceTronics YouTube Channel. The tutorial discusses how to build a
//shield and a prototyping board for the nRF24L01 Transceiver Module.
//the code was leverage from Ping pair example at http://tmrh20.github.io/RF24/pingpair_ack_8ino-example.html
//This sketch is free to the public to use and modify at your own risk
#include <SPI.h> //Call SPI library so you can communicate with the nRF24L01+
#include <nRF24L01.h> //nRF2401 libarary found at https://github.com/tmrh20/RF24/
#include <RF24.h> //nRF2401 libarary found at https://github.com/tmrh20/RF24/
const int pinCE = 9; //This pin is used to set the nRF24 to standby (0) or active mode (1)
const int pinCSN = 10; //This pin is used to tell the nRF24 whether the SPI communication is a command or message to send out
byte bVal; //used to store ADC value payload from transmit module, the ADC value will be < 256 so it will fit in a byte
RF24 wirelessSPI(pinCE, pinCSN); // Declare object from nRF24 library (Create your wireless SPI)
const uint64_t pAddress = 0xB00B1E5000LL; //Create a pipe addresses for the 2 nodes to communicate over, the "LL" is for LongLong type
void setup()
{
Serial.begin(57600); //start serial to communicate process
wirelessSPI.begin(); //Start the nRF24 module
wirelessSPI.setAutoAck(1); // Ensure autoACK is enabled, this means rec send acknowledge packet to tell xmit that it got the packet with no problems
wirelessSPI.openReadingPipe(1,pAddress); //open pipe o for recieving meassages with pipe address
wirelessSPI.startListening(); // Start listening for messages
}
void loop()
{
//loop until all of the payload data is recieved, for this example loop should only run once
if (!wirelessSPI.available())
{
Serial.println("No Radio");
delay(1000);
}
while(wirelessSPI.available()){
wirelessSPI.read( &bVal, 1 ); //read one byte of data and store it in bVal variable
Serial.print("Temperature at transmitter is ");
Serial.print(calculateTempF(calculateArduinoVolt(bVal))); //convert the ADC value to a voltage value and than to a temperature value in F
Serial.println(" F");
}
delay(200);
}
//this function calculates temp in F from TMP36 temp sensor
float calculateTempF(float v1) {
float temp = 0;
//calculate temp in C, .75 volts is 25 C. 10mV per degree
if (v1 < .75) { temp = 25 - ((.75-v1)/.01); } //if below 25 C
else if (v1 == .75) {temp = 25; }
else { temp = 25 + ((v1 -.75)/.01); } //if above 25
//convert to F
temp =((temp*9)/5) + 32;
return temp;
}
//This function takes an Arduino analog pin reading and converts it to a voltage value
float calculateArduinoVolt(int val) {
float volt = (float)val * (5.0 / 1023.0); //convert ADC value to voltage
return volt;
}
wiring in both side is as attachement
I have use arduino uno in receiver side and neno in transmitter side but wiring in both are same
I've not read through your code or tried it but I had issues transmitting and receiving even with the sample codes spent ages trying to find out why and did lots of research. Thinking everyone else has got them working from the samples from the Tutorial posted above. I was using a uno has the transmitter and a nano as the receiver. The uno was uploaded via usb and the nano was programmed by the ISCP with usbasp programmer . After it was doing my head I found out that the error was caused by me leaving the ISCP connector still been connected through the usbasp programmer. Removed and it started working perfectly. How are your units connected ?
If you go to the web site 123splat posted you will find your answer there here is a little quote from there
These instability problems are particularly noticeable when 3.3V power comes from a UNO, MEGA, Nano etc. that has only 50 ma of 3.3V power available.
Yes they only draw 13ma but the spike current can be a lot higher on Tx if you don't put a 10uF across the power pins like that mentioned on that website you will never get them working. You could have a faulty unit may be
Thank you robin. that works for me.
First it worked but it shows data sometime and sometime not. then i used buckconverter to supply nrf from arduino 5v by converting it to 3.3v. now its working perfectly. i have also used 10mfd direcly soldered on nrf module. Thank you once again
one thing i also marked that nrf should be supplied with good quality wire, bad quality wire drop voltage at end and should be soldered to pin directly, connector in supply may cause problems and confusions too
This simple code is working fine. Now i want both unit works as transmitter and receiver also. how can i merge both code in a single arduino, in which it stay in receiver mode and can be made transmitter whenever i need.
SimpleTx.ino
// SimpleTx - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte slaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataToSend[10] = "Message 0";
char txNum = '0';
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
void setup() {
Serial.begin(9600);
Serial.println("SimpleTx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5); // delay, count
radio.openWritingPipe(slaveAddress);
}
//====================
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}
}
//====================
void send() {
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
Serial.print("Data Sent ");
Serial.print(dataToSend);
if (rslt) {
Serial.println(" Acknowledge received");
updateMessage();
}
else {
Serial.println(" Tx failed");
}
}
//================
void updateMessage() {
// so you can see that new data is being sent
txNum += 1;
if (txNum > '9') {
txNum = '0';
}
dataToSend[8] = txNum;
}
SimpleRx.ino
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN);
char dataReceived[10]; // this must match dataToSend in the TX
bool newData = false;
//===========
void setup() {
Serial.begin(9600);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
}
//=============
void loop() {
getData();
showData();
}
//==============
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
}
}
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(dataReceived);
newData = false;
}
}
vijaysurat:
This simple code is working fine. Now i want both unit works as transmitter and receiver also. how can i merge both code in a single arduino,
Have a look at the other examples in my Tutorial that I linked to in Reply #1