Enjoy electronic work with GPIO on Raspberry Pi

Raspberry Pi 3 includes a library to operate GPIO from python as standard

  1. Preparation: ʻimport RPi.GPIO as GPIO`
  2. Specify the mode: GPIO.setmode (GPIO.BOARD) --GPIO.BOARD: Physical pin number (serial number from top left) --GPIO.BCM: Role pin number (named by broadcom)
  3. Pin settings: GPIO.setup (channel, GPIO.out) --GPIO.in: Input --GPIO.out: Output
  4. Pin operation:
    • GPIO.output(channel, GPIO.HIGH)
    • GPIO.output(channel, GPIO.LOW)
    • val = GPIO.input(channel)
スクリーンショット 2017-07-15 17.41.36.png

Example 1: LED flickering

led-blink.py


import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)

while True:
    GPIO.output(11, True)
    time.sleep(2)
    GPIO.output(11, False)
    time.sleep(2)

Example 2: Detect button input

led-blink.py


import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    input_state = GPIO.input(18)
    if input_state == False:
        print('Button Pressed')
        time.sleep(0.2)
スクリーンショット 2017-07-15 17.41.36.png

Recommended Posts

Enjoy electronic work with GPIO on Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
Working with sensors on Mathematica on Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
GPGPU with Raspberry Pi
[Electronic work] I made a Suica touch sound detector with Raspberry Pi
Power on / off your PC with raspberry pi
Play with your Ubuntu desktop on your Raspberry Pi 4
DigitalSignage with Raspberry Pi
Cython on Raspberry Pi
Connect to MySQL with Python on Raspberry Pi
Record temperature and humidity with systemd on Raspberry Pi
Run LEDmatrix interactively with Raspberry Pi 3B + on Slackbot
Try debugging Python on Raspberry Pi with Visual Studio.
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Let's operate GPIO of Raspberry Pi with Python CGI
Mutter plants with Raspberry Pi
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
VScode intellisense doesn't work on Raspberry Pi OS 64bit! (Python)
[Raspberry Pi] Stepping motor control with Raspberry Pi
Use vl53l0x with Raspberry Pi (python)
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Serial communication with Raspberry Pi + PySerial
OS setup with Raspberry Pi Imager
Try using ArUco on Raspberry Pi
Try L Chika with raspberry pi
OpenCV installation procedure on Raspberry Pi
VPN server construction with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Power on / off Raspberry pi on Arduino
Detect switch status on Raspberry Pi 3
Install OpenMedia Vault 5 on Raspberry Pi 4
Using a webcam with Raspberry Pi
L Chika on Raspberry Pi C #
Build wxPython on Ubuntu 20.04 on raspberry pi 4
Control the motor with a motor driver using python on Raspberry Pi 3!
Measure SIM signal strength with Raspberry Pi
Pet monitoring with Rekognition and Raspberry pi
"Honwaka Notification Lamp" on Raspberry Pi Part 2
Detect "brightness" using python on Raspberry Pi 3!
Detect analog signals with A / D converter using python on Raspberry Pi 3!
USB boot on Raspberry Pi 4 Model B
Hello World with Raspberry Pi + Minecraft Pi Edition
"Honwaka Notification Lamp" on Raspberry Pi Part 1
Build a Tensorflow environment with Raspberry Pi [2020]
Enable UART + serial communication on Raspberry Pi
Adafruit Python BluefruitLE works on Raspberry Pi.
Use python on Raspberry Pi 3 to light the LED with switch control!
Try tweeting arXiv's RSS feed on twitter from Raspberry Pi with python
Get BITCOIN LTP information with Raspberry PI
Accelerate Deep Learning on Raspberry Pi 4 CPU
Try fishing for smelt with Raspberry Pi
Use the Grove sensor on the Raspberry Pi
Install 64-bit OS (bate) on Raspberry Pi
Improved motion sensor made with Raspberry Pi
Install docker-compose on 64-bit Raspberry Pi OS