You are here

The Virtual Wire Library

In one of my projects I needed an inexpensive but reliable RF link over which I could send a few bytes of data. A popular and inexpensive transmitter and receiver is available on the 433 MHz frequency band using Amplitude Shift Keying (ASK). The challenge was to set up a reliable link using these devices.

Since ASK is being used as modulation technique you cannot send a continuous stream of ‘1’ and ‘0’ over it since the Automatic Gain Control (AGC) of the receiver would filter it out. Instead a DC-neutral signal had to be transmitted with an equal number of ‘1’ and ‘0’ in it. I started using bi-phase encoding, also known as Manchester encoding. By measuring the bit-time of each edge in the signal I could retrieve the data. This all worked fine as long as there are not extra edges in the bit stream due to interferences. These interferences are, however, present since the 433 MHz band is used by many other home devices like your weather station when it has a wireless outside temperature sensor.

So I needed something more reliable that works more or less like a UART. In a UART, samples are taken of the bit stream and with sufficient samples having the same value for 1 bit, a ‘1’ or ‘0’ can be decoded. I found the solution if the Virtual Wire library that was written for Arduino. This library acts as a Phase Locked Loop (PLL) to synchronize itself with the incoming bit stream after which it takes samples like a UART to determine if a bit is ‘1’ or ‘0’. Next to that the library uses an encoding mechanism to make the bit stream DC-neutral. Last but not least the library add a Cyclic Redundancy Check (CRC) as checksum to determine if the message was valid. Next to reception of data, the library supports the transmission of data, adding the same CRC checksum to the transmitted message.
I ported this library to JAL and created the virtual_wire.jal library. Since I wanted to be able to use this library also on smaller PICs I created a transmitter only version and a receiver only version that require less data space and code space.

Initially I was not so happy with the library since it was only supporting a few PIC types. Recently I rewrote the library and changed the structure so that it can be used on any PIC. The only change is that the timer that is used was moved from the library to the main program.
I did some tests with this library and it works perfectly even when sending data in my house from the addict to the basement. Of course disturbances are available but due to the sampling mechanism these are often filtered out. Because of the CRC you will never receive an invalid message.

For developing and debugging a transmitter/receiver module you would need 2 PIC programmers to make your development life easier. Unfortunately I did not have 2 programmers. Instead I used 2 of my JALPIC One development boards (see: which saved me development time. After I completed the library development on these boards I ported the sample application to a PIC12F617. In the attached document you find the set-up I used for testing it on the PIC12F617 using the transmitter only and receiver only version. I also created a different set of sample programs for the PIC16F1825 using the complete Virtual Wire Library. The sample programs can be found in one of the bee packages.