XBEEs + Python (pySerial) + Arduino + DC Motor

About: Electronics Engineer | Always in the phase of continuous learning

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.

  • In this tutorial we will use two XBee Series 2 modules configured with the following function sets: ZigBee Router AT and Zigbee Coordinator AT.
  • 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.
  • 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.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Software - XCTU, Arduino IDE, PyCharm IDE.

Step 2: XCTU

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

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

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

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

Step 6: Update the Radio Module Firmware.

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”.

Step 7: PAN ID

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

Step 8: Addresses (SL and DL)

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.

Step 9: Hardware

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.

Step 10: Arduino IDE

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.

Step 11: Arduino IDE - Sketch

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

Step 12: Motor Driver IC Pinout

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



Step 13: Circuit (Arduino + XBee + DC Motor)

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.

Step 14: Hardware Setup

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

Step 15: PyCharm IDE

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

Step 16: 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()

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

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.

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

Be the First to Share

    Recommendations

    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    Discussions