[Raspberry Pi] Stepping motor control with Raspberry Pi

Introduction

I bought a Raspberry Pi thinking that I might find a new hobby that I can do indoors during lockdown. I thought that if I could control the motor, it would be useful in experiments later, so I bought a stepper motor together, so I will try to control it. It is interesting just to rotate at a constant speed, but the goal was to be able to do any exercise. Knowledge of electrical and electronic circuits I started from almost zero, but I could easily do it because there was a lot of information on the net. I really appreciate it. It seems that it can be controlled in various ways, but I would like to introduce the method that I found the easiest.

hardware

Below is a list of the hardware used.

Constant speed rotation

First is the basic constant speed rotation. Use RpiMotorLib, A Raspberry pi python motor library on GitHub. It seems that not only stepper motors but also DC motors and servo motors can be controlled. You can install it from the terminal with sudo pip install rpimotorlib. After installing the library, wire it according to the Very polite tutorial and just copy and paste the code to turn the motor. I will. I wired it as follows (sorry for the hand-drawn rough drawing). The resolution of the image is poor and it is difficult to see, but the wiring is as shown in the table below.

Raspberry GPIO driver motor
17 IN1 -
18 IN2 -
27 IN3 -
22 IN4 -
- OUT1 A-
- OUT2 B-
- OUT3 A+
- OUT4 B+

The main function is motor_run (GpioPins, wait, steps, ccwise, verbose, steptype, initdelay). The explanation of each argument is as follows.

For example, if you want to rotate 5Hz counterclockwise at a rotation speed of 0.5Hz in half-step mode, the code is as follows. It's very simple.

python


import time 
import RPi.GPIO as GPIO
from RpiMotorLib import RpiMotorLib  
    
GpioPins = [17, 18, 27, 22]  #GPIO pin. IN1 from the left, IN2, IN3,Pin to connect to IN4.

f = 0.5  #Rotational speed[Hz]
Rev = 5  #Rotate 5 times
s_angle = 1.8  #Step angle[deg]
wait = (1/f)*(s_angle/360)/2  #Time between pulses. Half-Divide by 2 for step mode

#Give the motor a name and specify the motor type (Nema or 28BYJ).
mymotortest = RpiMotorLib.BYJMotor('MyMotorOne', 'Nema') 

#Main. Arguments are GPIO pin, wait, number of steps, reverse rotation, verbose, step mode, first delay from the left[ms]
mymotortest.motor_run(GpioPins, wait, Rev*50, True, False, 'half', 0.05)

#Finally clean up the pins
GPIO.cleanup()

stepper_test.gif

pleasant!

I want to do any exercise

Since I was able to turn it, I think I can do any exercise depending on the chord. I would like to make the motor move arbitrarily. As an example, let's make a sine wave pitch motion. 7 in 1 step.Since it rotates 2 °, the amplitude\theta_0Should be a multiple of that. This time it will be 72 °. periodTWas set to 2 seconds. Angular velocity\omega = \dot{\theta}(In this example\cos)but,waitCan only take a positive value, so take an absolute valuewait = (1/2)\times(1/|\omega|)\times(1.8/360)Is passed. And\omega < 0WhenccwiseTo'True'All you have to do is pass.\theta\omega|\omega|waitは下図のようToなります.

The code is as follows.

python


GpioPins = [17, 18, 27, 22]

T = 2  # Period [s]
N_period = 2  # No. of periods
amp = 72
unit_angle = 7.2
s_angle = 1.8  # [deg]
N = int(4*amp/unit_angle)

theta = amp*sin(np.linspace(0, 2*pi, N))  # Angle time history [deg]
dt = T/len(theta)  # time-step [s]
omega = np.gradient(theta, dt)/360  # [Hz]
omega_pos = abs(omega)
pm = omega < 0  # boolean for CW/CCW 
wait = (1/omega_pos)*(s_angle/360)/2

mymotortest = RpiMotorLib.BYJMotor, 'MyMotorOne', 'Nema')
time.sleep(0.002)

for _ in np.arange(N_period):
    for k in np.arange(N):
        mymotortest.motor_run(GpioPins, wait[k], 1, pm[k], False, "half", 0)

GPIO.cleanup()

Sinu_Pitch.gif

did it!

Other referenced sites

Recommended Posts

[Raspberry Pi] Stepping motor control with Raspberry Pi
Servo motor control with Raspberry Pi
Power SG-90 servo motor with raspberry pi
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
Control the motor with a motor driver using python on Raspberry Pi 3!
Mutter plants with Raspberry Pi
Control brushless motors with GPIOs on Raspberry Pi Zero
Use vl53l0x with Raspberry Pi (python)
Serial communication with Raspberry Pi + PySerial
OS setup with Raspberry Pi Imager
Production of temperature control system with Raspberry Pi and ESP32 (1)
Try L Chika with raspberry pi
VPN server construction with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Using a webcam with Raspberry Pi
I learned how the infrared remote control works with Raspberry Pi
Measure SIM signal strength with Raspberry Pi
Pet monitoring with Rekognition and Raspberry pi
Hello World with Raspberry Pi + Minecraft Pi Edition
Build a Tensorflow environment with Raspberry Pi [2020]
Get BITCOIN LTP information with Raspberry PI
Try fishing for smelt with Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
Improved motion sensor made with Raspberry Pi
Try Object detection with Raspberry Pi 4 + Coral
Working with sensors on Mathematica on Raspberry Pi
Use PIR motion sensor with raspberry Pi
Make a wash-drying timer with a Raspberry Pi
Infer Custom Vision model with Raspberry Pi
Operate an oscilloscope with a Raspberry Pi
Create a car meter with raspberry pi
Inkbird IBS-TH1 value logged with Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
Use python on Raspberry Pi 3 to light the LED with switch control!
Raspberry Pi backup
Production of temperature control system with Raspberry Pi and ESP32 (2) Production of transmission device
Discord bot with python raspberry pi zero with [Notes]
Control Akizuki's I2C-connected OLED display from Raspberry Pi
Media programming with Raspberry Pi (preparation for audio)
I tried L-Chika with Raspberry Pi 4 (Python edition)
Enjoy electronic work with GPIO on Raspberry Pi
MQTT RC car with Arduino and Raspberry Pi
Power on / off your PC with raspberry pi
Use Majoca Iris elongated LCD with Raspberry Pi
CSV output of pulse data with Raspberry Pi (CSV output)
Observe the Geminids meteor shower with Raspberry Pi 4
Get CPU information of Raspberry Pi with Python
Play with your Ubuntu desktop on your Raspberry Pi 4
Get temperature and humidity with DHT11 and Raspberry Pi
Stock investment analysis app made with Raspberry Pi
Logging Inkbird IBS-TH1 mini values with Raspberry Pi
Connect to MySQL with Python on Raspberry Pi
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
Measure CPU temperature of Raspberry Pi with Python
Creating a temperature control system with Raspberry Pi and ESP32 (3) Recipient Python file
Record temperature and humidity with systemd on Raspberry Pi
Machine learning with Raspberry Pi 4 and Coral USB Accelerator
Run LEDmatrix interactively with Raspberry Pi 3B + on Slackbot
Using the digital illuminance sensor TSL2561 with Raspberry Pi
What is Raspberry Pi?