How to program a stepper motor on a Raspberry Pi?

Publisher:冰心独语uLatest update time:2024-01-05 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

A stepper motor is an electric motor that converts electrical pulse signals into corresponding angular displacement or linear displacement. For each input pulse signal, the rotor rotates an angle or moves forward one step. The output angular displacement or linear displacement is proportional to the number of input pulses, and the speed is proportional to the pulse frequency. Therefore, a stepper motor is also called a pulse motor.


Stepper motors usually need to be paired with a stepper motor driver module (also called a stepper motor driver) to work. A stepper motor driver is an actuator that converts electrical pulses into angular displacement. When the stepper driver receives a pulse signal, it drives the stepper motor to rotate a fixed angle (called the "step angle") in the set direction. Its rotation runs step by step at a fixed angle. The angular displacement can be controlled by controlling the number of pulses to achieve accurate positioning; at the same time, the speed and acceleration of the motor rotation can be controlled by controlling the pulse frequency to achieve speed regulation and positioning.


According to the different power-on sequences, four-phase stepper motors can be divided into three working modes: single four-beat, double four-beat, and eight-beat. The step angles of single four-beat and double four-beat are equal, but the torque of single four-beat is small. The step angle of eight-beat working mode is half of that of single four-beat and double four-beat. Therefore, the eight-beat working mode can maintain a high torque and improve control accuracy. The driver is equivalent to a combination unit of switches. The motor phase sequence is energized in sequence through the pulse signal of the host computer to make the motor rotate.


This article uses the 28BJY-48 stepper motor.


Hardware Hookup


First, you need to connect the stepper motor and the motor driver board. Simply insert the white plug of the motor into the white slot on the driver board (pay attention to the direction of the buckle).


Then connect the driver board and Raspberry Pico. The wiring is as follows:

The + pin of the motor driver board is connected to the VSYS of Pico.

The – pin of the motor driver board is connected to the GND of the Pico.

The IN1 pin of the motor driver board is connected to the GP18 of Pico.

The IN2 pin of the motor driver board is connected to the GP19 of Pico.

The IN3 pin of the motor driver board is connected to the GP20 of Pico.

The IN4 pin of the motor driver board is connected to the GP21 of Pico.


programming


Save the following code in the root directory of Pico as main.py.


from machine import Pin

import utime

# Initialize the pins of the stepper driver module

in1 = Pin(18,Pin.OUT)

in2 = Pin(19,Pin.OUT)

in3 = Pin(20,Pin.OUT)

in4 = Pin(21,Pin.OUT)

ROUND_VALUE = 509

delay = 1

# Sequence value of four-phase eight-beat stepper motor

STEP_VALUE = [

[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],

]

# Reset pin output low level

def reset():

in1(0)

in2(0)

in3(0)

in4(0)

# Single-step clockwise rotation, count is the step value, which can be negative (counterclockwise)

def step_run(count):

direction = 1

if count < 0:

direction = -1

count = -count

for x in range(count):

for bit in STEP_VALUE[::direction]:

in1(bit[0])

in2(bit[1])

in3(bit[2])

in4(bit[3])

utime.sleep_ms(delay)

# Rotate clockwise at a specified angle, a is the angle value, which can be negative (counterclockwise)

def step_angle(a):

step_run(int(ROUND_VALUE * a / 360))

# Reset the driver board

reset()

# Rotate 90 degrees counterclockwise

step_angle(-90)

# Continue to rotate clockwise in single step

while True:

step_run(1)

The program defines the driving mode and parameters of this stepper motor. There are two ways to drive it: step_angle (angle) or step_run (number of steps).


After running, you can see that the stepper motor first rotates 90 degrees counterclockwise and then continues to run clockwise.


Reference address:How to program a stepper motor on a Raspberry Pi?

Previous article:Share the correspondence table between 20 PLC component numbers and Modbus number addresses
Next article:What is a stepper motor? Simple stepper motor drive debugging

Latest Embedded Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号