Motor control with Raspberry Pi

Motor control with Raspberry Pi

Hello and welcome back. In this tutorial, we will learn how to control a DC gear motor and stepper motor using a Raspberry pi board. For that, I used the L298N motor driver board. Through this board, we can control two gear motors and one stepper motor. Therefore, you can use this knowledge to make any DC gear and stepper motor-based projects. Also, you can buy this motor driver board at a cheap price in the market.

If you want to know more info about this L298N motor driver board and how it works with Arduino, please use this link.

Ok, let’s do it. Step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, connect the GPIO Extension board to the breadboard. And then, connect the GPIO Ribbon cable to the extension board.

DC gear motor control

Step 1

First, connect the motor driver board to the GPIO Extension board. For that, use the circuit diagram below.

Motor control with Raspberry Pi

Step 2

Next, connect the gear motor to the L298N motor driver board.

Step 3

Now, provide an external 12VDC power supply to the motor driver board. Use the circuit diagram above for that.

Step 4

Next, connect the mouse, keyboard, monitor, and SD card (OS installed) to the Raspberry Pi board.

Step 5

Finally, connect the GPIO ribbon cable and power supply to the Raspberry Pi board.

Step 6

Finally, copy and paste the following script on the Thonny IDE, then, save this script and click the Run button.

  • Full details of this project — Download
# Include the library files
import RPi.GPIO as GPIO
from time import sleep

# Include the motor control pins
ENA = 17
IN1 = 27
IN2 = 22

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(ENA,GPIO.OUT)
GPIO.setup(IN1,GPIO.OUT)
GPIO.setup(IN2,GPIO.OUT)


def forward():
    GPIO.output(ENA,GPIO.HIGH)
    GPIO.output(IN1,GPIO.HIGH)
    GPIO.output(IN2,GPIO.LOW)


def backward():
    GPIO.output(ENA,GPIO.HIGH)
    GPIO.output(IN1,GPIO.LOW)
    GPIO.output(IN2,GPIO.HIGH)

while True:
    forward()
    sleep(1)
    backward()
    sleep(1)

Stepper motor control

Step 1

First, connect the motor driver board and stepper motor as below. For this also connect an external 12VDC power supply.

Step 2

Now, connect the power supply to the Raspberry Pi board.

Step 3

Finally, copy and paste the following script on the Thonny IDE. After, save this script and click the Run button.

  • Full details of this project — Download
# Include the library files
import RPi.GPIO as GPIO
from time import sleep

# Include the motor control pins
ENA = 17
IN1 = 27
IN2 = 22
IN3 = 20
IN4 = 21

ControlPin = [IN1,IN2,IN3,IN4]

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(ENA,GPIO.OUT)
GPIO.setup(IN1,GPIO.OUT)
GPIO.setup(IN2,GPIO.OUT)
GPIO.setup(IN3,GPIO.OUT)
GPIO.setup(IN4,GPIO.OUT)


StepCount = 8
#Seq = range(0, StepCount)

seq = [[1,1,0,0],
       [0,1,1,0],
       [0,0,1,1],
       [1,0,0,1],
]

GPIO.output(ENA, 1)
rotetionNeeded = 0
rotetionCount = 200
    

def forward():
        for i in range (rotetionCount):
            for fullStep in range(4):
                for pin in range (4):
                    GPIO.output(ControlPin[pin],seq[fullStep][pin])
                    sleep(0.001)
    

def backward():
        for i in range (rotetionCount):
            for fullStep in range(4):
                for pin in reversed(range (4)):
                    GPIO.output(ControlPin[pin],seq[fullStep][pin])
                    sleep(0.001)

while True:
    forward()
    sleep(2)
    backward()
    sleep(2)



#GPIO.cleanup()

OK, enjoy this tutorial, The full video guide is below. So, see you in the next project or tutorial.

Motor control with Raspberry Pi

Similar Posts

Leave a Reply

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