433MHz RF communication to a Raspberry Pi

433MHz RF communication to 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 transmitter device to a Raspberry Pi. 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 any device connected to a 433MHz RF Transmitter module with a Raspberry Pi (connected to a 433MHz RF Receiver module) using specific RF signal-codes. We will be using an Arduino as the transmitter for testing purposes. I will show you how to do this with and then use the signal-code on the Raspberry Pi through Python to start some logic.

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 Damay from Homeautomation.org for his 433Mhtz RF communication between Arduino and Raspberry Pi: Raspberry Pi as receiver article. He saved me a lot of long hours’ research for writing this one.

Requirements & assumptions

First of all, you need a fully functional Raspberry Pi with Raspbian installed. After the setup, we will need the command prompt to do some testing and receiving. 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, coding and testing.

For this tutorial 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
  • Running Raspberry Pi
  • 433 MHz spiral helical antennas
  • Solderless developing breadboard
  • Some additional breadboard wiring (Arduino and Raspberry Pi compatible)
  • Functional Arduino connected to a serial monitor. An Arduino Nano was used.

Setting up the Arduino to transmit RF signals

433MHz RF Transmitter moduleThis will set the Arduino up as the transmitter. To do so grab the square (vs. longer) module from your 433MHz RF Transmitter Receiver module pair.

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

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 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 Transmitter module is clearly marked. Connect the breadboard wiring as follows:

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

Connect the Arduino to the Arduino IDE and upload the following sketch to it:

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

void setup() {
  Serial.begin(9600);
  // Transmitter is connected to Arduino Pin #10  
  mySwitch.enableTransmit(10);

  // Optional set pulse length.
  // mySwitch.setPulseLength(320);

  // Optional set protocol (default is 1, will work for most outlets)
  // mySwitch.setProtocol(2);

  // Optional set number of transmission repetitions.
  // mySwitch.setRepeatTransmit(15);

}

void loop() {
  /* Using decimal code */
  mySwitch.send(1234, 24);
  Serial.print("Attempting to send 1234");
  delay(4000);
  mySwitch.send(4321, 24);
  Serial.print("Attempting to send 4321");
  delay(4000);

  /* Using binary code */
  //mySwitch.send("000000000001010100010001");
  //delay(1000);  
  //mySwitch.send("000000000001010100010100");
  //delay(1000);*/
}

As is, this piece of coding makes the Arduino send the decimal code ‘1234’, wait for four seconds and then send ‘4321’. It will tun over and over until the Arduino is disconnected (switching it off). You can play a bit with the binary send options by removing the //’s. Don’t change this too much until your comfortable with Arduino code.

Other important things to notice here is the inclusion of the library and the setting up of the transmitter to pin D10.

Give your Arduino power and connect it to the Serial Monitor. The code should show that it is being send. That’s it from the Arduino for now – let it continue to run in the background…

Setting up the Raspberry Pi to receive RF signals

433MHz RF Receiver moduleIn this tutorial, we will set the Raspberry Pi up as the receiver. To do so grab the longer (vs. square) module from your 433MHz RF Transmitter Receiver module pair and get it antennaed again. We will be using wiringPi and a modified version of 433Utils.

When positioning your Raspberry Pi GPIO pins facing up and on the opposite side of you, 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 Receiver module is clearly marked. The receiver module has two DATA pins. As a rule I like to always use the one closest to GND. Before plugging in the Raspberry Pi, connect the breadboard wiring as follows:

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

To install wiringPi the git clone command will be used. 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, the build can be checked at any time with:

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 above how the physical GPIO pin numbers changes. Physical GPIO pin number 11 for example becomes GPIO 0 with wiringPi. We used the physical GPIO pin 13, so it will become GPIO 2.

To install (clone) 433Utils onto the Raspberry Pi go to the /home/pi directory and use the following:

cd /home/pi
git clone --recursive git://github.com/ninjablocks/433Utils.git
cd 433Utils/RPi_utils
make

To receive RF signal-codes, we will be using RFSniffer.cpp. While in the 433Utils/RPi_utils directory, you can go and check it out by using:

sudo nano RFSniffer.cpp

Nothing should be changed for now. Because we connected the DATA pin of the 433MHz RF Receiver module we will stick to wiringPi’s GPIO 2. Just press Ctrl X to exit back to the terminal.

If the code was changed, in order to use the newest version of RFSniffer.cpp it needs to be compiled again:

make RFSniffer.cpp

Testing the communication

While the Arduino is still sending the code as set up above, the ‘sniffer’ can be activated on the Raspberry Pi. In the Pi’s terminal, go back to the /home/pi directory and type in the sniffer command:

cd
sudo /home/pi/433Utils/RPi_utils/RFSniffer

The terminal will now be waiting for RF signals. If a signal is received it will be displayed on the terminal screen. There might be other random pieces of code in between, but it should mainly show ‘1234’ followed by ‘4321’.

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.

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 shit. 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

Online micro-electronic components, consumables & accessories
Online micro-electronic components, consumables & accessories
21 February 2019
Ad: Buy great value for money micro-electronic components online from BangGood.com. Free international delivery, including to South Africa. More…
433MHz RF Transmitter Receiver modules for wireless communication
433MHz RF Transmitter Receiver modules for wireless communication
14 January 2016 | Updated 19 November 2017
433MHz RF Transmitter Receiver modules are wireless communication modules that uses radio frequency (RF) as signal (similar to what is used in some remote controlled toys). More…
Mounting a shared NAS folder onto a Paspberry Pi
5 steps in mounting a shared NAS folder to a Raspberry Pi
17 September 2015 | Updated 6 April 2018
Network Attached Storage (NAS) devices are popular to add large amounts of data storage space to a network. A networked Raspberry Pi can also make use of this storage to, for example, make use of its space or to have access to its files. 5 steps in mounting a shared NAS folder to a Raspberry Pi will go through the process using NFS. More…
A Short Introduction to the GPIO pins of the Raspberry Pi
A short introduction to the GPIO pins of the Raspberry Pi
24 February 2016 | Updated 12 June 2018
The Raspberry Pi is equipped with a set of input/output or GPIO (general-purpose input/output) pins. This powerful feature significantly expands the Raspberry Pi’s abilities to communicate with other devices. These pins are connected directly the to CPU of the Raspberry Pi. More…