relay control problem, I am sure its something simple!!! newbie!

Hi all

I am a novis so please go easy on me.

I have successfully achieved communication between two arduino uno's with the NRF24l01 modules.
I can send a value of either 1 or 0, 1 should turn a relay on and 0 turn it off on pin 8.

when I read the serial data being received I get the result that I expect, but it doesn't activate or deactivate the relay! not really sure what I am doing wrong.

Please can you take a look at the code for the receiving arduino and give me some pointers.

Many thanks

John

/*
This Program will recieve data from the NRF24l01, depending on the result the relay on pin 8 will activate or deactivate. 1 on 0 off.
 */


#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10


const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/

int state;  // holds relay state
int relay = 8; // declare pin 8 as relay

void setup()   
{
  Serial.begin(9600);
  delay(1000);
  Serial.println("Nrf24L01 Receiver Starting");
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();

}
//--(end setup )---


void loop()   

{
 
  
  if ( radio.available() )
  {
    // Read the data payload until we've received everything
    bool done = false;
    while (!done)
    {
      // Fetch the data
      done = radio.read( &state, sizeof(unsigned long) );
      
    
     
      Serial.println(state); // print the received data 0 or 1
      
      digitalWrite(relay,state);// set relay as state: 1 on, 0 off
    }
  
}
}

Did you forget a pinMode for pin 8?

Hi,

digitalWrite(relay,state);// set relay as state: 1 on, 0 off

You have to have HIGH or LOW instead of state.

So you will need to test the value of state and then digitalWrite HIGH or LOW.

Tom.... :slight_smile:

yes! pinMode(8, OUTPUT);

although I did have this in the actual code, still doesn't work!

So you will need to test the value of state and then digitalWrite HIGH or LOW.

so this

if (state = 1)
digitalWrite(relay,HIGH);

You have to have HIGH or LOW instead of state.
digitalWrite() - Arduino Reference

Zero for LOW and any non-zero for HIGH will work.

so this

if (state = 1)

No.

 this

if (state == 1)

so should this work?

*
This Program will recieve data from the NRF24l01, depending on the result the relay on pin 8 will activate or deactivate. 1 on 0 off.
 */


#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10


const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/

int state;  // holds relay state
int relay = 8; // declare pin 8 as relay

void setup()   
{
  Serial.begin(9600);
  delay(1000);
  Serial.println("Nrf24L01 Receiver Starting");
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
  pinMode(relay, OUTPUT);
}
//--(end setup )---


void loop()   

{
 
  
  if ( radio.available() )
  {
    // Read the data payload until we've received everything
    bool done = false;
    while (!done)
    {
      // Fetch the data
      done = radio.read( &state, sizeof(unsigned long) );
      
    
     
      Serial.println(state); // print the received data 0 or 1
      
      
    }
  if (state = 1)
  {
    digitalWrite(relay,HIGH);// set relay as HIGH: on
}
}
}

OK ==, thanks

Hi,
you may also have to test for state 0, to turn the relay off, or add else to your if test.

Tom.. :slight_smile:

Yes, Thanks Tom