There are several methods to control DC motors using Raspberry Pi. The suitable method for your application depends on how many PWM outputs you have and what performance you expect out of those outputs.
There is 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 motors. There are some updates in RPi.GPIO of Raspberry Pi library that allow us to set any GPIO pins to become PWM pin. So in this tutorial we will demonstrate how to use PWM on any pin.

HARDWARE REQUIRED

  1. Raspberry Pi + 8GB SD Card (Raspbian image)
  2. TP-Link WiFi Dongle
  3. Jumper Wire
  4. SmartDriveDuo-10
  5. DC Geared Motor
  6. LiPo Battery
  7. Robot Chassis

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 you finish setting up the hardware and connections, you can start building a simple robot using these two DC Motors, 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 the RPi.GPIO 0.5.2a (or higher) library. So we need to update and upgrade our Raspberry Pi to the newer version. Open LX Terminal on your remote desktop connection. Type,

  1. sudo apt-get update
  2. sudo apt-get upgrade

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

  1. sudo nano MotorTest.py

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

  1. import RPi.GPIO as GPIO # using RPi.GPIO module
  2. from time import sleep # import function sleep for delay
  3. GPIO.setmode(GPIO.BCM) # GPIO numbering
  4. GPIO.setwarnings(False) # enable warning from GPIO
  5. AN2 = 25 # set pwm2 pin on MDDS10 to GPIO 25
  6. AN1 = 24 # set pwm1 pin on MDDS10 to GPIO 24
  7. DIG2 = 23 # set dir2 pin on MDDS10 to GPIO 23
  8. DIG1 = 18 # set dir1 pin on MDDS10 to GPIO 18
  9. GPIO.setup(AN2, GPIO.OUT) # set pin as output
  10. GPIO.setup(AN1, GPIO.OUT) # set pin as output
  11. GPIO.setup(DIG2, GPIO.OUT) # set pin as output
  12. GPIO.setup(DIG1, GPIO.OUT) # set pin as output
  13. sleep(1) # delay for 1 seconds
  14. p1 = GPIO.PWM(AN1, 100) # set pwm for M1
  15. p2 = GPIO.PWM(AN2, 100) # set pwm for M2

5. To run the program in loop, insert:

  1. try:
  2. while True

6. For forward function, insert:

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

7. For backward function, insert:

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

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

  1. except: # exit program when keyboard interrupt
  2. p1.start(0) # set speed M1=0
  3. p2.start(0) # set speed M2=0

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:

  1. sudo python MotorTest.py

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 modify 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:

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

For right:

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

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.

That is the end of the tutorial! If you have any question, feel free to ask in our technical forum as we seldom check the tutorial’s comment section. Thank you.

REFERENCES

Credit to:

  1. Controlling DC Motors Using Python with a Raspberry Pi by Jason Barnett
  2. Driving two DC motors with a Raspberry Pi by shallowsky
  3. How to use software PWM by raspi.tv

BUY

3 thoughts on “Raspberry Pi: Control DC Motor using MDDS10”

  1. I am using the 4 channel cytron motor controller and am trying to power my Pi from it as well as the motors. The Pi seems to receive power and starts to power up, however I get the lightning bolt sign on the screen indicating it does not receive enough power, it then randomly shuts down and starts up again every time, making it unworkable to power the Pi from the battery through the motor controller.

    Is there a way I could solve this and still power the pi and motor driver from the same (14V) battery?

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorail

Dot Matrix Clock With NTP Server Using ESP32
Safe Box Using I2C LCD and 4×4 Keypad On Arduino.
Send Sensor’s Data to ThingSpeak Using Raspberry Pi
Getting Started with Raspberry Pi 4B-1GB Beginner Set
5 Major Upgrades on Raspberry Pi 4 Model B
Scroll to Top