XBee, Arduino, and RPi

XBee, Arduino, and RPi

Introduction – over the garden wall

In mid July of 2014, I had posted about a new version of RPi and XBee.  In this post will cover XBee wireless technology.  We will also cover how XBee can be used to link sensors to the RPi.  Next, we go through linking the Arduino to the RPi.  In reminiscence, a newer offering of the RPi has just been made available to the public.  I may do a brief overview of the new features, but that will be secondary.

Purpose – the wire dogs can’t get through

One issue with bench setups is they don’t scale well in the real world.  Most of the problem has to due with connections.  There was a project I was developing and the biggest challenge was cabling.  Using a wireless connection was the only viable option.  Some barriers take too much effort and can hinder completion.  Using XBee broke down the wall.

Details – cobblestones

In this post I’ll be logging temperature and humidity readings from a DHT22 sensor.  You can find them online at Adafruit.  The sensor will be wired with an Arduino Uno board that runs a sketch and outputs the readings serially to a XBee.  I’ll be using an wireless modem adapter for the XBee that I purchased from Adafruit.  Since this will be point to point, I have 2 XBee and modem adapters.  I’m using the ZB Series 2 to keep costs down.  There are higher output models for greater range, but I won’t need that for this project.  On the other end of the wireless connection, I’ll wire the XBee serial and power rails to a PI T-Cobbler.  This makes it easier to connect jumpers to the bread board, without the ribbon cable getting in the way.

XBee wireless modem adapter pinout






This is a good time to mention the logic differences between the Arduino Uno and the RPi.  The Uno operates at 5V logic levels, while the RPi operates at 3.3V levels.  If we were to directly connect the Uno to the RPi, something would get bricked.  Here comes another benefit of using XBee and the modem adapter.  The XBee will only operate on 3.3V levels, however the adapter regulates to that voltage from a 5V source.  So, we can connect the XBee modem adapter directly to either the Uno or RPi.  Be sure to correctly link the power based on the logic level of your system.

XBee Uno RPi Interface







Here is a wiring diagram of the setup I’ve used.  The pin numbering for the adapter doesn’t reflect that actual pins used, pay attention to that.  There weren’t any special considerations other than paying attention to the voltage applied to what pin of the XBee adapter.  I can’t say it enough, you’ll smoke your equipment if you hook up the wrong voltage.  Need I say it again?

The Uno sketch used was taken from the Adafruit DHT sensor library.  The only thing I did was select the pin to use.  I verified that I had readings on the serial monitor of the Uno IDE before moving forward.  This is good to do.  If something doesn’t talk with the XBee or RPi, I’ll know it isn’t the Uno or sensor.

There are 3 options for sending data from XBee to XBee, digital, analog, or serial.  I won’t go into all the details on all the inner workings of the XBee here, so please watch this to get an introduction on the principles.  Consider giving Richee a donation for his hard work.  I had to relearn the XCTU interface, it has significantly changed since the tutorial videos.  However, the information is still relevant.

The method used in this post will be serial, so we’ll operate the XBee units in AT mode.  I verified my wireless communication worked with 2 Uno boards first.  Again, this gives me confidence that I don’t have a problem with the sensor, Uno, or XBee devices.   Now onto the Raspberry Pi.

This turned out to be more easier than I thought when I found this site.  There it is, all spelled out for you.  I had most of the work done, so I skipped ahead to step 3.  I’m going to summarize the steps I did do here.  Here are the commands I ran.

1. Install the python serial library, I already had it.

sudo apt-get install python-serial

2. Restart the RPi

sudo shutdown -r now

3. Edit the cmdline file so the serial link will work.

sudo nano /boot/cmdline.txt




4. Edit the inittab file so the serial link will work.

sudo nano /etc/inittab


T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100


5. Create the python script.

nano serialin.py


import serial
port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=3.0)
while True:
    rcv = port.readline()
    print("received" + repr(rcv))


6. Run the python script and watch the readings in the terminal.

python serialin.py

The readings can now be logged on the RPi by changing the python script a bit, I used the one posted on Cool Components, thanks to the blokes across the pond.  That’s it, the RPi is reading sensor readings gathered from the Uno board over a wireless connection and logging it to a file.

import serial
port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=3.0)
def filewrite(rcv):
   logfile = open("templog.txt", "a")
while True:
   rcv = port.readline()
   print("received" + repr(rcv))

Relations – rapid and flexible

The nice thing about having a mobile wireless sensor ready to use is it’s ready.  In the time it takes me to walk and place the sensor, I’m done.  This sort of thing would take longer with wired connections, if I wanted to be discreet and tidy about it.  There are times when the idea to monitor a condition evaporates before hardware is put in place.  With the number of sensors available, connecting them shouldn’t be an issue.  Nor will it be an issue to use a micro controller like the Uno or a micro processor like the RPi.  Go build it!

Summary – now you’re talking

In this post I covered how to link a RPi and a sensor polling Arduino using a wireless link provided by XBee devices.  The wireless link provided a fast, safe, and effective way to interface the Arduino and RPi.  With this information, you will have the means by which to develop and deploy these devices in a broader range of applications.

Comments are closed.