433MHz RF communication from a Raspberry Pi

433MHz RF Communication from a Raspberry Pi

This post will explain how to get up and running with the 433MHz RF Transmitter Receiver modules to send radio frequency (RF) signal-codes from a Raspberry Pi to other connected devices. It will be updated from time to time when I stumble upon more relevant information. If you stumbled upon more relevant information, see mistakes or doesn’t understand anything, please feel free to comment at the end.

A very easy way to ‘physically’ connect the Raspberry Pi to the outside world is using its GPIO pins and the 433MHz RF Transmitter Receiver modules. What is more, it will be wireless! After following this basic tutorial, you will be able to communicate from a Raspberry Pi (connected to a 433MHz RF Transmitter module) with one or multiple devices (connected to a 433MHz RF Receiver module) using specific RF signal-codes, and for example an Arduino (which we will be using for testing purposes) or other Raspberry Pis. This post will show how to do this with a simple command line which can then be used in Bash or Python.

Background

Because the Raspberry Pi is quite fragile, or should be considered that, I was looking for a way to use the GPIO pins without having to open up the case all the time. I ultimately developed a little RF box connected to the Raspberry Pi which can both send and receive signal-codes. This allowed me to do the initial and additional programming without interfering with the hardware. See the Related Topics for related posts.

Thanks

I would like to thank Vincent Demay from Homeautomation.org for his 433Mhtz RF communication between Arduino and Raspberry Pi: Arduino as receiver article. He saved me a lot of long hours’ research for writing this post.

Requirements & assumptions

First of all, you need a fully functional Raspberry Pi with Raspbian installed. After the setup, we will be using the command line, Bash or a Python script to send the signals. If you don’t have a screen, keyboard and mouse you will need to be on a network and use PuTTY and/or WinSCP for the setup, sending of signal-codes, testing and coding.

You will also need:

These were the parts that were used. Most are available from BangGood, eBay and RS Components. Direct links to some of the parts are supplied lower down.

  • At least 2 pairs of 433 MHz Transmitter Receiver units.
  • 433 MHz Spiral Spring Helical Antennas
  • Solderless developing breadboard and  some additional breadboard wiring (Arduino and Raspberry Pi compatible)
  • Functional Arduino connected to a serial monitor. An Arduino Nano was used.

Setting up the Raspberry Pi to transmit RF signals

433MHz RF Transmitter moduleIn this tutorial, the Raspberry Pi will be configured the transmitter. To do so grab the square (vs. longer) module from the 433MHz RF Transmitter Receiver module pair. wiringPi and a modified version of 433Utils will be used.

First of all, very important, get that thang antennaed. Use the official 433 MHz Spiral Spring Helical Antennas or make one by coiling a 25 cm 1 mm solid wire and soldering it into the antennae hole.

When positioning the Raspberry Pi GPIO pins facing up and on the opposite side, i.e. with the USB and Ethernet port on the right, the physical GPIO pin numbering will be as follows:

Raspberry Pi GPIO Layout Model B Plus

The pins on the 433MHz RF Transmitter module is clearly marked.

Before plugging in the Raspberry Pi, connect the breadboard wiring as follows:

  • ATAD (DATA) to physical GPIO pin number 11 (i.e. GPIO17)
  • VCC (5 V) to physical GPIO pin number 2 or 4
  • GND to physical GPIO pin numbers 6, 9 or 14

To install wiringPi git needs to be installed. The following commands can be used from the /home/pi directory:

sudo apt-get install git
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build

After wiringPi is installed, check the build at any time with this command:

gpio readall

which, if correctly installed will give the state of each pin on the GPIO:

+----------+-Rev1-+------+--------+------+-------+
| wiringPi | GPIO | Phys | Name   | Mode | Value |
+----------+------+------+--------+------+-------+
|      0   |  17  |  11  | GPIO 0 | OUT  | Low   |
|      1   |  18  |  12  | GPIO 1 | IN   | Low   |
|      2   |  21  |  13  | GPIO 2 | IN   | Low   |
|      3   |  22  |  15  | GPIO 3 | IN   | Low   |
|      4   |  23  |  16  | GPIO 4 | IN   | Low   |
|      5   |  24  |  18  | GPIO 5 | IN   | Low   |
|      6   |  25  |  22  | GPIO 6 | IN   | Low   |
|      7   |   4  |   7  | GPIO 7 | IN   | Low   |
|      8   |   0  |   3  | SDA    | IN   | High  |
|      9   |   1  |   5  | SCL    | IN   | High  |
|     10   |   8  |  24  | CE0    | IN   | Low   |
|     11   |   7  |  26  | CE1    | IN   | Low   |
|     12   |  10  |  19  | MOSI   | IN   | Low   |
|     13   |   9  |  21  | MISO   | IN   | Low   |
|     14   |  11  |  23  | SCLK   | IN   | Low   |
|     15   |  14  |   8  | TxD    | ALT0 | High  |
|     16   |  15  |  10  | RxD    | ALT0 | High  |
+----------+------+------+--------+------+-------+

Note how the physical GPIO pin numbers changes. Physical GPIO pin number 11 for example becomes GPIO 0 with wiringPi.

To install (clone) 433Utils onto the Raspberry Pi, go back to the /home/pi directory and use the git clone command again:

cd /home/pi
git clone https://github.com/ninjablocks/433Utils.git
cd 433Utils/RPi_utils

To send RF signals, use codesend.cpp. While in the 433Utils/RPi_utils directory, it can be checked it out by using:

sudo nano codesend.cpp

Nothing should be changed for now. Because the DATA pin of the 433MHz RF Transmitter module is connected, stick to wiringPi’s GPIO 0. Press Ctrl X to exit back to the terminal.

If codesend.cpp was altered in any way, it needs to be compiled again with:

make codesend.cpp

Setting up the Arduino to receive RF signals

433MHz RF Receiver moduleThis will set the Arduino up as the receiver. To do so grab the longer (vs. square) module from the 433MHz RF Transmitter Receiver module pair and get it antennaed again. On the Arduino, the RC Switch library needs to be available in the Libraries directory.

To add this library to the Arduino IDE, select the latest version of the downloaded zip-file from Add .ZIP Library… from the Include Library option under the Sketch menu.

On a Linux system (including Raspbian) the following can be used to clone the latest version of the RC Switch library directly into the Arduino IDE libraries folder:

sudo apt-get install git
cd /home/pi/sketchbook/libraries
git clone https://github.com/sui77/rc-switch.git

To prevent Java errors, rename the rc-switch directory to rcswitch:

mv /home/pi/sketchbook/libraries/rc-switch /home/pi/sketchbook/libraries/rcswitch

The pins on the 433MHz RF Receiver module is once again clearly marked. The receiver module has two DATA pins. As a rule I like to always use the one closest to GND. Connect the breadboard wiring as follows:

  • ATAD (DATA) to the D2 pin of the Arduino
  • VCC (5V) to 5V pin of the Arduino
  • GND to the GND pin of the Arduino

Connect the Arduino to the Arduino Software on a working computer and upload the following sketch to it:

/*
  RF_Sniffer

  Hacked from http://code.google.com/p/rc-switch/

  by @justy to provide a handy RF code sniffer
*/

#include "RCSwitch.h"
#include <stdlib.h>
#include <stdio.h>
RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(0);  // Receiver on interrupt 0 > pin D2
}

void loop() {
  if (mySwitch.available()) {
    int value = mySwitch.getReceivedValue();
    if (value == 0) {
      Serial.print("Unknown encoding");
    }
    else {
      Serial.print("Received ");
      Serial.print( mySwitch.getReceivedValue() );
      Serial.print(" / ");
      Serial.print( mySwitch.getReceivedBitlength() );
      Serial.print("bit ");
      Serial.print("Protocol: ");
      Serial.println( mySwitch.getReceivedProtocol() );
    }
    mySwitch.resetAvailable();
  }
}

As is, this piece of coding makes the Arduino ‘sniff’ for RF code and if it receives it, it will be displayed on the serial monitor. The ‘bit length’ and ‘protocol’ variables will give more information about the code received. Don’t change this code too much until you are comfortable with Arduino code.

Other important things to notice here is the inclusion of the libraries and the setting up of the receiver pin D2.

Testing the communication

On the Raspberry Pi, go back to the /home/pi directory:

cd /home/pi

A code send command can be issued from the terminal by using the absolute destination of codesend:

sudo ./433Utils/RPi_utils/codesend 1233434
sudo ./433Utils/RPi_utils/codesend 4321
sudo ./433Utils/RPi_utils/codesend 9876574

etc.

These three commands will send ‘1233434’, ‘4321’ and ‘9876674’ respectively. This should be visible on the Arduino Serial Monitor. If not, try to move the Receiver Arduino closer or further away while issuing the codesend command.

Related product links

Please support Behind The Scenes by using these links to purchase these related products online from our partner websites. Links will open in a new window...

About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.
Behind the Scenes is a free, informative website. If you find value in any of out content, please consider making a donation to our cause.
Donate via PayPal

Save, share & Disqus

Use the buttons below, on the left or the bottom of this page to share this post. Your comment is important, but don't be a knob. Keep it constructive and polite.

Comment via Disqus

Disqus is a worldwide comment hosting service for web sites and online communities. This secure platform ensures a pleasant commenting environment which is manageable from one account. Use the Login button to sign up.

More Raspberry Pi related posts

Get WP Toolset – for custom WordPress development without programming
Get WP Toolset – for custom WordPress development without programming
24 June 2019
Ad: Extend the functionality of WordPress with regards to its ability to store data and publish it on your website. More…
Raspberry Pi media centre
Using a Raspberry Pi as a cheap media centre
2 March 2019
With a plethora of mini media centre boxes becoming readily available, it comes as no surprise that open source platforms are also available for this purpose. Kodi is one such platform and it is available to be installed onto a Raspberry Pi. More…
Categories
KodiRaspberry Pi
Getting started with private projects on Raspbian and Bitbucket
Getting started with private projects on Raspbian and Bitbucket
28 October 2018
Bitbucket is a great alternative to Github to start, store and track simple programming dependant project files. On a Raspberry Pi it can be simply done using Raspbian. In addition to Github, projects uploaded to Bitbucket with less than 6 contributors can be kept private and won’t cost a cent. More…
Taking a screenshot on a Raspberry Pi from a remote location
Taking a screenshot on a Raspberry Pi from a remote location
1 July 2018
Whether it is for debugging purposes, making images for tutorials or capturing a moment in a game, screenshots (screengrabs or screen captures) often come in handy. Most operating systems, including Raspbian, will have options to create screenshots by using the keyboard, but what if no keyboard is connected? More…
Categories
Raspberry Pi