Go Down

Topic: NRF24L01: Communication works, program doesn't (Read 62 times) previous topic - next topic

eliaselias1

Hello,

I'm trying to run a program via a NRF24L01 wireless 2.4GHz connection. The connection certainly works; printing a message from tx to rx on the serial monitor works. But the output on rx side does not come high...

I'm running Uno boards on both sides, the outputs do work on the rx side as i tested this with a simple program on the board itself.


Code on tx-side
Code: [Select]
#include <SPI.h>
#include <RF24.h>
RF24 radio(9, 10);       
const byte address[6] = "00001";
int button_pin = 2;
boolean button_state = 0;

void setup() {
pinMode(button_pin, INPUT);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MAX);
radio.stopListening();
}

void loop(){
button_state = digitalRead(button_pin);
if(button_state == HIGH)
{
const char text[] = "HIGH";
radio.write(&text, sizeof(text));
}
else
{
const char text[] = "LOW";
radio.write(&text, sizeof(text));
}
radio.write(&button_state, sizeof(button_state));
delay(1000);
}



Code on rx-side
Code: [Select]
#include <SPI.h>
#include <RF24.h>
RF24 radio(9, 10);
const byte address[6] = "00001";
int led_pin = 3;
boolean button_state = 0;

void setup() {
pinMode(led_pin, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
}

void loop(){
if (radio.available())
{
char text[32] = "";
radio.read(&text, sizeof(text));
radio.read(&button_state, sizeof(button_state));
if(button_state == HIGH)
{
digitalWrite(led_pin, HIGH);
Serial.println(text);
}
else
{
digitalWrite(led_pin, LOW);
Serial.println(text);}
}
delay(5);
}



Danois90

#1
Oct 28, 2020, 12:20 pm Last Edit: Oct 28, 2020, 01:38 pm by Danois90
On the RX side, you read up to 32 bytes into "text", this may include the button state, so "button_state" may never be received properly. All you need to send is the button state, the text is not required for anything since you can check the received state on the RX side.

Another issue is that the LED on the RX side only goes on for 5 milliseconds (due to the delay) and then it goes off again and you will not be able to see such a short blink. You should only change the state of the LED when a button state is received and you should only send a button state when it changes.
Instead of mocking what's wrong, teach what's right! ;)
When you get help, remember to thank the helper and give some karma!
Please, do NOT send me any private messages!!

Robin2

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

There is also a connection test program to check that the Arduino can talk to the nRF24 it is connected to. If the first example does not work be sure to try the connection test for both of your Arduinos. Nothing will work if the connection test fails.

A common problem with nRF24 modules is insufficient 3.3v current from the Arduino 3.3v pin. This seems to be a particular problem with the nano. The high-power nRF24s (with the external antenna) will definitely need an external power supply. At least for testing try powering the nRF24 with a pair of AA alkaline cells (3v) with the battery GND connected to the Arduino GND.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up