Interfacing 433MHz RF Module With Arduino
Interfacing 433MHz RF Module With Arduino
Interfacing 433MHz RF Module With Arduino
Interested in making your next Arduino project wireless for the price of a cup
of coffee? Then go with a 433MHz RF Transmitter and Receiver Module!
It is available online for less than two dollars, making it one of the most
affordable data communication options available. And, best of all, this module
is so small that you can incorporate it into almost any project.
Hardware Overview
Let’s take a closer look at the 433MHz RF Transmitter and Receiver Modules.
The Transmitter
This tiny module serves as the transmitter. It is as simple as it appears. At the
core of the module is a SAW resonator tuned to operate at 433.xx MHz. Apart
from that, it has a switching transistor and some passive components.
When the DATA input is high, the oscillator generates a constant RF output
carrier wave at 433.xx MHz, and when the DATA input is low, the oscillator
ceases operation; resulting in an amplitude modulated wave. This technique is
known as Amplitude Shift Keying, which we will go over in detail shortly.
The Receiver
It’s a lot like the Amplitude Modulation technique used in AM radio. Because it
only has two levels, it is sometimes referred to as Binary Amplitude Shift
Keying.
VCC provides power to the transmitter. Any positive DC voltage between 3.5V
and 12V can be used. It should be noted that the RF output is proportional to
the supply voltage, so the higher the voltage, the greater the range.
Antenna is a pin that connects to the external antenna. To improve the range,
you should solder a 17.3-centimeter-long solid wire to this pin. It is discussed
in detail later.
VCC provides power to the receiver. Unlike the transmitter, the receiver
requires a 5V supply voltage.
DATA pins output the received digital data. Both pins are internally linked, so
you can use either one for data out.
Antenna is a pin that connects to the external antenna. To improve the range,
you should solder a 17.3-centimeter-long solid wire to this pin.
Because we’ll be sending data between two Arduino boards, we’ll need two
Arduino boards, two breadboards, and a few jumper wires.
Connect the module’s VCC pin to the Arduino’s 5V pin and GND to Ground.
The Data-in pin should be connected to digital pin #12 on the Arduino. Try to
use only digital pin #12 for data input, as the library we are using uses this pin
by default.
After you’ve wired the transmitter, you can proceed to the receiver. The
receiver’s wiring is as simple as the transmitter’s.
Once again, there are only three connections to make. Connect the module’s
VCC pin to the Arduino’s 5V pin and GND to Ground. Connect any of the
middle two data-out pins to the Arduino’s digital pin #11, as they are
internally connected.
Receiver Arduino
VCC 5V
Data 11
GND GND
The wiring is shown in the image below.
At the transmitter end, the RadioHead library takes the data, encapsulates it
into a data packet (known as a RadioHead Packet) with a CRC checksum, and
then sends it to another Arduino with the necessary preamble and header. At
the receiver end, if the data is correctly received, the receiving Arduino is
notified that data is available.
You can get the library from airspayce.com or by clicking the following link:
RadioHead.zip
To install the library, launch the Arduino IDE, navigate to Sketch > Include
Library > Add.ZIP Library, and then choose the RadioHead file you just
downloaded. For more information on installing a library, see the Installing an
Arduino Library tutorial.
void setup()
{
// Initialize ASK Object
rf_driver.init();
}
void loop()
{
const char *msg = "Hello World";
rf_driver.send((uint8_t *)msg, strlen(msg));
rf_driver.waitPacketSent();
delay(1000);
}
Code Explanation:
It’s a short sketch, but that’s all you need to send a message.
The sketch begins by including the RadioHead ASK library. The Arduino SPI
Library must also be included because the RadioHead library is dependent on
it.
#include <RH_ASK.h>
#include <SPI.h>
To access special functions related to the RadioHead ASK library, we must first
create an ASK object.
// Preparing a message
const char *msg = "Hello World";
The message is then sent using the send() function. This function takes two
parameters: an array of data and the number of bytes (data length) to send.
Here is the code for our receiver. Connect the receiver circuit to the computer
and load the following code.
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library
#include <SPI.h>
void setup()
{
// Initialize ASK Object
rf_driver.init();
// Setup Serial Monitor
Serial.begin(9600);
}
void loop()
{
// Set buffer to size of expected message
uint8_t buf[11];
uint8_t buflen = sizeof(buf);
// Check if received packet is correct size
if (rf_driver.recv(buf, &buflen))
{
After loading the sketch, open your serial monitor. If everything is fine, you
should be able to see your message.
Code Explanation:
The receiver code, like the transmitter code, begins by loading both the
RadioHead and SPI libraries and creating an ASK object.
#include <RH_ASK.h>
#include <SPI.h>
RH_ASK rf_driver;
In the setup function, we initialize the ASK object and configure the serial
monitor to display the received message.
rf_driver.init();
Serial.begin(9600);
In the loop function, we create a buffer of the same size as the transmitted
message. Remember, it’s 11 in our case? This will need to be adjusted to
match the length of your message. Include any spaces and punctuation
because they all count as characters.
uint8_t buf[11];
uint8_t buflen = sizeof(buf);
Then we call the recv() function. This activates the receiver. When a valid
message is available, it copies it to its first parameter buffer and returns true. If
the function returns true, the received message is printed on the serial
monitor.
if (rf_driver.recv(buf, &buflen))
{
Serial.print("Message Received: ");
Serial.println((char*)buf);
}
Then we return to the beginning of the loop and repeat the process.
The antenna does not have to be complicated. A single core wire can be used
to make an excellent antenna for both the transmitter and receiver.
A 69.24 cm antenna is quite long and inconvenient to use. That is why we will
use a quarter wave antenna, which measures 17.3 cm or 6.8 inches.
Coiling the antenna to make it more compact will severely reduce its range. A
straight antenna is always preferable!