Raspberry Pi: Control DC Motor using MDDS10

There are several method to control DC motor using Raspberry Pi. The suitable method for your applications depends on how many PWM outputs you need and what performance you want out of those outputs.
There are only one hardware PWM output on the Raspberry Pi, so it difficult to control two DC Motor simultaneously. In this tutorial, we will use software PWM method to control 2 DC motor. There are some updates in RPi.GPIO of Raspberry Pi library that allow us to set any GPIO pins to become PWM pin. So, we can use any pin in this tutorial to become PWM pins.

Hardware Required

Software Required

1. Remote Desktop Connection

Step by Step (Hardware)

1. In this tutorial, we will use MDDS10 as our motor driver.

2. Now, we need to setup our connection first before jump to programming session. Don’t power up Raspberry Pi or MDDS10 yet in connecting process.

MDDS10Raspberry Pi
GNDGND (pin 20)
5V5V (pin 2)
AN2GPIO 25 (pin 22)
AN1GPIO 24 (pin 18)
DIG2GPIO 23 (pin 16)
DIG1GPIO 18 (pin 12)

Refer to GPIO pins below to help you connecting between Raspberry Pi and MDDS10

3. Connect left motor to MLA and MLB port and right motor to MRA and MRB port.

4. In this tutorial, we will use only one battery to run Raspberry Pi and motor driver. We will take 5V from MDDS10 board to power up Raspberry Pi.

5. Connect the battery to the MDDS10. Make sure the battery is connect with correct polarity. It is important to check and double-check any wiring before adding any power source to your project.

6. To check the movement of the left motor and right motor,

Press button MLB and MRB,  the motor should rotates forward.

Press button MLA and MRA,  the motor should rotates backward.

If the motor move in opposite direction, just change the polarity of the wire motor on MDDS10.

7. Make some adjustment to DIP Switch to make MDDS10 in PWM mode.

8. After finish up all of setting and connection, you can start to build a simple robot using this two DC Motor, MDDS10 and Raspberry Pi

Step by Step (Programming)

1. We will use Remote Desktop Connection in this tutorial to give a command to Raspberry Pi

2. Now we will control two DC Motor using RPi.GPIO module. To control pwm with Python, it’s requires RPi.GPIO 0.5.2a or higher. So we need to update and upgrade our Raspberry Pi to the new version. Open LX Terminal on your remote desktop connection. Type,

[code] sudo apt-get update [/code]

[code] sudo apt-get upgrade [/code]

 

3.  Create new file to test the movement of motor.

[code] sudo nano MotorTest.py[/code]

4. Setting pwm of motor by insert this following code to the file.

[code]
import RPi.GPIO as GPIO # using RPi.GPIO module
from time import sleep # import function sleep for delay
GPIO.setmode(GPIO.BCM) # GPIO numbering
GPIO.setwarnings(False) # enable warning from GPIO
AN2 = 25 # set pwm2 pin on MDDS10 to GPIO 25
AN1 = 24 # set pwm1 pin on MDDS10 to GPIO 24
DIG2 = 23 # set dir2 pin on MDDS10 to GPIO 23
DIG1 = 18 # set dir1 pin on MDDS10 to GPIO 18
GPIO.setup(AN2, GPIO.OUT) # set pin as output
GPIO.setup(AN1, GPIO.OUT) # set pin as output
GPIO.setup(DIG2, GPIO.OUT) # set pin as output
GPIO.setup(DIG1, GPIO.OUT) # set pin as output
sleep(1) # delay for 1 seconds
p1 = GPIO.PWM(AN1, 100) # set pwm for M1
p2 = GPIO.PWM(AN2, 100) # set pwm for M2
[/code]

5. To run the program in loop, insert:

[code]
try:
while True
[/code]

6. For forward function, insert:

[code]
print "Forward" # display "Forward" when program executed
GPIO.output(DIG1, GPIO.HIGH) # set DIG1 as high, dir2 = forward
GPIO.output(DIG2, GPIO.HIGH) # set DIG2 as high, dir1 = forward
p1.start(50) # set speed for M1, speed=0 – 100
p2.start(50) # set speed for M2, speed=0 – 100
sleep(1) # delay for 1 seconds
[/code]

7. For backward function, insert:

[code]
print "Backward" # display "Backward" when program executed
GPIO.output(DIG1, GPIO.LOW) # set DIG1 as low, DIR = backward
GPIO.output(DIG2, GPIO.LOW) # set DIG2 as low, DIR = backward
p1.start(50) # set speed for M1, speed=0 – 100
p2.start(50) # set speed for M2, speed=0 – 100
sleep(1) # delay for 1 seconds
[/code]

8. To stop the movement of motor and exit from executed, insert :

[code]
except: # exit program when keyboard interrupt
p1.start(0) # set speed M1=0
p2.start(0) # set speed M2=0
[/code]

9. Next, combine all of the function of the code.

10. Save and exit from file. CTRL+X, type Y and ENTER. To run the code, type:

[code] sudo python  MotorTest.py [/code]

To stop the movement of robot, press CTRL+C and the program will terminated.

11. If you see the message above, it means you need to do something with your file. Python uses indentation to define blocks and show which statements are controlled by a for or if statement. You need to indent the lines after the for, if or while. The indentation can be any consistent white space. In this tutorial we will use two spaces for indent.

12. Open the file to correct it and run the code again

13. Now you can see the robot will move forward and backward continuously until you press CTRL+C.

14. Next, we will proceed to program right and left movement

For left:

[code]
print "Left" # display "Left" when program executed
GPIO.output(DIG1, GPIO.LOW) # set DIG1 as low, dir2 = backward
GPIO.output(DIG2, GPIO.HIGH) # set DIG2 as high, dir1 = forward
p1.start(25) # set speed for M1
p2.start(25) # set speed for M2
sleep(1) # delay for 1 seconds
[/code]

For right:

[code]
print "Right" # display "Right" when program executed
GPIO.output(DIG1, GPIO.HIGH) # set DIG1 as high, dir1 = forward
GPIO.output(DIG2, GPIO.LOW) # set DIG2 as low, dir2 = backward
p1.start(25) # set speed for M1
p2.start(25) # set speed for M2,
sleep(1) # delay for 1 seconds
[/code]

15. Now you can try to combine and run all of the function.

Here is for left function that I combine with forward function

And here is for right function that I combine with forward function

16. You can change any value on the function above in term of delay or it speed based on your situation.

Ok, that’s all. If you have any question, feel free to ask in our technical forum as we seldom check the tutorial’s comment section. Thank you.

Buy

, , , , , , , , ,

Related Post

ThingSpeak: Store and Display Digital Compass Data to Web Services with Espresso Lite V2.0

Peak Current vs Continuous/Rated Current

How to Make your Line Following Robot Faster?

Raspberry Pi: Line Following Robot

2 thoughts on “Raspberry Pi: Control DC Motor using MDDS10

Leave a Reply

Your email address will not be published. Required fields are marked *