Skip to content

Interfacing XBEE for UART communication with Raspberry Pi

Introduction

In this brief guide, we will explore how to establish XBee communication with a Raspberry Pi to verify the UART (Universal Asynchronous Receiver-Transmitter) functionality. XBee is a popular wireless communication module that allows for reliable and efficient communication between devices. By connecting an XBee module to the Raspberry Pi's UART pins, we can test and verify the UART communication capabilities of the Raspberry Pi. UART is a standard serial communication protocol that enables data exchange between devices. By successfully establishing XBee communication and verifying UART functionality, we can ensure the Raspberry Pi is ready for various wireless communication applications. Let's dive in and explore the world of XBee communication with the Raspberry Pi!

Problem Statement

Using XBEE verify UART communication with Raspberry Pi.

Requirements

  1. Raspberry Pi
  2. XBEE - S2C(2 XBEE'S)
  3. USB to serial converter - 2
  4. Jumper wires

Working

To verify UART communication with a Raspberry Pi using XBee modules, we begin by connecting the XBee module to the appropriate UART pins on the Raspberry Pi. We configure the Raspberry Pi's UART settings, such as baud rate and parity, to match the settings of the XBee module. Once the hardware setup is complete, we can write a Python program to establish communication with the XBee module using the Raspberry Pi's UART interface. This program sends and receives data to and from the XBee module, confirming the successful functioning of UART communication. By observing the data exchange between the Raspberry Pi and the XBee module, we can verify that the UART communication is working correctly, ensuring a reliable and efficient wireless communication setup using XBee modules with the Raspberry Pi.

Setting up Raspberry Pi for Serial Communication

Setting up Raspberry Pi for Serial Communication In the terminal, run the command "sudo raspi-config", the settings tab for enabling might be slightly different but follow the keys as mentioned.

Raspberry Pi

Go to option Interfacing options and hit the enter. Now, select the Serial option and Enable it and then save.

Raspberry Pi

Configure XBEE modules using XCTU

To connect XBEE module with the laptop, a USB to serial converter is used.

Download the XCTU software for Windows or Linux and install it. After downloading and installing the XCTU software, open it and make sure your XBee module is properly connected. Check the COM port in the Device Manager->Check the COMX (X->number) under PORT.

Open the application after downloading it.

The transmitting(connecting) XBEE is connected directly to one PC/laptop and the receiver(end device) is connected to Raspberry Pi connected directly/indirectly to same PC/laptop(if the above approach does not work configure transmitting and receiving XBEE's to 2 different PC/laptop).

  1. Now, click on the search button. This will show you all the RF devices connected with your laptop. In our case, it will show only one XBee module.
Raspberry Pi
  1. Select the Serial port.
Raspberry Pi
  1. In the next window, set the USB port parameters as shown below and click on Finish.
Raspberry Pi
  1. Select the Discovered device and click on Add selected device. This process will add your XBee module to XCTU dashboard.
Raspberry Pi
  1. Now, you can configure your XBee module in this window. You can use either AT commands or put the data manually. As you can see, there is R showing on the left panel which means XBee is in router mode. We have to make it Coordinator for the transmitter part.

First, update the Firmware by clicking on the Update firmware.

Raspberry Pi
  1. Choose the Product family of your device which is available on back of your XBee module. Select function set and firmware version as highlighted below and click on Update.
Raspberry Pi
  1. Now, you have to give ID, MY and DL data to make connection with other XBee. ID remains same for both the modules. Only MY and DL data interchange i.e. MY for the receiver XBee becomes DL of the transmitter XBee (coordinator) and DL for the receiver XBee becomes MY of the transmitter XBee. Make CE as Coordinator and then hit the Write button. As shown below.
Raspberry Pi

XBee 1 coordinator

  • DL- 1234
  • MY-5678I
  • ID-2244

XBee 2 end device

  • DL-5678
  • MY-1234
  • ID-2244
  1. After writing the above data to the transmitter part, plug in the second XBee module in it. Repeat the same process as above only changes are the DL, MY, and CE. As we will make the second XBee as End device so in CE drop down menu, select the End device and hit the Write button.

  2. Now, our XBee modules are ready to interface with the Raspberry Pi. We will connect the transmitter XBee to the laptop and receiver XBee with the Raspberry Pi. Then give commands to the receiver part using laptop.

Circuit Diagram

(End device XBEE connection to Raspberry Pi)

  • Connect the XBee module to the RPi using a USB to serial converter (similar to how the first XBee was connected to the laptop)

Raspberry Pi

Testing Wireless XBEE Communication with Raspberry Pi connected XBEE

Now, we all set to test our XBee transmitter and receiver. To give command to the transmitter part, we will use XCTU’s console terminal. Click on the Console icon near the settings option. Then, click on Open button to connect the XBee to the laptop.

Enter ‘a’ in Console log. You will see that LED will turn ON for 3 seconds and then it turn OFF.

Raspberry Pi

Code

Now let's write the Python code for UART. Follow the steps below:

  1. Connect to your Raspberry Pi using VNC or Desktop environment.
  2. Launch the Geany Python IDE or open a text editor to write the code.
  3. Create a new file named uart.py and save the following code into it
  4. You can copy and paste the following code from here into the file.
py
#!/usr/bin/env python
import time
import serial
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

ser = serial.Serial(
    port='/dev/ttyS0',  # Corrected the serial port path
    baudrate=9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
)

counter = 0

while True:
    ser.write(str.encode('Write counter: %d \n' % counter))
    time.sleep(1)
    counter += 1
    print("Counter value = ", counter)
    x = ser.readline().strip().decode()  # Decoded the received data to string
    print("Message receuved from coordinator = ", x)
  1. After saving the file execute it by clicking on messenger like icon on geany editor or open terminal and navigate to the folder you saved the file, execute it by typing python uart.py
  2. While running the code, by default the counter value will be printed. By sending a message from the coordinator, you can verify whether the message is received by the second Xbee module.

Output

You should be able to see the data sent from the console log of the Connector XBEE to the End Device XBEE connected to Raspberry Pi and the data displayed on terminal.

Conclusion

By following this guide, you have learned how to use XBee modules for UART communication with a Raspberry Pi. You have configured the XBee modules using XCTU, wired them to your Raspberry Pi, and written the necessary Python code to establish a reliable communication link between devices. This opens up a wide range of possibilities for your Raspberry Pi projects that require wireless communication.

With XBee modules, you can easily exchange data between multiple Raspberry Pis or other devices over a wireless network. You can create remote control systems, sensor networks, or even build interactive projects that communicate wirelessly. The flexibility and reliability of XBee modules make them a valuable addition to your Raspberry Pi toolkit.

By integrating XBee modules into your projects, you can extend the functionality and create more advanced applications.

The combination of Raspberry Pi and XBee modules gives you the power to create robust and versatile wireless solutions.