Control relay module using nRF24l01, with using push button as toggle switch

so I want to use push button, relay module and nRF24l01, what I want is, if I push button, make arduino think it's toggle and make LED ON/OFF. basic function without RF communication worked well, but implementing on 2 Arduino while using RF module makes me headache.

here's sample code I used for testing relay module

int relayPin = 6;
int buttonPin = 3;
boolean state = false;
void setup() {
  pinMode(relayPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP); 
 
}

void loop() {
  if(digitalRead(buttonPin)==LOW)
  {
    delay(200);                             
    state = !state;
    digitalWrite(relayPin, state);
  }

}

I got to separate those codes to make it working on 2 arduino(transmitting, receiving). what should I do?

If you read and, closely, follow Robin2's simple rf24 tutorial you should be able to get them working. That tutorial sure helped me. The code in the examples has been proven to work many many times. If it does not work for you, there is likely a hardware problem.

Run the CheckConnection.ino (look in reply #30 in the tutorial) to verify the physical wiring between the radio module and its processor (Arduino).

It is very important that you get the 3.3V supply to the radio modules to supply enough current. This is especially true for the high power (external antenna) modules. I use homemade adapters like these. They are powered by 5V and have a 3.3V regulator on the board. Robin2 also has suggested trying with a 2 AA cell battery pack.

If using the high powered radios make sure to separate them by a few meters. They may not work too close together. Try the lower power settings.

Reset the radios by cycling power to them after uploading new code. I have found that to help. They do not reset with the Arduino.

The TMRh24 version of the RF24 library is available for installation via the IDE program manager. How to install an Arduino library.

seems like it's bit off from what I want.... but I'll try to see all. wish I was good at English :frowning:

Use the tutorial to get the radios to communicate reliably, then add the code to do what you want.

Here are demo programs using the example code from Robin2's excellent tutorial, simple rf24. Relay state controlled by a push switch. The switch toggles the state of the mode variable using state change detection. If a change of state of the button pin is detected, a payload is sent to the receiver. If the state changes from high to low (active low) the mode flag is toggled. The receiver receives the payload and sets an LED, to represent the relay, per the mode value.

Sender code:


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

const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte buttonPin = 4;

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 100; 

void setup()
{
   Serial.begin(115200);
   Serial.println("Remote relay controller.");
   Serial.println("  Push on, push off");

   pinMode(buttonPin, INPUT_PULLUP);

   radio.begin();
   radio.setChannel(76);  //76 library default
   //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   radio.setPALevel(RF24_PA_HIGH);
   radio.setDataRate( RF24_250KBPS );
   radio.setRetries(3, 5); // delay, count
   radio.openWritingPipe(slaveAddress);
   radio.stopListening();
}

void loop()
{
   currentMillis = millis();
   if (currentMillis - prevMillis >= txIntervalMillis)
   {       
      sendOnButton();      
      prevMillis = millis();
   }
}

//====================

void sendOnButton()
{
   static bool mode = 0;
   static bool lastButtonState = 1;
   bool buttonState = digitalRead(buttonPin);   
   if (buttonState != lastButtonState)
   {      
      if (buttonState == 0)
      {
         mode = ! mode; // on/off
         Serial.print("  mode now >> ");
         Serial.println(mode);
      }
      lastButtonState = buttonState;
      // send on both edges, error detection?
      radio.write( &mode, sizeof(mode) );
   }
}

Edit: Added radio.stopListening(); to sender. Thanks @Whandall.

Receiver code:

// SimpleRx - the slave or the receiver

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

const byte CE_PIN = 9;
const byte CSN_PIN = 10;
const byte ledPin = 7;

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

bool newData = false;
bool buttonValues = 0;


//===========

void setup()
{

   Serial.begin(115200);
   Serial.println("Remote relay controller");

   pinMode(ledPin, OUTPUT);
  
   radio.begin();
   radio.setChannel(76);  //76 library default
   //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   radio.setPALevel(RF24_PA_HIGH);
   radio.setDataRate( RF24_250KBPS );
   radio.openReadingPipe(1, thisSlaveAddress);
   radio.startListening();
}
//=============

void loop()
{
   getData();
   showData();
   actOnData();
}

//==============

void getData()
{
   if ( radio.available() )
   {
      radio.read( &buttonValues, sizeof(buttonValues) );
      newData = true;
   }
}

void showData()
{
   if (newData == true)
   {
      Serial.print("Data received ^^ ");
      Serial.print("mode = ");
      Serial.println(buttonValues);
      Serial.println();
      //newData = false;
   }
}

void actOnData()
{
   if (newData == true)
   {
      digitalWrite(ledPin, buttonValues);
      newData = false;
   }
}

Tested, successfully, on real hardware.

1 Like

You should add a radio.stopListening(); to setup of the sender.

Thanks for pointing that out. I fixed it here and went through my stored examples and fixed those, too. Hopefully won't make that mistake again.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.