Raspberry PI + Xbee: UART / Serial howto

Happy owner of a Raspberry PI for almost a year now, I couldn’t resist to take advantage of if advanced features, and especially it’s GPIO, to communicate with my other electronics parts including my Xbee-enabled wireless sensors.

Although RPI’s onboard UART usage seemed quite simple at first, it finally took me some time to figure out exactly how to read my first bytes received trough UART via Xbee on my RPI, that’s why I’m writing this short snippet.

On the original Debian available for the Raspberry PI, the UART allow you to have a serial console so you can connect to it, without needing network nor SSH.

It takes a few steps to change this default behavior so we can connect our Xbee (Series 1) to our RPI.

First of all, you will need to edit the /boot/cmdline.txt file :

sudo cp /boot/cmdline.txt /boot/cmdline.txt.bak # Backup file
sudo vi /boot/cmdline.txt

Remove all references to ttyAMA0 (console and kgdboc) so your file looks something like that :

dwc_otg.lpm_enable=0 rpitestmode=1 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait

Now edit /etc/inittab :

sudo cp /etc/inittab /etc/inittab.bak # Backup file
sudo vi /etc/inittab

Comment out the following line :

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

Now reboot your Raspberry PI.

Let’s now connect our Xbee to the RPI’s GPIO. Here is a quick schema explaining how :

Raspberry PI Xbee

Raspberry PI and Xbee Series 1

Note that on this schema, I only connected a wire from Xbee’s DOUT to RPI’s RXD as will only use it to receive data. However, you might also connect your Xbee’s DIN to RPI’s TX according to this RPI GPIO pinout.

Your Raspberry PI should now receive it’s first bytes via the Xbee ; you can test this using Minicom (sudo aptitude install minicom, if not already installed) :

sudo minicom -b 9600 -o -D /dev/ttyAMA0

(You could also use minicom without sudoing by adding your current user [I guess pi] to dialout group)

Special thanks to Clayton Smith’s blog post which greatly helped me figure out what was wrong with my RPI’s UART.

About 

Freelance PHP Symfony2 & Magento developer, passionate about programming and electronics.

  • googleplus
  • twitter
  • CHRISTIAN

    help me. Where to store the data transmitted by the xbee?
    What file?.

    please

    • Hi Christian. You may for example use a tiny Python script based on Python Serial library (http://pyserial.sourceforge.net/) to read data incoming on the serial port.

      Then you can process this data and store it wherever you like.

      Do not hesitate to ask if you’d like me to email you the portion of Python code I’m using.

      • Joris

        Wow, this was exactly what I was looking for, so thanks a lot for posting this. My end goal is to use Xbee modules to take in data from different sensors and use the RPi to send it to a MySQL database.
        Anyway, the reason of my email: if you can send me the Python code that you use for saving the data, that would be awesome. Hopefully I can use it, learn to understand it and adapt it for my needs.

        Thanks again for the effort and greetings from Belgium,
        Joris

        • bend94

          hi
          i try to connect Xbee to Raspberry pi but nothing works.
          my /boot/cmdline.txt looks like this:

          pi@raspberrypi:~ $ cat /boot/cmdline.txt
          dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

          i haven’t go /etc/inittab but not getty process on /dev/ttyAMA0:

          pi@raspberrypi:~ $ ps aux | grep AMA0
          pi 819 0.0 0.4 4252 1884 pts/0 S+ 21:35 0:00 grep –color=auto AMA0

          i try to open /dev/ttyAMA0 with minicom but still offline :

          Welcome to minicom 2.7

          OPTIONS: I18n
          Compiled on Jan 12 2014, 05:42:53.
          Port /dev/ttyAMA0, 21:24:01

          Press CTRL-A Z for help on special keys

          CTRL-A Z for help | 9600 8N1 | NOR | Minicom 2.7 | VT102 | Offline | ttyAMA0

          I have not idea, please help

          regards

          • Imen Aouf

            I had the same problem , did you find a solution please ?

      • Anupriya Ap

        hello Michael

        plz provide me the above link as the link is unavailable that you mentioned above.

        plzzzz

        With regards
        Anupriya

  • Juanra

    Good post.
    Is it posible to use another uart? I have only found shitching between UART1 and 0 in the same pins?

  • Henk

    Hello. i have this question, i want to record audio with a raspberry and send it via xbee to another raspberry, do you know wil this work?, and do you have any examples or links to help me?

  • Mario

    I tried it as described with to PIs and to xbee modules, but no connection. I connected the TXD to the 3thrd xbee pin. The minicom does nothing (offline, no user input possible). Any ideas?

    • Aaron

      Mario, to turn on local echo in minicom, type ctrl+a e

  • Lucas Batistussi

    Thanks guy! You saved my life! Love u!

  • dizzy dizzy

    u can connect an XBee to a Raspberry through the same USB adapter you use to configure it on a PC (i’m pretty sure). should show us as ttyUSB#

  • Gonzalo

    Awesome! Thanks a lot for this info!

    I’m also a PHP developer, from Argentina 🙂 and I’ve been playing with my RPi for the last months.
    Just to be clear, you are attaching the XBee module alone (see image) directly to your breadboard? Or are you using some shield/adapter in between?

    It would be great if you’re able to answer me!

    Best regards,

    Gonzalo

    http://mla-s1-p.mlstatic.com/modulo-rf-xbee-802154-series-1-de-1-mw-con-antena-monopolo-4009-MLA132206955_836-O.jpg

    • Michael BOUVY

      Hi Gonzalo and thanks for your comment,

      Unfortunately, pins spacing on XBee modules is 2mm instead of 2.54mm (0.1″), so you’ll need sort of an adapter like this one : https://www.sparkfun.com/products/11373

      Sorry for the late answer, hope it’ll help !

  • Isaac

    First congratulations by your work that was very usefull.

    I have this problem:
    I want communicate arduino and raspberry using xbee

    I’ve tryed use this code:

    import serial
    ser = serial.Serial(‘/dev/ttyAMA0′, 9600)
    ser.open()
    string = “Hello from Raspberry pi”
    print(‘Sending “%s” ‘ %string)
    ser.write(‘%s’ %string)

    while True:
    incoming = ser.readline().strip()
    print(‘Received %s’ %incoming)
    ser.write(‘RPi RECEIVED: %S’ %incoming)

    And this error appears:
    Import error: No module named serial.

    I’ve tested and the minicom works. I see the information sent from arduíno, but just using minicom because the code doesn’t work. Can you help about this problem?

    Thank you and one more time congratulations

  • James

    Hi there,

    I’m trying to find out if its possible to send .jpeg images via serial through the XBee. I’ve read many articles explaining that you need to use xmodem protocol to transmit such a file. Your explanation is the closest thing I’ve found so far so I would very much appreciate it if you could help with my situation.
    I’m attempting to use a Raspberry Pi to take motion activated .jpeg images and then transmit them with the XBee to another Raspberry Pi.

    • Miguel Lara

      Hi James,
      I am trying to do the same comminication between the raspberrys using XBee. I am wondering if you know how to configure the XBees using the raspberrys to help them talk to each other.

  • Suraj

    Hi there,
    I want the python script to get the data from the xbee.
    The like u gave to CHRISTIAN is expired.
    Please please help me out of tis problem.
    Thank You.

  • Sergiy

    It didn’t work to me, im not receiveng anything at minicom, I’ve tried also with cat and with a little python script and nothing. I’m pretty sure I’m sending data because I can see it in a windows laptop with X-CTU. Any ideas?

  • sky

    does this method applicable in xbee s2?

  • atishay

    hello, this was useful in receiving data from zigbee..but i want to send data also from raspberry pi. How to make it a two way communication.
    Thanks.