Last week, I bought 2 pieces NRF24L01 IIC adapter board from ICStation,then I began to make my project. I want to use two Arduino boards to realize wireless communicating relay.It takes almost an hour in total.Now I make it!
![]()
The function of the program:Press the button and then loosen, the wireless relay will connect or disconnect which depends on the previous state.
Receiving program
unsigned char ppt =0;
const int relayPin = 2; // relay output
#include <Wire.h>
void setup()
{
pinMode(relayPin, OUTPUT); //relay output;
digitalWrite(relayPin, LOW);
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
unsigned char recieve = 0;
void loop()
{
// Wire Master Reader
Wire.requestFrom(35, 1); // request 6 bytes from slave device #2
if(Wire.available())
{
recieve = Wire.read(); // receive a byte as character
if(recieve !=0x47){
if(recieve == 1) digitalWrite(relayPin, HIGH);
if(recieve == 2) digitalWrite(relayPin, LOW);
}
}
delay(10);
}
Sending program:
#include <Wire.h>
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 3; // the number of the LED pin
int buttonState = 0 ,buttonState2 = 0; // variable for reading the pushbutton status
unsigned char Key_status = 0;
unsigned char Key_blink =0;
int Key_num =0;
void setup()
{
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
unsigned char judge = 0;
char scan;
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if(buttonState == LOW) {
Key_num = 1;
}
else
{
if (Key_num == 1){
Key_num = 0;
++Key_blink;
if(Key_blink == 1){
digitalWrite(ledPin, HIGH); //open
Wire.beginTransmission(35); // transmit to device #35
//Wire.write(7); // sends five bytes
Wire.write(1);
Wire.endTransmission(); // stop transmitting
}
else{ //close
digitalWrite(ledPin, LOW);
Wire.beginTransmission(35); // transmit to device #35
//Wire.write(7); // sends five bytes
Wire.write(2);
Wire.endTransmission(); // stop transmitting
Key_blink =0;
}
} //
}
}
