Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15404

NRF24 Modules Not Working

$
0
0

I needed to get wireless communication for a project, so I tried to use the NRF24L01 and NRF24L01+PA+LNA modules. I connected them according to the schematic in this project hub article: https://projecthub.arduino.cc/tmekinyan/how-to-use-the-nrf24l01-module-with-arduino-813957

I also uploaded the code provided in the article

This is the code for the transmitter (NRF24L01+PA+LNA):

#include "SPI.h" 
#include "RF24.h" 
#include "nRF24L01.h" 
#define CE_PIN 9 
#define CSN_PIN 10 
#define INTERVAL_MS_TRANSMISSION 250 
RF24 radio(CE_PIN, CSN_PIN); 
const byte address[6] = "00001"; 
//NRF24L01 buffer limit is 32 bytes (max struct size) 
struct payload { 
	 byte data1; 
	 char data2; 
}; 
payload payload; 
void setup() 
{ 
	 Serial.begin(115200); 
	 radio.begin(); 
	 //Append ACK packet from the receiving radio back to the transmitting radio 
	 radio.setAutoAck(false); //(true|false) 
	 //Set the transmission datarate 
	 radio.setDataRate(RF24_250KBPS); //(RF24_250KBPS|RF24_1MBPS|RF24_2MBPS) 
	 //Greater level = more consumption = longer distance 
	 radio.setPALevel(RF24_PA_MAX); //(RF24_PA_MIN|RF24_PA_LOW|RF24_PA_HIGH|RF24_PA_MAX) 
	 //Default value is the maximum 32 bytes 
	 radio.setPayloadSize(sizeof(payload)); 
	 //Act as transmitter 
	 radio.openWritingPipe(address); 
	 radio.stopListening(); 
} 
void loop() 
{ 
	 payload.data1 = 123; 
	 payload.data2 = 'x'; 
	 radio.write(&payload, sizeof(payload)); 
	 Serial.print("Data1:"); 
	 Serial.println(payload.data1); 
	 Serial.print("Data2:"); 
	 Serial.println(payload.data2); 
	 Serial.println("Sent"); 
	 delay(INTERVAL_MS_TRANSMISSION); 
}

And this is the code for the receiver (NRF24L01):

#include "SPI.h" 
#include "RF24.h" 
#include "nRF24L01.h" 
#define CE_PIN 9 
#define CSN_PIN 10 
#define INTERVAL_MS_SIGNAL_LOST 1000 
#define INTERVAL_MS_SIGNAL_RETRY 250 
RF24 radio(CE_PIN, CSN_PIN); 
const byte address[6] = "00001"; 
//NRF24L01 buffer limit is 32 bytes (max struct size) 
struct payload { 
	 byte data1; 
	 char data2; 
}; 
payload payload; 
unsigned long lastSignalMillis = 0; 
void setup() 
{ 
	 Serial.begin(115200); 
	 radio.begin(); 
	 //Append ACK packet from the receiving radio back to the transmitting radio 
	 radio.setAutoAck(false); //(true|false) 
	 //Set the transmission datarate 
	 radio.setDataRate(RF24_250KBPS); //(RF24_250KBPS|RF24_1MBPS|RF24_2MBPS) 
	 //Greater level = more consumption = longer distance 
	 radio.setPALevel(RF24_PA_MIN); //(RF24_PA_MIN|RF24_PA_LOW|RF24_PA_HIGH|RF24_PA_MAX) 
	 //Default value is the maximum 32 bytes1 
	 radio.setPayloadSize(sizeof(payload)); 
	 //Act as receiver 
	 radio.openReadingPipe(0, address); 
	 radio.startListening(); 
} 
void loop() 
{ 
	 unsigned long currentMillis = millis(); 
	 if (radio.available() > 0) { 
	   radio.read(&payload, sizeof(payload)); 
	   Serial.println("Received"); 
	   Serial.print("Data1:"); 
	   Serial.println(payload.data1); 
	   Serial.print("Data2:"); 
	   Serial.println(payload.data2); 
	   lastSignalMillis = currentMillis; 
	 } 
	 if (currentMillis - lastSignalMillis > INTERVAL_MS_SIGNAL_LOST) { 
	   lostConnection(); 
	 } 
} 
void lostConnection() 
{ 
	 Serial.println("We have lost connection, preventing unwanted behavior"); 
	 delay(INTERVAL_MS_SIGNAL_RETRY); 
}

When it's working, the receiver code should be able to print the data values '123' and 'x' to the serial monitor. However, when I upload the codes and turn on the serial monitor, it either says that the connection has been lost or it prints the data values as '0' and some sort of square symbol.

Has anyone encountered a similar issue to this and have an idea of how to get this transmitter/receiver system to work?

3 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 15404

Trending Articles