This is the receiver code for the 433MHz radio controller and relay control. Make the connections as below and use SPI pin D11 for the receiver radio module. You will also need to download and install the RadioHead library from below. We use that library with the 433MHz modules.
#include <RH_ASK.h> //Download it here https://electronoobs.com/eng_arduino_RadioHead.php
#include <SPI.h> //Not actualy used but needed to compile
RH_ASK driver;
//Outputs
int Pin_D2 = 2; //Connect to relay1 pin
int Pin_D3 = 3; //Connect to relay2 pin
int Pin_D4 = 4; //Connect to relay3 pin
int Pin_D5 = 5; //Connect to relay4 pin
//Initial pin state (LOW OR HIGH)
bool Pin_D2_state = HIGH;
bool Pin_D3_state = HIGH;
bool Pin_D4_state = HIGH;
bool Pin_D5_state = HIGH;
//Variables
int delay_time = 1000; //Delay after each received values (radio)
void setup()
{
//Define pins as outputs
pinMode(Pin_D2,OUTPUT);
pinMode(Pin_D3,OUTPUT);
pinMode(Pin_D4,OUTPUT);
pinMode(Pin_D5,OUTPUT);
//Set initial state to LOW
digitalWrite(Pin_D2,Pin_D2_state);
digitalWrite(Pin_D3,Pin_D3_state);
digitalWrite(Pin_D4,Pin_D4_state);
digitalWrite(Pin_D5,Pin_D5_state);
Serial.begin(9600); // Debugging only
if (!driver.init()){
Serial.println("init failed");
}
}//End of setup
void loop()
{
uint8_t buf[1];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking
{
char received = buf[0];
if(received == '2'){ //If received radio value is "2" we do this...
Pin_D2_state = !Pin_D2_state;
digitalWrite(Pin_D2, Pin_D2_state);
delay(delay_time);
}
else if(received == '3'){ //If received radio value is "3" we do this...
Pin_D3_state = !Pin_D3_state;
digitalWrite(Pin_D3, Pin_D3_state);
delay(delay_time);
}
else if(received == '4'){ //If received radio value is "4" we do this...
Pin_D4_state = !Pin_D4_state;
digitalWrite(Pin_D4, Pin_D4_state);
delay(delay_time);
}
else if(received == '5'){ //If received radio value is "5" we do this...
Pin_D5_state = !Pin_D5_state;
digitalWrite(Pin_D5, Pin_D5_state);
delay(delay_time);
}
Serial.println(received); //Print received value for debug
}
}//End of void loop