Run servomotor on Raspberry Pi 3 using python

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 introduce "servo motors" used for robot arms and the like! The tension rises just by the motor ringing (laughs)

1. Wiring photo

2. Source servo_motor.py

The description of the program is given in the comments starting with #.

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

Clone with git


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

servo_motor.py


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

#Specify the GPIO terminal number connected to the servo motor
servo_pin  =  18
#Specify the angle to move the servo motor
# set_degree =90 default
#Receive a value from an argument
param = sys.argv
set_degree = int(param[1])
print(set_degree)

wiringpi.wiringPiSetupGpio()
#Output with hardware PWM
wiringpi.pinMode( servo_pin, 2 )
#PWM waveform setting according to the servo motor
wiringpi.pwmSetMode(0)
wiringpi.pwmSetRange(1024)
wiringpi.pwmSetClock(375)

#Controls the servo motor only when the specified angle is within the operating range
if ( set_degree = -90 ):
	#Calculate the PWM pulse width sent from the angle
	move_deg = int( 81 + 41 / 90 * set_degree )
	#Send PWM to the servo motor to move the servo motor
	wiringpi.pwmWrite( servo_pin, move_deg )

3. Run servo_motor.py

servo_motor.Execute py (angle the argument)


$ sudo python3 servo_motor.py {The angle you want to set-From 90 to 90}

bash:py3:Example: 90 degrees


$ sudo python3 servo_motor.py 90

It bends to the angle entered in the argument. Supplement: In Razpai Magazine, it was described inside the source. Commented out.

Hard side Click the video commentary →

Raspberry Pi side Click the video explanation →

4. Useful when programming

It doesn't matter to those who operate the Raspberry Pi directly. For those who want to operate Raspberry Pi on Mac. We write the source on Mac, save it as it is on Raspberry Pi, and execute it from Mac. (There is no display ...) For that, we use AFP. The setting method is Install Raspberry Pi 3 → Wireless LAN → Japanese input / output → Operate from Mac of "6. AFP settings" Please refer to! It's convenient because you don't have to upload files by SFTP etc.! Windows users should be able to do something similar with Samba installed!

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

Run servomotor on Raspberry Pi 3 using python
Detect "brightness" using python on Raspberry Pi 3!
Detect temperature using python on Raspberry Pi 3!
Detect slide switches using python on Raspberry Pi 3!
Detect magnet switches using python on Raspberry Pi 3!
Sound the buzzer using python on Raspberry Pi 3!
Output to "7-segment LED" using python on Raspberry Pi 3!
Try using ArUco on Raspberry Pi
Run AWS IoT Device SDK for Python on Raspberry Pi
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
Access google spreadsheet using python on raspberry pi (for myself)
Adafruit Python BluefruitLE works on Raspberry Pi.
Working with GPS on Raspberry Pi 3 Python
Control the motor with a motor driver using python on Raspberry Pi 3!
pigpio on Raspberry pi
Cython on Raspberry Pi
Try using a QR code on a Raspberry Pi
Make DHT11 available on Raspberry Pi + python (memo)
Connect to MySQL with Python on Raspberry Pi
Build a Python development environment on Raspberry Pi
Detect analog signals with A / D converter using python on Raspberry Pi 3!
Run Openpose on Python (Windows)
Indoor monitoring using Raspberry Pi
Build an OpenCV4 environment on Raspberry Pi using Poetry
Run LEDmatrix interactively with Raspberry Pi 3B + on Slackbot
[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.
Run Tensorflow 2.x on Python 3.7
Raspberry Pi + Python + OpenGL memo
Run Python CGI on CORESERVER
Run unix command on python
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
raspberry pi 1 model b, python
Install pyenv on Raspberry Pi and version control Python
Broadcast on LINE using python
Introduced pyenv on Raspberry Pi
Using the 1-Wire Digital Temperature Sensor DS18B20 from Python on a Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Install PyCall on Raspberry PI and try using GPIO's library for Python from Ruby
VScode intellisense doesn't work on Raspberry Pi OS 64bit! (Python)
Cross-compiling for Raspberry Pi Zero on Debian-Try using shared libraries
Run BNO055 python sample code with I2C (Raspberry Pi 3B)
Testing uart communication on Raspberry Pi
Use vl53l0x with Raspberry Pi (python)
USB over ethernet using Raspberry pi
MQTT on Raspberry Pi and Mac
Introducing Python using pyenv on Ubuntu 20.04
Notes on using MeCab from Python
Install ghoto2 on Raspberry Pi (memo)
Run Python on Schedule on AWS Lambda
Run TensorFlow Docker Image on Python3
Study on Tokyo Rent Using Python (3-2)
Notes on installing Python using PyEnv
OpenCV installation procedure on Raspberry Pi
Notes on using rstrip with python.
Install Python on CentOS using Pyenv
Study on Tokyo Rent Using Python (3-3)
Power on / off Raspberry pi on Arduino