Computing stuff tied to the physical world

RFM12B library for Arduino

In AVR, Software on Feb 10, 2009 at 00:03

Here’s a driver for the RFM12B radio with Arduino’s and similar AVR boards. The radio is connected as follows:


The code is available as or via subversion. It’s packaged as an Arduino library. To try out the included example, you’ll obviously need two Arduino’s + radio modules, connected as above. Then:

  • unpack the above zip as new directory in your Arduino IDE’s “libraries/” folder
  • build and upload the included example sketch to both systems
  • check proper operation by opening the serial port at 57600 baud
  • you should see a “[RF12DEMO]” greeting on both
  • on one unit, type “0i 0i 1i” to set its node ID to 1
  • on the other, type “0i 0i 2i” to set its node ID to 2
  • send a test packet of 7x <N> bytes by typing “<N>s” (<N> = 0..9)
  • you should see the test packet on the other node
  • use “<N>a” to send a test packet and request an ack

The node ID is stored in EEPROM (the demo uses byte 0), so you only need to initialize ID’s once in each node.

This code uses interrupts to do most of the work in the background for transmission as well as reception. The packet buffer is limited to 66 bytes of data. All packets are verified with a 16-bit CRC.

Packets can be sent to a specific node (ID 1..30) or broadcast to all (ID 0). The demo code sends its test packets as broadcasts, but replies with acknowledgements to just the originator. Note that requesting an acknowledgement for broadcast packets only makes sense with two nodes.

Update – instructions for the demo are now slightly different (see the README).

  1. This does not work on my hardware. I have two Arduino’s with RFM12B’s that work with your code published with your article ‘Good RF with RFM12B’ so I guess the hardware should be ok. Although I must say that with this last code some bits seem to get lost in the transmission and only about 50% of the characters are received correctly. Puzzles me…

  2. Maybe I should clearify the ‘does not work’. I can load the sketch and set the node id’s. But send packets do not show up at the receiver end. Also the test package with ack gets no ack.

  3. Weird. Note that this driver runs at 57600 baud vs. 4800 for the previous code. Do you have ≈ 85 mm antenna’s? Are you doing the 3.3V / 5V level conversion with the Arduino? See the “Arduino + RFM12B + µSD” post for an example. I can’t think of much more, I’m using the latest code in subversion (now at Google Code) with a handful of setups. Works with Arduino IDE’s 12 and 13.

    This stuff can be very tricky to debug, given that problems can be on either end…

    (Thanks for trying it out, btw!)

  4. One more thing: once you put stuff in hardware/libraries/, that won’t be re-compiled after a change. You have to switch board type and back to force the compile. Tedious.

  5. I’ll might give it another try later. I got your previous code running 100% now. There was an issue in the 3,3 volt from the RFM12B to 5 volt Arduino. I found out that slowing down the SPI speed fixes the problem.

  6. I have been looking around for suppliers of the RFM12B after first hearing about the device in an Elektor article a couple of months ago.

    HopeRF do them for $3 each samples or £2 each large quantity. But with a $22 carriage charge and customs charges to pay. in Germany sell them for 4.49 euro.

    Does jcw or others know of alternative suppliers in the EU who are cheaper than for this device?

  7. When I wrote above £2 each… I of course meant $2 each…

  8. Thanks for that feedback jcw, I was not suggesting you should retail them BTW. If I find other suppliers in due course, I will let this blog know.

Comments are closed.