Here it is demonstrated how one can control LED wirelessly with switches using 433MHz RF module and Arduino. This simple demonstration uses LED control but we can substitute LED with motors or any other actuators. This is useful in robotics, industrial automation and internet of things. This is follow up tutorial of the first tutorial 443MHz RF Wireless Communication with Arduino where simple message was sent from one Arduino to another Arduino wirelessly.
RF Module
The following shows the 433MHz RF module used in this tutorial.
The transmitter module is shown below. It has three pins- VCC, DATA and GND. The VCC is connected to +5V supply, GND pin is connected to ground and the DATA pin is connected to Arduino pin.
The receiver module is shown below. It consist of four pins- one is VCC which is connected to +5V supply, GND is connected to ground and there are two pins for DATA which is connected to Arduino pin.
The following shows the interfacing of the RF transmitter module with Arduino Nano and RF receiver module with Arduino Uno.
The above circuit diagram populated on breadboard is shown below.
At the transmitter, the RF transmitter module DATA pin is connected to Arduino Nano digital pin 12. Two push buttons are connected to pin 5 and 4 respectively. A LED is also connected to pin 3 of Arduino Nano with 220Ohm resistor.
At the receiver the DATA pin of the RF receiver module is connected to pin 11 of the Arduino Uno. A LED with 220Ohm resistor is connected to pin 4 of Arduino Uno.
When the push button connected to Arduino Nano pin 5 is pressed(grounded) at the transmitter then the LED connected at pin 5 is turned on. At the same time, message from the transmitter is sent over the air to turn on the LED at the receiver. The receiver receives the message and the LED connected to Arduino Uno pin 4 is turned on.
Similarly, when the push button connected to Arduino Nano pin 4 is pressed at the transmitter the LED at pin 5 is turned off. At the same time message from the transmitter is sent wirelessly to the receiver to turn off the LED at the receiver.
Program Code
The following is the transmitter code.
// RF 433MHz Modules Transmitter Program
// Module Data pin connected to Arduino digital pin #12 which is used by the library default
#include <RH_ASK.h>
#include <SPI.h>
#define SWON 5
#define SWOFF 4
#define LED 3
char *msg;
int state = 0;
RH_ASK driver;
void setup () {
pinMode(SWON, INPUT_PULLUP);
pinMode(SWOFF, INPUT_PULLUP);
pinMode(LED, OUTPUT);
Serial.begin (9600); // For debugging only
driver.init();
if (!driver.init ())
Serial.println ("Initialization Failed!");
}
void loop () {
if (digitalRead(SWON) == LOW) {
msg = "ON";
state = 1;
digitalWrite(LED, HIGH);
}
else if (digitalRead(SWOFF) == LOW) {
msg = "OFF";
state = 1;
digitalWrite(LED, LOW);
}
else if (state == 1) {
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
delay(200);
state = 0;
}
}
The following is the receiver code.
// RF 433MHz Module Receiver Program
// Module Data pin connected to Arduino digital pin #11 which is used by the library default
#include <RH_ASK.h>
#include <SPI.h> // Required to compile
#define MOT 4
char receive[32];
RH_ASK driver;
void setup () {
pinMode(MOT, OUTPUT);
Serial.begin (9600); // For debugging only
if (!driver.init ())
Serial.println ("Initialization Failed!");
}
void loop () {
uint8_t buff [RH_ASK_MAX_MESSAGE_LEN];
uint8_t bufflen = sizeof (buff);
if (driver.recv (buff, &bufflen)) { // Non-blocking
memset(receive, 0, sizeof(receive));
for (int i = 0; i < bufflen; i++) {
receive[i] = buff[i];
}
if (strcmp(receive, "ON") == 0) {
digitalWrite(MOT, HIGH);
Serial.print ("Received Message: ");
Serial.println ((char*) receive);
}
else if (strcmp(receive, "OFF") == 0) {
digitalWrite(MOT, LOW);
Serial.print ("Received Message: ");
Serial.println ((char*) receive);
}
else {
digitalWrite(MOT, LOW);
Serial.print ("Received Message: ");
Serial.println ((char*) receive);
}
}
}
Video demonstration
The following video shows how the LED control with switch using the 433MHz RF communication works.