Raspberry Pi2 Based DC Motor Control Using ZigBee

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:

Modules Needed

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

import cv2

import RPi.GPIO asGPIO

from time import sleep

import serial

# Enable USB Communication

ser=serial.Serial('/dev/ttyUSB0',9600,timeout=.5)

GPIO.setmode(GPIO.BOARD)

Motor1=16

Motor2=18

Motor3=22

GPIO.setup(Motor1,GPIO.OUT)

GPIO.setup(Motor2,GPIO.OUT)

GPIO.setup(Motor3,GPIO.OUT)

motorS=GPIO.PWM(Motor3,100)

motorS.start(0)

motorS.ChangeDutyCycle(0)

ck=1

whileck==1:

incoming=ser.readline().strip()

print'Received Data : '+incoming

ifincoming=='1':

print"FORWARD MOTION"

GPIO.output(Motor1,GPIO.HIGH)

GPIO.output(Motor2,GPIO.LOW)

motorS.ChangeDutyCycle(99)

ifincoming=='2':

print"BACKWARD MOTION"

GPIO.output(Motor1,GPIO.LOW)

GPIO.output(Motor2,GPIO.HIGH)

motorS.ChangeDutyCycle(99)

ifincoming=='3':

print"STOP"

motorS.ChangeDutyCycle(0)

ifincoming=='4':

print"FINISHED"

ck=4

GPIO.cleanup()

ifincoming=='5':

print"Speed"

motorS.ChangeDutyCycle(20)

ifincoming=='6':

print"Speed"

motorS.ChangeDutyCycle(80)

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.