Nrf24l01 wireless relay problem!

Hi everyone,

i have a little problem that i am trying to overcome since a month.

i am using two arduino nano’s
2 nrf24l01 chips with 3.3 regulator board
on the reciever end i have 4 Leds connected to the breadboard through pins 4,5,6,7.
and on the transmitter end 4 buttons connected to the second nrf

i have tried to compile a program which is intended to switch on a relay when i press a button and should shut the relay when i release the button .

when i push the button it says button one pressed on the serial monitor and the led remains on even if i release the button. Same is the case with other buttons and if i push all the buttons at once, all the leds start to flicker. please help me… please find the attached file and look at the code…

can i edit the transmitter part with

if (digitalRead(Button4) == LOW)
{
Serial.println(“Button 4 pressed”);
DataMsg[0] = 4;
radio.write(DataMsg, 1);
else

DataMsg[0] = 4;
radio.write(DataMsg, 0);
}

(i tried it in the code now with else command in all the relays but it comes to the above commands and give errors.)

(I have also checked the nrf24l01 connections by uploading two way chat program ,available on the web, and it works as it should)

Please help me out …

final_transmitter.ino (1.11 KB)

final_reciever.ino (1.33 KB)

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

...R

i did looked at this forum when i was starting with this project. it was really helpful and then as far as the first example is concerned i did checked the communication between the two modules and they were sending and recieveing the data as they should.

Now that I know the wireless works I have had a look at your code.

This is wrong in a few areas

if (digitalRead(Button4) == LOW)
  {
    Serial.println("Button 4 pressed");
    DataMsg[0] = 4;
    radio.write(DataMsg, 1);
else 

    DataMsg[0] = 4;
    radio.write(DataMsg, 0);
  }

In no particular order

radio.write(DataMsg, 0);

should be

radio.write(DataMsg, sizeof(DataMsg);

Using '0' is telling it to send no data. Look how it is done in my examples.

This

DataMsg[0] = 4;

sets the value that is sent.
it needs to be different when the button is pressed and when it is not pressed

And, in general, the code seems to send a continuous stream of message all the time that the button is either pressed or releases. Too many messages will swamp the system. Five message per second should be ample to give a responsive system.

...R

thanks a lot .... ill try it tommorow and will get back to you. thamks again for helping me out. (will update you in any case)

which is intended to switch on a relay when i press a button and should shut the relay when i release the button .

@Robin2

And, in general, the code seems to send a continuous stream of message all the time that the button is either pressed or releases. Too many messages will swamp the system. Five message per second should be ample to give a responsive system.

One way to do this is to work with the button transitions, the moment it becomes pressed and the moment it becomes released. I find using the Bounce2 library with the .fell() and .rose() functions is very convenient to achieve this.

This might give you some ideas

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

int DataMsg[1];

RF24 radio(9,10);

const uint64_t pipe = 0xE14BC8F482LL;

#include <Bounce2.h>
const byte button1 = 4;
const byte button2 = 5;
Bounce button_1 = Bounce();
Bounce button_2 = Bounce();

void setup() {
  Serial.begin(115200);
  
  pinMode(button1, INPUT_PULLUP); 
  button_1.attach(button1);
  button_1.interval(25);
  pinMode(button2, INPUT_PULLUP); 
  button_2.attach(button2);
  button_2.interval(25);
}

void loop() {
  if (button_1.update())
  {
    if (button_1.fell()) //button pressed, state change to LOW
    {
      Serial.println("Button 1 pressed");
      DataMsg[0] = 1;
      radio.write(DataMsg, 1);
    }   
    else if (button_1.rose()) //button released state change to HIGH
    {
      Serial.println("Button 1 released");
      DataMsg[0] = 5;//pick a unique message for button 1 released
      radio.write(DataMsg, 1);     
    }
  }

   if (button_2.update())
  {
    if (button_2.fell()) //button pressed, state change to LOW
    {
      Serial.println("Button 2 pressed");
      DataMsg[0] = 2;
      radio.write(DataMsg, 1);
    }   
    else if (button_2.rose()) //button released state change to HIGH
    {
      Serial.println("Button 2 released");
      DataMsg[0] = 6;//pick a unique message for button 2 released
      radio.write(DataMsg, 1);     
    }
  }
}

cattledog:
One way to do this is to work with the button transitions,

With a wireless system it can be useful to send data at regular intervals even if the values have not changed as that provides a means for the receiver to recognize when there is a communication problem.

...R

thanks again, have been very busy since a few days, did'nt even get a chance to open arduino window. will update you day after tommorrow.

hey robbin, tried what you said. transmitter side is working as it should(when i coonect the pin to my led ie pin no.4,5,6,7) it says button 1,2,3,4 pressed.

but on the reciever side it says Waiting for signal…
NRF24L01 Receiver: 1
Waiting for signal…
Waiting for signal…
NRF24L01 Receiver: 3
Waiting for signal…
NRF24L01 Receiver: 2
Waiting for signal…
Waiting for signal…
NRF24L01 Receiver: 4
etc

without connecting any wire to the transmitter.

the code is as follows:-

//Transmitter Code

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

int DataMsg[1];

RF24 radio(9,10);

const uint64_t pipe = 0xE14BC8F482LL;

int Button1 = 4;
int Button2 = 5;
int Button3 = 6;
int Button4 = 7;

void setup()
{
  
  pinMode(4, INPUT);
  digitalWrite(4,HIGH);
  pinMode(5, INPUT);
  digitalWrite(5,HIGH);
  pinMode(6, INPUT);
  digitalWrite(6,HIGH);
  pinMode(7, INPUT);
  digitalWrite(7,HIGH);

  Serial.begin(57600);
  Serial.println("NRF24L01 Transmitter");

  radio.begin();
  radio.openWritingPipe(pipe);
}

void loop()

{
  
  if (digitalRead(Button1) == LOW)
  {
    Serial.println("Button 1 pressed");
    DataMsg[0] = 1;
    radio.write(DataMsg, 1);
    
  }
  else
  { 
    DataMsg[0] = 1;
    radio.write(DataMsg, sizeof(DataMsg));
    delay(200);
  }
  if (digitalRead(Button2) == LOW)
  {
    Serial.println("Button 2 pressed");
    DataMsg[0] = 2;
    radio.write(DataMsg, 1);
    
  }
    else
    {
    DataMsg[0] = 2;
    radio.write(DataMsg, sizeof(DataMsg));
    delay(200);
  }
  if (digitalRead(Button3) == LOW)
  {
    Serial.println("Button 3 pressed");
    DataMsg[0] = 3;
    radio.write(DataMsg, 1);
    
  }
    else
  {
    DataMsg[0] = 3;
    radio.write(DataMsg, sizeof(DataMsg));
    delay(200);
  }
  if (digitalRead(Button4) == LOW)
  {
    Serial.println("Button 4 pressed");
    DataMsg[0] = 4;
    radio.write(DataMsg, 1);
    
  }
    else
    {
    DataMsg[0] = 4;
    radio.write(DataMsg, sizeof(DataMsg));
    delay(200);
  }
  delay(500);
}
//Receiver Code


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

int DataMgs[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE14BC8F482LL;
int Relay1 = 4;
int Relay2 = 5;
int Relay3 = 6;
int Relay4 = 7;

void setup()
{
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);
  Serial.begin(57600);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
}

void loop()

{
  
  if (radio.available())
  {
    bool done = false;    
    while(radio.available()){
  radio.read(DataMgs, 1);
}
    {

      Serial.print("NRF24L01 Receiver: ");    
      Serial.println(DataMgs[0]);
      
      if (DataMgs[0] == 1)
      {
        delay(10);
        digitalWrite(Relay1, LOW);
      }
      else
      digitalWrite(Relay1, HIGH);
      }

      if (DataMgs[0] == 2)
      {
        delay(10);
        digitalWrite(Relay2, LOW);
      }
      else
      digitalWrite(Relay2, HIGH);
      }

      if (DataMgs[0] == 3)
      {
        delay(10);
        digitalWrite(Relay3, LOW);
      }
      else
      digitalWrite(Relay3, HIGH);

      if (DataMgs[0] == 4)
      {
        delay(10);
        digitalWrite(Relay4, LOW);
      }
      else
      digitalWrite(Relay4, HIGH);
      
  {

Serial.println("Waiting for signal...");
  }
  delay(1000);
}

My intentions for this project are as follows:-

(1) Relay switch (4 channel relay)

(2) Water level meter

if i havent mentioned before.

webaddic:
without connecting any wire to the transmitter.

That makes no sense at all. What wire? Why would it be necessary to connect a wire to a wireless transmitter?

In you Tx program you still have this line repeated several times - once is plenty.

radio.write(DataMsg, 1);

Try to avoid repeating code in a program. It just creates scope for silly errors.

In your RX program you seen to have made a dog's breakfast of your { and }. Use the AutoFormat tool to indent your code consistently and the problems should be apparent.

...R

Robin2:
That makes no sense at all. What wire? Why would it be necessary to connect a wire to a wireless transmitter?
...

probably meant an antenna.

BabyGeezer:
probably meant an antenna.

I doubt it. The simple nRF24s have a PCB antenna and the external antenna (for the high power modules) does not resemble a wire.

...R

hey robbin thanks for the reply again… will try the tips you give me tommorow… have again changed the code … will upload to nano tommorow… and by wires i meant wires from d4,d5,d6,and d7 pins.

by the way i tried a few programs from the internet too. programs are working fine if i switch one led on and off at a time. but if i try to light-up all the leds at the same time all the leds light up …but when i release the switch one by one, they stay lit up. also attaching the other code. please have a look.

//Transmitter


#include  <SPI.h>                     
#include  "RF24.h"                   
byte data[1];                         
const uint64_t pipe = 0xF0F0F0F0A1LL; 
RF24 radio(9,10);                     

void setup(){
 pinMode(4, INPUT_PULLUP);            
 pinMode(5, INPUT_PULLUP);            
 pinMode(6, INPUT_PULLUP);            
 pinMode(7, INPUT_PULLUP);
 radio.begin();                       
 radio.openWritingPipe(pipe);         
}

void loop(){
 if(digitalRead(4)==HIGH || digitalRead(5)==HIGH || digitalRead(6)==HIGH || digitalRead(7)==HIGH){
  data[0]=0;
 }
 if(digitalRead(4)==LOW){
  data[0]=1;
 }
 if(digitalRead(5)==LOW){
  data[0]=2;
 }
 if(digitalRead(6)==LOW){
  data[0]=3;
  }
 if(digitalRead(7)==LOW){
  data[0]=4;
 }
 radio.write(data, 1);                 
}
//Reciever

#include <SPI.h>                       
#include "RF24.h"                      
byte data[1];                          
boolean var;                           
const uint64_t pipe = 0xF0F0F0F0A1LL;  
RF24 radio(9,10);                      

void setup(){
 pinMode(4, OUTPUT);                    
 pinMode(5, OUTPUT);                    
 pinMode(6, OUTPUT);                    
 pinMode(7, OUTPUT);
 radio.begin();                         
 radio.openReadingPipe(1,pipe);         
 radio.startListening();                
}

void loop(){
 if(radio.available()){
   
   radio.read(data, 1);
      if(data[0] == 0){
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      }
      if(data[0] == 1){
      digitalWrite(4, HIGH);
      }
      if(data[0] == 2){
      digitalWrite(5, HIGH);
      }
      if(data[0] == 3){
      digitalWrite(6, HIGH);
      }
      if(data[0] == 4){
      digitalWrite(7, HIGH);
   }
  }
 }

webaddic:
and by wires i meant wires from d4,d5,d6,and d7 pins.

That means something to you but not to us.

What are the wires for?

In your program you have code like this

digitalWrite(4, LOW);

It would be much easier to understand your program if you give meaningful names to the variables. For example (I have just invented names for illustration)

byte redLedPin = 4;


digitalWrite(redLedPin, LOW);

…R

ok thanks, will do so.. i am still a beginner in the programming world. sorry for my immature mistakes.

webaddic:
ok thanks, will do so.. i am still a beginner in the programming world. sorry for my immature mistakes.

There is nothing to apologise for.

...R

"Update"

created pcb for the project. the vero board one was confusing. will upload the pictures in a day.