[PYTHON] I wanted to run the motor with Raspberry Pi, so I tried using Waveshare's Motor Driver Board

Video for the time being

IMAGE ALT TEXT HERE

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.

HAT condition

--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! !! 20191106_234520.jpg 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. 20191108_225610.jpg

Run the motor in Python

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.

Library import

It uses the familiar GPIO and time sleep library.

import RPi.GPIO as GPIO
from time import sleep

Variable declaration

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.

Declaration to use GPIO

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.

Motor control uses PWM

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.

Specify the speed of the motor

motor1.start(50)
motor2.start(50)

You can specify a speed from 0 to 100 as an argument.

Apply electricity to the motor

This is familiar.

Forward rotation

GPIO.output(PWMA1,1) #Port 6
GPIO.output(PWMA2,0) #Port 13

Current flows from port 6, and port 13 becomes GND.

Back rotation

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 **

Move for 3 seconds

Familiar sleep

sleep(3)

End

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

I wanted to run the motor with Raspberry Pi, so I tried using Waveshare's Motor Driver Board
I tried using the DS18B20 temperature sensor with Raspberry Pi
I tried to automate the watering of the planter with Raspberry Pi
When I tried to do socket communication with Raspberry Pi, the protocol was different
I tried to automate [a certain task] using Raspberry Pi
I tried to move ROS (Melodic) with the first Raspberry Pi (Stretch) at the beginning of 2021
I tried to make a motion detection surveillance camera with OpenCV using a WEB camera with Raspberry Pi
I want to run the Python GUI when starting Raspberry Pi
It's getting cold, so I tried to make it possible to turn on / off the AC heater automatically with Raspberry Pi!
I tried to estimate the pi stochastically
I tried to control multiple servo motors MG996R using the servo driver PCA9685.
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
Using COTOHA, I tried to follow the emotional course of Run, Melos!
I tried to save the data with discord
I wanted to play with the Bezier curve
I tried L-Chika with Raspberry Pi 4 (Python edition)
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
I tried to create a button for Slack with Raspberry Pi + Tact Switch
I wanted to know the number of lines in multiple files, so I tried to get it with a command
I tried to learn the sin function with chainer
Using the digital illuminance sensor TSL2561 with Raspberry Pi
Try to visualize the room with Raspberry Pi, part 1
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to approximate the sin function using chainer
I tried to solve the problem with Python Vol.1
I tried to identify the language using CNN + Melspectogram
I tried to complement the knowledge graph using OpenKE
I tried to compress the image using machine learning
I wanted to operate google spread sheet with AWS lambda, so I tried it [Part 2]
The first artificial intelligence. I wanted to try natural language processing, so I will try morphological analysis using MeCab with python3.
I tried to find the entropy of the image with python
I tried to simulate how the infection spreads with Python
I tried to analyze the whole novel "Weathering with You" ☔️
I tried using the Python library from Ruby with PyCall
I wanted to solve the Panasonic Programming Contest 2020 with Python
I tried to find the average of the sequence with TensorFlow
I tried to notify the train delay information with LINE Notify
A story that I wanted to realize the identification of parking lot fullness information using images obtained with a Web camera and Raspberry Pi and deep learning.
I tried to simulate ad optimization using the bandit algorithm.
I tried running Movidius NCS with python of Raspberry Pi3
I tried connecting Raspberry Pi and conect + with Web API
I talked to Raspberry Pi
I tried using the Pi Console I / F of the Raspberry Pi IoT starter kit "anyPi" from Mechatrax.
[TF] I tried to visualize the learning result using Tensorboard
[Shell startup] I tried to display the shell on the TV with a cheap Linux board G-cluster
I captured the Touhou Project with Deep Learning ... I wanted to.
I tried to divide the file into folders with Python
I tried to approximate the sin function using chainer (re-challenge)
I tried to output the access log to the server using Node.js
I sent the data of Raspberry Pi to GCP (free)
I tried to expand the database so that it can be used with PES analysis software
I tried to describe the traffic in real time with WebSocket
I tried to get the index of the list using the enumerate function
I learned how the infrared remote control works with Raspberry Pi
I made a web server with Raspberry Pi to watch anime
I tried to process the image in "sketch style" with OpenCV
I wanted to challenge the classification of CIFAR-10 using Chainer's trainer
I tried to digitize the stamp stamped on paper using OpenCV
I tried to get started with Bitcoin Systre on the weekend
I wanted to solve the ABC164 A ~ D problem with Python