Arduino and XBees can work very well together in wireless sensor systems. With the help of an Arduino, XBees can interact with GPS modules, drive large motors, drive LCD display screens, store data in local memory banks and interact directly with the Internet via Wi-Fi or your mobile phone. Working together, the possibilities are limitless.
P1. In this tutorial we will use two XBee Series 2 modules configured with the following function sets: ZigBee Router AT and Zigbee Coordinator AT.
P2. The module configured as the Router is connected to the PC via an XBee Explorer USB and the module configured as the Coordinator is connected to the Arduino board. XBee Series 2 modules have a typical (indoor/urban) range of up to 40 meters.
P3. We’ll create a simple Python GUI that can control the movement of a DC Motor connected to an Arduino board, wirelessly from the PC.
Every XBee radio has a 64-bit serial number address printed on the back. The beginning (SH) or “high” part of the address will be 13A200. The last or “low” (SL) part of the address will be different for every radio. To make the XBee radios chat with each other, the addresses of the Router and the Coordinator are switched as shown in the figure.
Click on the “Write” button to program your radio. Once you’ve finished configuring your first radio with the required configuration software, gently remove that radio from the explorer module and carefully seat a second radio in the same module and repeat the above steps.
//XBEE - CO-ORDINATOR AT
//PIN CONNECTIONS //Pin 1 - 3.3V | Pin 10 - GND //Pin 2 - RX0 | Pin 3 - TX0
//LED pin connections const int redLED = 30; const int greenLED = 32;
char incoming_data = '\0';
// Motor Driver IC connections //enA - Pin (1) of L293D //in1 - Pin (2) of L293D //in2 - Pin (7) of L293D
int enA = 9; int in1 = 8; int in2 = 7;
void setup() { Serial.begin(9600); pinMode(redLED, OUTPUT); pinMode(greenLED, OUTPUT); digitalWrite(redLED, LOW); digitalWrite(greenLED, LOW);
pinMode(enA, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT);
digitalWrite(in1, LOW); digitalWrite(in2, LOW); }
void loop() { //check for incoming data if (Serial.available() > 0) { //read the incoming data incoming_data = Serial.read();
if (incoming_data == 'r') { //Set motor to max speed analogWrite(enA, 255);
//Red LED - ON digitalWrite(redLED, HIGH); digitalWrite(greenLED, LOW);
//Motor Reverse digitalWrite(in1, HIGH); digitalWrite(in2, LOW); } else if (incoming_data == 'g') { //Set motor to max speed analogWrite(enA, 255);
//Green LED - ON digitalWrite(redLED, LOW); digitalWrite(greenLED, HIGH);
//Motor Forward digitalWrite(in1, LOW); digitalWrite(in2, HIGH); } else if (incoming_data == 'q') { //Turn OFF LEDs digitalWrite(redLED, LOW); digitalWrite(greenLED, LOW);
//Turn OFF motors digitalWrite(in1, LOW); digitalWrite(in2, LOW); } delay(1000); } }
For more information, check out this datasheet.
L293x Quadruple Half-H Drivers (http://www.ti.com/lit/ds/symlink/l293.pdf)
Before running the program below, make sure that the pySerial package is installed. PySerial is a Python library which provides support for serial connections over a variety of different devices.
To install any package in PyCharm IDE, follow the below steps:
1. File -> Settings.
2. Under Project, select Project Interpreter and click on the “+” icon.
3. In the search bar, type pySerial and click on Install Package.
NOTE: Make sure the COM port number that is used in the Python program is that of the XBee Explorer module connected to the PC. The COM port number can be found in Device Manager -> Ports (COM#)
import serial import time import tkinter as tk
window = tk.Tk() window.configure(background="dark orange") window.title("BASIC GUI")
megaBoard = serial.Serial('COM3', 9600)
def motor_control(): print(">>> XBEE + MOTOR CTRL - PROGRAM <<<\n") def forward(): print("CTRL -> FORWARD + GREEN LED -> ON") megaBoard.write(b'g')
def reverse(): print("CTRL -> REVERSE + RED LED -> ON") megaBoard.write(b'r')
def quit(): print("\n** END OF PROGRAM **") megaBoard.write(b'q') megaBoard.close() window.destroy()
b1 = tk.Button(window, text="FORWARD", command=forward, bg="lime green", fg="gray7", font=("Comic Sans MS", 20))
b2 = tk.Button(window, text="REVERSE", command=reverse, bg="firebrick2", fg="ghost white", font=("Comic Sans MS", 20))
b3 = tk.Button(window, text="EXIT", command=quit, bg="gold", fg="gray7", font=("Comic Sans MS", 20))
l = tk.Label(window, text="XBEE + MOTOR CTRL - PYTHON", bg="SlateBlue1", font=("Comic Sans MS", 15))
b1.grid(row=1, column=1, padx=5, pady=10) b2.grid(row=2, column=1, padx=5, pady=10) b3.grid(row=3, column=1, padx=5, pady=10) l.grid(row=4, column=1, padx=5, pady=10)
window.mainloop()
time.sleep(2) motor_control()
Check out this short video that shows the entire operation.
References:
1. Building Wireless Sensor Networks By Robert Faludi:
http://shop.oreilly.com/product/9780596807740.do
2. XCTU:
https://www.digi.com/products/embedded-systems/digi-xbee-tools/xctu
3. pySerial:
https://pyserial.readthedocs.io/en/latest/shortintro.html
4. PyCharm:
https://www.jetbrains.com/pycharm/
5. Tkinter:
https://docs.python.org/2.7/library/tkinter.html#
6. TI L293x Quadruple Half-H Drivers:
http://www.ti.com/product/L293D/technicaldocuments?dcmp=dsproject&hqs=td&#doctype2