Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Raspberry Pi
90 min
Share

How to control a stepper motor with raspberry pi 5 ?

There are many different kinds of motors, each of which has a variety of uses. One type is the stepper motor, which got its name because it moves in precise steps and allows you to control the movement speed and direction with high precision.

Project Video

Overview

In this tutorial, we will use the Raspberry pi 5 and motor driver to control the stepper motor. We will make it rotates a complete cycle in the right direction and then rotates a complete cycle in the opposite direction.

Getting the Items

Raspberry Pi 5 Single Board Computer
Get Item
5V DC Stepper Motor & Drive Module
Get Item
Jumper Wires - Female to Female (40 Pack)
Get Item

Steps

Wiring it Up

Connect the wires between the stepper motor driver module and the Raspberry pi 5 as shown in the image below.

Connections from the stepper motor driver module:


• Stepper motor driver module GND pin→ Raspberry pi 5 GPIO GND pin


• Stepper motor driver module VCC pin→ Raspberry pi 5 GPIO 5V pin


• Stepper motor driver module IN1 pin → Raspberry pi 5 GPIO pin 14


• Stepper motor driver module IN2 pin → Raspberry pi 5 GPIO pin 15


• Stepper motor driver module IN3 pin → Raspberry pi 5 GPIO pin 18


• Stepper motor driver module IN4 pin → Raspberry pi 5 GPIO pin 23

Coding

Now on your Raspberry Pi, click on the menu, then choose programming, then open the Thonny ide program.

Now copy that code into it, The function of this code is to make the Raspberry Pi 5 board controls the movement of the stepper motor so that it rotates a complete cycle in the right direction and then rotates a complete cycle in the opposite direction.

import gpiod

import time

# Define the control pins

ControlPin = [14, 15, 18, 23]

# Create a GPIO chip and get the lines

chip = gpiod.Chip('gpiochip4')

lines = [chip.get_line(pin) for pin in ControlPin]

# Configure the lines as outputs

for line in lines:

   line.request('stepper_motor', gpiod.LINE_REQ_DIR_OUT, 0)

# Define the segment pattern

seg_right = [

   [1, 0, 0, 0],

   [1, 1, 0, 0],

   [0, 1, 0, 0],

   [0, 1, 1, 0],

   [0, 0, 1, 0],

   [0, 0, 1, 1],

   [0, 0, 0, 1],

   [1, 0, 0, 1]

]

seg_left = [

   [0, 0, 0, 1],

   [0, 0, 1, 1],

   [0, 0, 1, 0],

   [0, 1, 1, 0],

   [0, 1, 0, 0],

   [1, 1, 0, 0],

   [1, 0, 0, 0],

   [1, 0, 0, 1]

]

delay = 0.001 #You can control the acceleration from the delay value !

# Run the stepper motor for 512 steps

for i in range(512):

   for halfstep in range(8):

       for pin in range(4):

           lines[pin].set_value(seg_right[halfstep][pin])

       time.sleep(delay)

for i in range(512):

   for halfstep in range(8):

       for pin in range(4):

           lines[pin].set_value(seg_left[halfstep][pin])

       time.sleep(delay)

       

# Release the GPIO lines

for line in lines:

   line.release()

Testing it Out

Now run the code, you will find that the Raspberry Pi 5 board controls the movement of the stepper motor so that it rotates a complete cycle in the right direction and then rotates a complete cycle in the opposite direction.

Resources

No items found.