XBEEs + Python (pySerial) + Arduino + DC Motor

by thesharanmohan in Circuits > Arduino

1145 Views, 1 Favorites, 0 Comments

XBEEs + Python (pySerial) + Arduino + DC Motor

COVER PHOTO - INSTRUCTABLES.jpg

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.

Software - XCTU, Arduino IDE, PyCharm IDE.

SW_32.png

XCTU

XCTU_SW_32.PNG

Plug one of your XBee radios into an Explorer module and connect the module to your computer’s USB port. Launch the XCTU program.

Click “Discover Devices” and Select the Port to Be Scanned. Click “Next”.

XCTU_COM_32.PNG

Select the Port Parameters to Be Configured and Click “Finish”.

XCTU_SET_PORT_PARAMETERS.PNG

Your Radio Module Should Be Discovered Under the “Devices Discovered” List. Click “Add Selected Devices”.

XCTU_DISCOVERING_RADIO_32.png

Update the Radio Module Firmware.

RADIO_FW_MODULE_UPDATE_32.png

Click on “Read” in the Modem Configuration screen to see what firmware is on that radio. Each XBee radio should be running the newest firmware version of the ZigBee Coordinator AT or ZigBee Router AT. Update the radio firmware module by clicking “Update”.

PAN ID

ZIGBEE_ROUTER_COORDINATOR_AT_32.png

To get the radios talking, there are 3 important things we must check. The first is the “PAN ID”. The PAN ID is the network ID that these radios are going to talk on. We will be setting this to a unique value = 2019 (You can choose a value between 0 and 0xFFFF).

Addresses (SL and DL)

EXCHANGE_ADDR_SL_DL_32.PNG

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.

Hardware

HARDWARE_32.jpg

Hardware: XBee S2 radios (2), Breakout board for XBee module, XBee Explorer USB, Arduino board, L293D Motor Driver IC, DC Motor, 1k ohm resistors (2), LEDs (2), jumper wires, USB cables (A-to-B), 9V battery and breadboard.

Arduino IDE

ARDUINO_IDE_32.PNG

Connect the Arduino Mega to one of your computer’s USB ports and open Arduino IDE. Select the appropriate COM port and board. Upload the below program to the Arduino board by clicking on the Upload button.

Arduino IDE - Sketch

ARDUINO_IDE_UPLOAD_32.png
//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);
  }
}

Motor Driver IC Pinout

L293D_DRIVER_IC_L_32.PNG

For more information, check out this datasheet.
L293x Quadruple Half-H Drivers (http://www.ti.com/lit/ds/symlink/l293.pdf)



Circuit (Arduino + XBee + DC Motor)

FRITZING_CIRCUIT_32.PNG

Plug the XBee radio configured as the Coordinator into your XBee Breakout board and connect it to the Arduino by following the schematic.

NOTE: The Arduino Mega is powered by a 5V USB charger.

Hardware Setup

HARDWARE_FINAL_SETUP_32.jpg

Plug the XBee radio configured as the Router into your XBee Explorer module and connect it to one of your computer’s USB ports.

PyCharm IDE

PYCHARM_IDE_32.PNG

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#)

PyCharm IDE - Python Program

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()

Almost There! Click the "Run" Button in PyCharm IDE.

PYTHON_GUI_32.png

A simple GUI opens with 3 buttons - FORWARD, REVERSE and EXIT.

Depending on the motor connection wiring, the motor runs in the desired direction with the click of the FORWARD or REVERSE button. The EXIT button turns off the motor + LEDs and ends the program execution.

You've Finally Made It to the End of This Tutorial!!

XBEEs + PYTHON (pySerial) + ARDUINO + DC MOTOR