In the previous tutorials we had seen the Zigbee communication and DC motor control in Raspberry Pi2. Now we have to interface these two modules for developing an intelligent embedded system. Our intention is to control the speed and rotation of the DC motor on the basis of commands received through a Zigbee module. A suitable paired Zigbee is connected to a PC for transmitting the commands, and there by running the DC motor through wireless communication system.
Apart from the previous tutorial there is an additional process that is to control the speed of the DC motor by means of pulse width Modulation (PWM). The amount of power going to the motor is controlled by changing the duty cycle. As the Duty cycle changes the width of on time and off time will be changed and a suitable power control is retrieved by the motor. If the duty cycle is given as 10 %, and the total pulse width is 1ms, then 0.1ms is for on time and the rest 0.9ms is for off time. A graphical representation is shown below:
- Raspberry Pi2
- Plastic Gear Motor
- L293D ic
- Power supply or Battery (5v)
- Bread Board
- Connecting Wires
- XBee 1mW Wire Antenna- Series 1 (2 No:)
- XBee Explorer Dongle (2 No:)
So here we can change the duty cycle from 0 to 99. The connection diagram is shown below:
L293D Interfacing Diagram
Before doing the process check the zigbee communication with Raspberry Pi. Click here to see the zigbee interface with Raspberry Pi2.
After that make the connections as shown in the figure and power up the module. Write a Python script to test the process.
import RPi.GPIO as GPIO
from time import sleep
# Enable USB Communication
ser = serial.Serial('/dev/ttyUSB0', 9600,timeout=.5)
Motor1 = 16
Motor2 = 18
Motor3 = 22
motorS = GPIO.PWM(Motor3,100)
incoming = ser.readline().strip()
print 'Received Data : '+ incoming
if incoming == '1' :
print "FORWARD MOTION"
if incoming == '2' :
print "BACKWARD MOTION"
if incoming == '3' :
if incoming == '4' :
if incoming == '5' :
if incoming == '6' :
Open the Hyperterminal in the PC where the Zigbee is connected. The modes of operations of the DC motor is differentiated on the basis of the commands transmitted through the Zigbee. Check the Motor movements by entering the commands mentioned in the code.
Shop With Us
- Click here to buy Raspberry Pi 2 from RhydoLabz.
- Click here to buy Raspberry Pi 2 Starter Kit from RhydoLabz.
- Click here to buy Raspberry Accessories from RhydoLabz.
- Click here to buy XBee 1mW Wire Antenna-Series 1 from RhydoLabz
- Click here to buy XBee Explorer Dongle from RhydoLabz.
- Click here to buy plastic geared DC Motor
- An open source website by Raspberry Pi foundation.
- RPi Serial Connection.
- Get started with Python programming.
- Serial Communication using Raspberry Pi
- FTDI Virtual COM Port Driver Installation
- Test your XBee Explorer Dongle with PC
- X-CTU Software
- L293D Datsheet
- Raspberry Pi2 ZigBee Interface
- Raspberry Pi2 Interfaced with DC Motor