Who reads these maniac articles? ..
Raspberry Pi has a lot of extension platforms called HAT that specialize in a certain field. This time, the mission to move the motor with Raspberry Pi came in. At first I worked hard on the breadboard, but it became troublesome and
No, I should look for a HAT! !! !!
That's why I started looking for it.
--All GPIOs of Raspberry Pi must survive. -It is possible to supply power to the Raspberry Pi itself with just the battery. --5000 yen or less --Two motors can be set
It was 4 points.
And I found this! !! It is a HAT called RPi Motor Driver Board made by Waveshare.
Even if you cover the Raspberry Pi with a HAT, you can use all GPIOs, Two motors can be connected to supply power to the Raspberry Pi! !! We use 6 AA batteries.
Example)
import RPi.GPIO as GPIO
from time import sleep
PWMA1 = 6
PWMA2 = 13
PWMB1 = 20
PWMB2 = 21
D1 = 12
D2 = 26
#Use GPIO port number
GPIO.setmode(GPIO.BCM)
#Set up GPIO for motor use
GPIO.setup(PWMA1,GPIO.OUT)
GPIO.setup(PWMA2,GPIO.OUT)
GPIO.setup(PWMB1,GPIO.OUT)
GPIO.setup(PWMB2,GPIO.OUT)
GPIO.setup(D1,GPIO.OUT)
GPIO.setup(D2,GPIO.OUT)
#PWM settings
motor1 = GPIO.PWM(D1,50)
motor2 = GPIO.PWM(D2,50)
#()Specify a speed from 0 to 100 inside
motor1.start(50)
motor2.start(50)
#Move the motor.
GPIO.output(PWMA1,1)
GPIO.output(PWMA2,0)
GPIO.output(PWMB1,1)
GPIO.output(PWMB2,0)
#Move the motor for 3 seconds
sleep(3)
#Stop the motor.
GPIO.output(PWMA1,0)
GPIO.output(PWMA2,0)
GPIO.output(PWMB1,0)
GPIO.output(PWMB2,0)
With the above program, it is possible to run two motors for 3 seconds for the time being. Let's look at the program in order from the top.
It uses the familiar GPIO and time sleep library.
import RPi.GPIO as GPIO
from time import sleep
Declare the GPIO port number.
PWMA1 = 6
PWMA2 = 13
PWMB1 = 20
PWMB2 = 21
D1 = 12
D2 = 26
Motor Driver Board can set two motors,
** Motor 1 with port number 12 ** ** Motor 2 port number 26 **
Seems to use.
When passing more electricity
** One of motor 1 has port number 6 ** ** One of motor 1 has port number 13 **
** One of motor 2 has port number 20 ** ** One of motor 2 has port number 21 **
Use the.
It is assumed that current will flow from one side and the other side will be GND.
GPIO.setup(PWMA1,GPIO.OUT)
GPIO.setup(PWMA2,GPIO.OUT)
GPIO.setup(PWMB1,GPIO.OUT)
GPIO.setup(PWMB2,GPIO.OUT)
GPIO.setup(D1,GPIO.OUT)
GPIO.setup(D2,GPIO.OUT)
This is also familiar.
The motor declares PWM after GPIO setup.
motor1 = GPIO.PWM(D1,50)
motor2 = GPIO.PWM(D2,50)
--Port number where you want to use PWM as the first argument --The second argument is the frequency. (Here, it is set to 50)
Originally, GPIO only supports digital signal input / output. Digital signals can only be turned on and off. Speed adjustment and LED brightness adjustment cannot be expressed only by turning on and off, and analog signals must be sent. Therefore, it is necessary to control the output voltage in a pseudo manner using this PWM method.
motor1.start(50)
motor2.start(50)
You can specify a speed from 0 to 100 as an argument.
This is familiar.
GPIO.output(PWMA1,1) #Port 6
GPIO.output(PWMA2,0) #Port 13
Current flows from port 6, and port 13 becomes GND.
GPIO.output(PWMA1,0) #Port 6
GPIO.output(PWMA2,1) #Port 13
Current flows from port 13, and port 6 becomes GND. This reverses the rotation of the motor.
** Motor 2 has the same movement, so it is omitted **
Familiar sleep
sleep(3)
By turning off all the current flowing through the GPIO, the motor will stop.
GPIO.output(PWMA1,0)
GPIO.output(PWMA2,0)
GPIO.output(PWMB1,0)
GPIO.output(PWMB2,0)
that's all.
I wonder if anyone uses such a maniac HAT. .. If you have any questions, please let us know what you are using and share the information!
Recommended Posts