Use python on Raspberry Pi 3 to light the LED with switch control!

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, the LED is lit by switch control! [Razpai Magazine-August 2016 Special Feature 1 Basic Pattern of Electronic Work 12 ① Infinite Loop and Conditional Branching](https://www.amazon.co.jp/%E3%83%A9%E3%82%BA % E3% 83% 91% E3% 82% A4% E3% 83% 9E% E3% 82% AC% E3% 82% B8% E3% 83% B3-2016% E5% B9% B48% E6% 9C% 88 % E5% 8F% B7-% E6% 97% A5% E7% B5% 8CBP% E3% 83% 91% E3% 82% BD% E3% 82% B3% E3% 83% B3% E3% 83% 99% E3% 82% B9% E3% 83% 88% E3% 83% A0% E3% 83% 83% E3% 82% AF-% E6% 97% A5% E7% B5% 8CLinux / dp / 4822237710 / ref = as_li_ss_tl ? ie = UTF8 & qid = 1471057119 & sr = 8-4 & keywords =% E3% 83% A9% E3% 82% BA% E3% 83% 91% E3% 82% A4 & linkCode = ll1 & tag = sr2smail-22 & linkId = c42c3f78bd8af3af1414020a400090fe)

procedure

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

1. Wiring

[Razpai Magazine-August 2016 Special Feature 1 Basic Pattern of Electronic Work 12 ① Infinite Loop and Conditional Branching](https://www.amazon.co.jp/%E3%83%A9%E3%82%BA % E3% 83% 91% E3% 82% A4% E3% 83% 9E% E3% 82% AC% E3% 82% B8% E3% 83% B3-2016% E5% B9% B48% E6% 9C% 88 % E5% 8F% B7-% E6% 97% A5% E7% B5% 8CBP% E3% 83% 91% E3% 82% BD% E3% 82% B3% E3% 83% B3% E3% 83% 99% E3% 82% B9% E3% 83% 88% E3% 83% A0% E3% 83% 83% E3% 82% AF-% E6% 97% A5% E7% B5% 8CLinux / dp / 4822237710 / ref = as_li_ss_tl ? ie = UTF8 & qid = 1471057119 & sr = 8-4 & keywords =% E3% 83% A9% E3% 82% BA% E3% 83% 91% E3% 82% A4 & linkCode = ll1 & tag = sr2smail-22 & linkId = c42c3f78bd8af3af1414020a400090fe)

If you get an image like this, referring to the picture of the circuit Raspberry Pi 3でpythonを使いスイッチ制御でLEDを光らせる!回路.jpg

I also made a video Click the video commentary →

Text version From the Raspberry Pi side

--Raspberry Pi Pin 1 (3.3V) --Left side of slide switch --Raspberry Pi 11th pin (GPIO17) --Slide switch center --Raspberry Pi 16th pin (GPIO23) --LED 1st plus side --Raspberry Pi 18th pin (GPIO24) --LED 2nd plus side --Raspberry Pi 6th pin (GND) --Slide switch right side --Raspberry Pi 6th pin (GND) --Resistance (100Ω) --LED 1st minus side --Raspberry Pi 6th pin (GND) --Resistance (100Ω) --LED 2nd minus side

2. Program switch_led.py

[Razpai Magazine-August 2016 Special Feature 1 Basic Pattern of Electronic Work 12 ① Infinite Loop and Conditional Branching](https://www.amazon.co.jp/%E3%83%A9%E3%82%BA % E3% 83% 91% E3% 82% A4% E3% 83% 9E% E3% 82% AC% E3% 82% B8% E3% 83% B3-2016% E5% B9% B48% E6% 9C% 88 % E5% 8F% B7-% E6% 97% A5% E7% B5% 8CBP% E3% 83% 91% E3% 82% BD% E3% 82% B3% E3% 83% B3% E3% 83% 99% E3% 82% B9% E3% 83% 88% E3% 83% A0% E3% 83% 83% E3% 82% AF-% E6% 97% A5% E7% B5% 8CLinux / dp / 4822237710 / ref = as_li_ss_tl ? ie = UTF8 & qid = 1471057119 & sr = 8-4 & keywords =% E3% 83% A9% E3% 82% BA% E3% 83% 91% E3% 82% A4 & linkCode = ll1 & tag = sr2smail-22 & linkId = c42c3f78bd8af3af1414020a400090fe)

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

Clone with git


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

switch_led.py


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

#GPIO definition
led1_pin = 23
led2_pin = 24
switch_pin = 17


#Get the interval to shine from the argument
param = sys.argv
set_interval = int(param[1])

#GPIO initialization
wiringpi.wiringPiSetupGpio()
wiringpi.pinMode( led1_pin, 1 )
wiringpi.pinMode( led2_pin, 1 )
wiringpi.pinMode( switch_pin, 0 )

#Which LED is on
led = 0

#This circuit is permanent, so repeat until you stop
while True:
    #Turn off the LED
    wiringpi.digitalWrite( led1_pin, 0 )
    wiringpi.digitalWrite( led2_pin, 0 )
    
    #Detect slide switch
    while ( wiringpi.digitalRead(switch_pin) == 1 ):
        #Switch on
        print("Switch on")
        
        if ( led == 0 ):
            #Make LED1 shine
            wiringpi.digitalWrite( led1_pin, 1 )
            wiringpi.digitalWrite( led2_pin, 0 )
            led = 1
            print("LED1")
        else:
            #Make LED2 shine
            wiringpi.digitalWrite( led1_pin, 0 )
            wiringpi.digitalWrite( led2_pin, 1 )
            led = 0
            print("LED2")
        #Wait for the number of seconds specified by the argument
        print(set_interval, "Wait for seconds")
        time.sleep(set_interval)

3. Run the program

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

LED switching every 3 seconds


$ sudo python3 switch_led.py 3

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! Use python on Raspberry Pi 3 to light the LED with switch control!

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

Use python on Raspberry Pi 3 to light the LED with switch control!
Use python on Raspberry Pi 3 to illuminate the LED (Hello World)
Control the motor with a motor driver using python on Raspberry Pi 3!
Connect to MySQL with Python on Raspberry Pi
How to use the Raspberry Pi relay module Python
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Output to "7-segment LED" using python on Raspberry Pi 3!
Try to use up the Raspberry Pi 2's 4-core CPU with Parallel Python
Use python on Raspberry Pi 3 and turn on the LED when it gets dark!
Use vl53l0x with Raspberry Pi (python)
Use the Grove sensor on the Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
How to use Raspberry Pi pie camera Python
Specify the Python executable to use with virtualenv
The easiest way to use OpenCV with python
Introduction to Python with Atom (on the way)
Sound the buzzer using python on Raspberry Pi 3!
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
Try to visualize the room with Raspberry Pi, part 1
Try debugging Python on Raspberry Pi with Visual Studio.
Control brushless motors with GPIOs on Raspberry Pi Zero
Specify MinGW as the compiler to use with Python
Install pyenv on Raspberry Pi and version control Python
Switch python to 2.7 with alternatives
Use NeoPixel on Raspberry Pi
Control music playback on a smartphone connected to Raspberry Pi 3 and bluetooth with AVRCP
Update Python for Raspberry Pi to 3.7 or later with pyenv
[Hyperledger Iroha] Notes on how to use the Python SDK
Install pyenv on MacBook Air and switch python to use
[Python] I want to use the -h option with argparse
Save images on the web to Drive with Python (Colab)
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
Python: How to use async with
Servo motor control with Raspberry Pi
It was great to edit the Python file in the Raspberry Pi with Atom's remote function
How to use FTP with Python
Detect switch status on Raspberry Pi 3
Sakura Use Python on the Internet
[Raspberry Pi] Changed Python default to Python3
A note on what you did to use Flycheck with Python
I learned how the infrared remote control works with Raspberry Pi
[September 2020 version] Explains the procedure to use Gmail API with Python
Linking Python and Arduino to display IME On / Off with LED
I want to run the Python GUI when starting Raspberry Pi
How to use python put in pyenv on macOS with PyCall
[Python] Explains how to use the format function with an example
How to play music (wav / mp3) files on Raspberry Pi python
Install a tact switch on the Raspberry Pi to make daily "Good morning Yosoro !!" tweets comfortable
Detect "brightness" using python on Raspberry Pi 3!
Adafruit Python BluefruitLE works on Raspberry Pi.
[Introduction to Python] Let's use foreach with Python
Programming normally with Node-RED programming on Raspberry Pi 3
Memorandum on how to use gremlin python
Download files on the web with Python
Run servomotor on Raspberry Pi 3 using python
Working with sensors on Mathematica on Raspberry Pi
Install python on xserver to use pip
Use PIR motion sensor with raspberry Pi
Detect temperature using python on Raspberry Pi 3!
How to install NumPy on Raspberry Pi