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!
The black object on the left side of the image is the button. It will be "ON" while teaching!
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)
$ 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!
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!
Install Raspberry Pi 3 → Wireless LAN → Japanese input / output → Operate from Mac
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!
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!
Coding rules "Let's write gentle code" (FuelPHP) Naming convention "Friendly to yourself, team-friendly, and unseen members after 3 months"
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