Maker.io main logo

How To Wirelessly Transmit Data on Arduino

2019-04-22 | By Maker.io Staff

Arduino

Arduino boards contain multiple peripherals for communicating with other devices including UART, SPI, I2C, USB, and even bit-banged custom protocols on GPIO pins. However, wired communication can be a pain, especially in scenarios where there is a fair bit of distance between the transmitter and receiver. In this article, we will learn how to use the very famous 433MHz modules with an Arduino so we can get two Arduino’s to wirelessly communicate.

BOM

SCHEME-IT

How To Wirelessly Transmit Data on Arduino 

Scheme-it

THE FAMOUS 433MHz TRANSMITTER / RECEIVER MODULES

When two devices talk wirelessly, they often use electromagnetic waves known as radio waves. The data sent is transmitted on a specific frequency so that only receivers tuned to the correct frequency can receive it. This tuning prevents devices from detecting all radio waves and is one of the core principles of radio design. The same happens when you tune your car radio to different stations. Despite radio frequencies being an infinite series in theory, the reality of radio frequencies is that there are a finite number of different frequencies which can exist before interference occurs.

Therefore, to prevent interference and misuse most radio frequencies require a license to broadcast. Radio stations, for example, own a radio broadcast library for a specific frequency such as FM 107MHz. However, there are some radio frequencies which are open to public use, the most common example being the 2.4GHz spectrum which is used by Wi-Fi networks. One other frequency that is very common is the 433MHz spectrum, used by many simple radio applications, like wireless thermostat control and front door alarms.

While there are many 433MHz modules on the market, the easiest by far is the 433MHZ RF LINK KIT transmitter and receiver. Unlike other modules, these radio modules use no processor and entirely analog, using transistors, oscillators, and simple components. Using only a single data pin, they can easily be integrated into Arduino projects. When using the RH_ASK library they are even easier to program!

How To Wirelessly Transmit Data on Arduino 

INSTALL THE NEEDED LIBRARY

Before we can use the 433MHz modules, we need to install a library that contains the transmission protocol, handling code, and checksum error detection. Unfortunately, the RH_ASK library is not available via the Arduino Library Manager. Instead, download the ZIP file manually. To download the ZIP file, visit the link below.

http://www.airspayce.com/mikem/arduino/RadioHead/RadioHead-1.41.zip

When the ZIP file has been downloaded, open up the Arduino IDE and then navigate to Sketch > Include Library > Add .ZIP library. An open file dialog should open at this point, simply locate your ZIP file and then click OK. The Arduino IDE should automatically install the RadioHead library for you and will be available in the “Include Library” menu.

How To Wirelessly Transmit Data on Arduino 

USING THE RADIOHEAD LIBRARY

The RH_ASK library is incredibly easy to use as it takes care of all the complexity involved with sending data via radio. The first two lines that you need to include in your Arduino program are shown below. The SPI library is most likely needed for timing reasons since library is designed to run on many different platforms, therefore it requires a timing reference.

 
Copy Code
#include <RH_ASK.h>   // Include the RH_ASK library
#include <SPI.h>      // Not actually used but needed to compile the RH_ASK library 

With the needed libraries included, we now create a RH_ASK object to send and receive data with. The RH_ASK object declaration accepts several parameters. If no parameters are passed, then the default options are assumed, but it is best to set at least the first three as these define the speed of the radio transmission, the tx pin, and the rx pin. Then the init() function needs to be called as this initializes the RH_ASK object.

Copy Code
RH_ASK radio(2000, 11, 12);
 
void setup()
{
    Serial.begin(9600);   // Use this for debugging
 
    // Speed of 2000 bits per second
    // Use pin 11 for reception
    // Use pin 12 for transmission
    
    if (!radio.init())
    {
         Serial.println("Radio module failed to initialize");
    }
}

Sending data with the library is rather easy because it uses only two functions: .send(message, length) and .waitPacketSent(). The send function takes a const char buffer and an integer which is the size of that buffer. The waitPacketSent function simply waits until the RH_ASK library has sent all the data over the transmitter. When sending messages it’s important to include the uint8_t typecast as this ensures that data being sent is in the form of 8-bit characters.

Copy Code
void loop()
{
    // Create our message
    const char *msg = "Hello World";
 
    // Send our message
    radio.send((uint8_t*)msg, strlen(msg));
 
    // Wait until the data has been sent
    radio.waitPacketSent();
 
    // Delay since we dont want to send a trillion packets 
    delay(1000);
 
    // Also inform the serial port that we are done!
    Serial.println("Data Sent");
}

Receiving data is done using the non-blocking function .recv(buffer, bufferLength). A non-blocking function is one that does not halt the system until a specific event had occurred. For example, when the recv function is called, the RH_ASK library checks to see if there is any new data and if there is, then the function returns true, else it will return false.

A blocking function would sit and wait until data was ready and no other code can execute during this wait. The data is transferred into the specified buffer. The buffer length parameter is just to ensure that the RH_ASK library does not overflow your buffer.

Copy Code
void loop()
{
  // Create a 32 byte char buffer
  uint8_t receive_buffer[32];
  uint8_t buflen = sizeof(receive_buffer);
 
  // If data is available, print it
  if (radio.recv(receive_buffer, &buflen))
  {
    Serial.print("Message: ");
    Serial.println((char*)receive_buffer);         
  }
}

If all goes to plan you should see the following on the output window from the Arduino that has the receiver.

How To Wirelessly Transmit Data on Arduino

TRANSMITTER CODE

Copy Code
#include <RH_ASK.h>   // Include the RH_ASK library
#include <SPI.h>      // Not actually used but needed to compile the RH_ASK library
 
RH_ASK radio(2000, 11, 12);
 
void setup()
{
    Serial.begin(9600);   // Use this for debugging
 
    // Speed of 2000 bits per second
    // Use pin 11 for reception
    // Use pin 12 for transmission
    
    if (!radio.init())
    {
         Serial.println("Radio module failed to initialize");
    }
}
 
void loop()
{
    // Create our message
    const char *msg = "Hello World";
 
    // Send our message
    radio.send((uint8_t*)msg, strlen(msg));
 
    // Wait until the data has been sent
    radio.waitPacketSent();
 
    // Delay since we dont want to send a trillion packets 
    delay(1000);
 
    // Also inform the serial port that we are done!
    Serial.println("Data Sent");
}

RECEIVER CODE

Copy Code
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
 
RH_ASK radio;
 
void setup()
{
    Serial.begin(9600);   // Use this for debugging
 
    // Speed of 2000 bits per second
    // Use pin 11 for reception
    // Use pin 12 for transmission
    
    if (!radio.init())
    {
         Serial.println("Radio module failed to initialize");
    }
}
 
void loop()
{
  // Create a 32 byte char buffer
  uint8_t receive_buffer[32];
  uint8_t buflen = sizeof(receive_buffer);
 
  // If data is available, print it
  if (radio.recv(receive_buffer, &buflen))
  {
    Serial.print("Message: ");
    Serial.println((char*)receive_buffer);         
  }
}

Recommended Reading

What is Thread? Low-power IoT Networking for Smart Home Devices | DigiKey

How to Use Logic Level Shifters – ATM | DigiKey

制造商零件编号 A000057
ARDUINO LEONARDO W/ HDRS ATMEGA3
Arduino
制造商零件编号 113990010
433MHZ RF LINK KIT
Seeed Technology Co., Ltd
制造商零件编号 PRT-12796
JUMPER WIRE F/F 6" 20PCS
SparkFun Electronics
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum