Thursday, March 28, 2024

XBee Interfacing With Raspberry Pi Model 2

Arnav Bansal. He is pursuing B.E. in electronics and communication from UIET PUSSGRC, Hoshiarpur

Now the serial port of Raspberry Pi is disengaged from the boot sequence and can be used to communicate with XBee module. The default baud rate of the ttyAMA0 is 115200, but a baud rate of 9600 is required for XBee. You can change the baud rate in minicom software as given below:

 [stextbox id=”grey”]

pi@raspberrrypi ~ $ sudo minicom –b 9600

–o –D /dev/ttyAMA0

[/stextbox]// This will set the baud rate of the port ttyAMA0 to 9600

- Advertisement -

A window, as shown in Fig. 9, will come up. Press Ctrl+A keys followed by Q on the keyboard to exit without resetting.

Python code

Start your Raspberry Pi and create a new ‘XBeeTest’ folder. For this tutorial, the libraries used are ‘serial’ and ‘time.’ Create a new file using nano editor as given below:

 [stextbox id=”grey”]

- Advertisement -

$ pi@raspberrrypi ~ $ cd /XBeeTest

pi@raspberrrypi ~ /XBeeTest$ sudo nano

serial_XBee.py

Here serial_XBee.py is the name of the project. Type the following code:

import serial

// For serial communication

import time

 [stextbox id=”grey”]

[/stextbox]

// For delay function

 [stextbox id=”grey”]ser = serial.Serial(‘/dev/ttyAMA0’)[/stextbox]

// Set ser as object

 [stextbox id=”grey”]ser.baudrate = 9600[/stextbox]

// Set baud rate to 9600

[stextbox id=”grey”]

ser.write(“A”)

[/stextbox]

// Send character ‘A’

 [stextbox id=”grey”]time.sleep(1)[/stextbox]

// Give 1 second delay

 [stextbox id=”grey”]ser.write(“B”)[/stextbox]

// Send character ‘B’

 [stextbox id=”grey”]time.sleep(1)[/stextbox]

 // Give 1 second delay

[stextbox id=”grey”]ser.close()[/stextbox]

// Close communication

To save this code, press Ctrl+O keys followed by Ctrl+X.

fig 10Fig. 10: Receiver console
Fig. 10: Receiver console

This code sends character ‘A’ to the console (refer Fig. 10) of the receiver end, pauses for a second and then sends character ‘B.’

7 COMMENTS

  1. Hi,
    Thanks for sharing your knowledge. While I update the radio module firmware, I only have “ZIGBEE TH Reg”, “805.15.4 TH” and “DIGIMESH 2.4 TH” function set. Is it compulsory to have “XBEE COORDINATOR AT”. Could you please tell me how can I go forward with the available function sets.

SHARE YOUR THOUGHTS & COMMENTS

Electronics News

Truly Innovative Tech

MOst Popular Videos

Electronics Components

Calculators