[PYTHON] Detect switch status 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 detect the status of the switch!

1. Wiring photo

Raspberry Pi 3でスイッチの状態を検出する The black object on the left side of the image is the button. It will be "ON" while teaching!

2. Source button.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

button.py


#Library to control GPIO
import wiringpi
#Timer library
import time

#GPIO terminal number to which the button is connected
button_pin = 17 #Terminal 11

#GPIO initialization
wiringpi.wiringPiSetupGpio()
#Set GPIO to output mode (1)
wiringpi.pinMode( button_pin, 0 )
#Set the state when nothing is connected to the terminal
# 3."2" (pull-up) for 3V
#Set to "1" for 0V (pull-down)
wiringpi.pullUpDnControl( button_pin, 2 )

#The while process seems to be in the loop range when indented.
while True:
    #Read the state of GPIO terminal
	#Press the button to get "0" and release to get "1"
	#GPIO status is 0V(0)Compare
	if( wiringpi.digitalRead(button_pin) == 0 ):
		# 0V(0)Display in case of
		print ("Switch ON")
	else:
		# 3.3V(1)Display in case of
		print ("Switch OFF")
	
	time.sleep(1)

3. Run button.py

$ sudo python3 button.py Then, "Switch ON" is displayed while the switch is pressed, and "Switch OFF" is displayed when the switch is not pressed!

Hard side Click the video commentary →

Raspberry Pi side Click the video commentary →

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

Detect switch status on Raspberry Pi 3
Detect "brightness" using python on Raspberry Pi 3!
pigpio on Raspberry pi
Cython on Raspberry Pi
Detect slide switches using python on Raspberry Pi 3!
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Detect mask wearing status with OpenCV and Raspberry Pi
Testing uart communication on Raspberry Pi
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Install ghoto2 on Raspberry Pi (memo)
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
Try using ArUco on Raspberry Pi
OpenCV installation procedure on Raspberry Pi
Power on / off Raspberry pi on Arduino
Install OpenMedia Vault 5 on Raspberry Pi 4
L Chika on Raspberry Pi C #
Build wxPython on Ubuntu 20.04 on raspberry pi 4
"Honwaka Notification Lamp" on Raspberry Pi Part 2
USB boot on Raspberry Pi 4 Model B
"Honwaka Notification Lamp" on Raspberry Pi Part 1
Enable UART + serial communication on Raspberry Pi
Adafruit Python BluefruitLE works on Raspberry Pi.
Accelerate Deep Learning on Raspberry Pi 4 CPU
Set swap space on Ubuntu on Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
Use the Grove sensor on the Raspberry Pi
Install 64-bit OS (bate) on Raspberry Pi
Install docker-compose on 64-bit Raspberry Pi OS
Run servomotor on Raspberry Pi 3 using python
"Honwaka Notification Lamp" on Raspberry Pi Part 3
Working with sensors on Mathematica on Raspberry Pi
Mount Windows shared folder on Raspberry Pi
Matrix multiplication on Raspberry Pi GPU (Part 2)
How to install NumPy on Raspberry Pi
I installed OpenCV-Python on my Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
Use python on Raspberry Pi 3 to light the LED with switch control!
Raspberry Pi backup
Build a Django environment on Raspberry Pi (MySQL)
Try using a QR code on a Raspberry Pi
Enjoy electronic work with GPIO on Raspberry Pi
Power on / off your PC with raspberry pi
Use Grove-Temperature & Humidity Sensor (DHT11) on Raspberry Pi
Make DHT11 available on Raspberry Pi + python (memo)
Beginning cross-compilation for Raspberry Pi Zero on Ubuntu
Sound the buzzer using python on Raspberry Pi 3!
Play with your Ubuntu desktop on your Raspberry Pi 4
Display CPU temperature every 5 seconds on Raspberry Pi 4
Introduced Ceph on Kubernetes on Raspberry Pi 4B (ARM64)
Connect to MySQL with Python on Raspberry Pi
Build a Python development environment on Raspberry Pi
Build an Arch Linux environment on Raspberry Pi
Record temperature and humidity with systemd on Raspberry Pi
Build an OpenCV4 environment on Raspberry Pi using Poetry
GPGPU with Raspberry Pi
Try debugging Python on Raspberry Pi with Visual Studio.
Try using the temperature sensor (LM75B) on the Raspberry Pi.