Control the motor with a motor driver using python on Raspberry Pi 3!

Introduction

My name is Ryosuke Kamei and I am an IT consultant based on the idea of "gentle IT"! Currently, in my work, I am doing upstream processes focusing on requirements analysis and requirements definition, but I also write programs! As part of our friendly IT activities, we will introduce "Raspberry Pi 3" (commonly known as Raspberry Pi), which has the philosophy of "providing inexpensive PCs that can be programmed for education"! This time, we will control the motor! If you apply this, you will have a radio-controlled model with Raspberry Pi!

procedure

  1. Wiring
  2. Program motor.py
  3. Run the program

1. Wiring

Wiring diagram is [Razpai Magazine-June 2016 issue Special feature 1 Akiba's popular parts Wiring diagram best 17 ⑨ Motor can be rotated freely](https://www.amazon.co.jp/ Razpai Magazine-2016 June issue-Nikkei BP PC Best Mook-Nikkei Linux-ebook / dp / B01EH2RX42 / ref = as_li_ss_tl? -22 & linkId = 18fd04738627edef8d6fffd6c9b8f9c2) As it is.

The photo is as follows. Is it quite difficult to understand? ラズパイ×モーター配線図

I made a video for the time being. Isn't this also difficult to understand ... Click the video commentary →

I tried to wake it up in the text. Wiring seen from Raspberry Pi

--Raspberry Pi 2nd pin (5V) --Motor driver 7th (Vcc) --Raspberry Pi 16th pin (GPIO23) --Motor driver 5th (IN1) --Raspberry Pi 18th pin (GPIO24) --Motor driver 6th (IN2) --Raspberry Pi 6th pin (GND) --Motor driver 1st (GND) --Battery minus side

Wiring seen from the motor driver

--Motor driver No. 4 (Vref) --Resistance 5.1kΩ --Motor driver No. 8 (Vs) --Motor driver No. 8 (Vs) --Battery plus side --Motor driver No. 2 (OUT1) --DC motor --Motor driver No. 10 (OUT2) --DC motor

Other

--Condenser --DC motor --Condenser --DC motor

2. Program motor.py

The program is [Razpai Magazine-June 2016 Special Feature 1 Akiba's Popular Parts Wiring Diagram Best 17 ⑨ Motor can be rotated freely](https://www.amazon.co.jp/ Razpai Magazine-June 2016 No.-Nikkei BP Personal Computer Best Mook-Nikkei Linux-ebook / dp / B01EH2RX42 / ref = as_li_ss_tl? = 18fd04738627edef8d6fffd6c9b8f9c2) is changed so that arguments can be entered.

The source is uploaded to GitHub, so please use it as you like.

Clone with git


$ git clone https://github.com/RyosukeKamei/raspberrypi3.git

motor.py


#Library to control GPIO
import wiringpi
#Timer library
import time
#Get arguments
import sys

#GPIO terminal settings
motor1_pin = 23
motor2_pin = 24

#argument
param = sys.argv

#1st argument
# go :rotation
# back :back reverse
# break :brake
order = param[1]

#Second argument Seconds
second = int(param[2])

#Set GPIO output mode to 1
wiringpi.wiringPiSetupGpio()
wiringpi.pinMode( motor1_pin, 1 )
wiringpi.pinMode( motor2_pin, 1 )

if order == "go":
    if second == 0:
        print("Break 0 command to stop rotation!")
    else:
        print(str(second)+"Rotate seconds")
    wiringpi.digitalWrite( motor1_pin, 1 )
    wiringpi.digitalWrite( motor2_pin, 0 )
    time.sleep(second)
elif order == "back":
    if second == 0:
        print("Break 0 command to stop reverse rotation!")
    else:
        print(str(second)+"Second reverse rotation")    
    wiringpi.digitalWrite( motor1_pin, 0 )
    wiringpi.digitalWrite( motor2_pin, 1 )
    time.sleep(second)

#If the second argument is 0, do not brake
#If the first argument is break, brake
if order == "break" or second != 0:
    print("brake!")
    wiringpi.digitalWrite( motor1_pin, 1 )
    wiringpi.digitalWrite( motor2_pin, 1 )

3. Run the program

I recorded the video of how it is moving. Click the video commentary →

3-1. Arguments

The program has the following format: It is necessary to enter both the first argument and the second argument.

program


$ sudo python3 motor.py [1st argument:order] [2nd argument:The number of seconds]

The first argument is

There are three. Please enter one. (Actually, other than these three, message display and control are required, but it will be omitted because the program will be complicated.)

The second argument is the number of seconds. If you enter 1, it will rotate for 1 second. If you enter 0, it will rotate until it is "breaked" by the next command.

3-2. Rotate go

10 seconds rotation


$ sudo python3 motor.py go 10

Continue to rotate


$ sudo python3 motor.py go 0

3-3. Reverse rotation back

10 seconds rotation


$ sudo python3 motor.py back 10

Continue to rotate


$ sudo python3 motor.py back 0

3-4. Brake

brake


$ sudo python3 motor.py break 0

site map

Raspberry Pi 3 setup

Install Raspberry Pi 3 → Wireless LAN → Japanese input / output → Operate from Mac

Build a Python + MySQL environment with Docker on Raspberry Pi 3!

Install Docker on RaspberryPi3 Build a Python + bottle + MySQL environment with Docker on RaspberryPi3![Easy construction] Build a Python + bottle + MySQL environment with Docker on RaspberryPi3![Trial and error]

Make an air conditioner integrated PC "airpi" with Raspberry Pi 3!

Make an air conditioner integrated PC "airpi" with Raspberry Pi 3!

Let's play with Raspberry Pi 3 and python

Programming with Node-RED programming with Raspberry Pi 3 and programming normally Light the LED with python on Raspberry Pi 3 (Hello World) Detect switch status on Raspberry Pi 3 Run a servo motor using python on Raspberry Pi 3 Control the motor with a motor driver using python on Raspberry Pi 3! Detect slide switch using python on Raspberry Pi 3! Detect magnet switch using python on Raspberry Pi 3! Detect temperature using python on Raspberry Pi 3! Sound the buzzer using python on Raspberry Pi 3! Detect analog signals with A / D converter using python on Raspberry Pi 3! Detect "brightness" using python on Raspberry Pi 3! Detect "temperature (using A / D converter)" using python on Raspberry Pi 3! Output to "7-segment LED" using python on Raspberry Pi 3!

Rules focused on test-driven development

Coding rules "Let's write gentle code" (FuelPHP) Naming convention "Friendly to yourself, team-friendly, and unseen members after 3 months"

Web application development with Docker + Python

Install Python3, related libraries pip, virtualenv and frameworks Django, bottle, Flask on CentOS on Docker! With a Dockerfile that summarizes these!

Easy to develop environment construction (Docker + PHP)

PHP environment + Eclipse is linked to Apache using Docker Building FuelPHP development environment using Docker Create CRUD skeleton using initial settings of FuelPHP development environment using Docker and scaffold FuelPHP database migration

Recommended Posts

Control the motor with a motor driver using python on Raspberry Pi 3!
Finally ... Make a radio control using python on Raspberry Pi 3! (The motor moves while the button is pressed)
Sound the buzzer using python on Raspberry Pi 3!
Finally ... Make a radio control using python on Raspberry Pi 3! (The motor moves while the magnet is brought closer)
Detect analog signals with A / D converter using python on Raspberry Pi 3!
Use python on Raspberry Pi 3 to light the LED with switch control!
[Raspberry Pi] Stepping motor control with Raspberry Pi
Servo motor control with Raspberry Pi
Using a webcam with Raspberry Pi
Using the 1-Wire Digital Temperature Sensor DS18B20 from Python on a Raspberry Pi
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
Finally ... Make a radio control using python on Raspberry Pi 3! (When the magnet is brought closer, the motor moves and stops automatically)
Detect "brightness" using python on Raspberry Pi 3!
Run servomotor on Raspberry Pi 3 using python
Detect temperature using python on Raspberry Pi 3!
Working with GPS on Raspberry Pi 3 Python
[Note] Using 16x2-digit character LCD (1602A) from Python with Raspberry Pi
I wanted to run the motor with Raspberry Pi, so I tried using Waveshare's Motor Driver Board
Detect slide switches using python on Raspberry Pi 3!
Try using a QR code on a Raspberry Pi
Detect magnet switches using python on Raspberry Pi 3!
Connect to MySQL with Python on Raspberry Pi
Build a Python development environment on Raspberry Pi
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
Using the digital illuminance sensor TSL2561 with Raspberry Pi
[Raspberry Pi] Publish a web application on https using Apache + WSGI + Python Flask
Try debugging Python on Raspberry Pi with Visual Studio.
Try using the temperature sensor (LM75B) on the Raspberry Pi.
Make a breakpoint on the c layer with python
Control brushless motors with GPIOs on Raspberry Pi Zero
Information for controlling the motor with Python on RaspberryPi
Creating a temperature control system with Raspberry Pi and ESP32 (3) Recipient Python file
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Install pyenv on Raspberry Pi and version control Python
Output to "7-segment LED" using python on Raspberry Pi 3!
Control music playback on a smartphone connected to Raspberry Pi 3 and bluetooth with AVRCP
Access google spreadsheet using python on raspberry pi (for myself)
I tried using the DS18B20 temperature sensor with Raspberry Pi
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
Use vl53l0x with Raspberry Pi (python)
Try using ArUco on Raspberry Pi
Notes on using rstrip with python.
I learned how the infrared remote control works with Raspberry Pi
The story of making a standard driver for db with python.
Use python on Raspberry Pi 3 to illuminate the LED (Hello World)
How to upload a file to Cloud Storage using Python [Make a fixed point camera with Raspberry PI # 1]
Behind the flyer: Using Docker with Python
Build a Tensorflow environment with Raspberry Pi [2020]
Adafruit Python BluefruitLE works on Raspberry Pi.
A memo with Python2.7 and Python3 on CentOS
Map rent information on a map with python
Search the maze with the python A * algorithm
Working with OpenStack using the Python SDK
Programming normally with Node-RED programming on Raspberry Pi 3
Use the Grove sensor on the Raspberry Pi
Download files on the web with Python
Power SG-90 servo motor with raspberry pi
Working with sensors on Mathematica on Raspberry Pi
I tried using "Asciichart Py" which can draw a beautiful graph on the console with Python.
Make a wash-drying timer with a Raspberry Pi
Operate an oscilloscope with a Raspberry Pi