Use python on Raspberry Pi 3 and turn on the LED when it gets dark!

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, let the LED light up when it gets dark! It seems that it can be applied to security equipment and light management of crops! [Razpai Magazine-August 2016 Special Feature 1 Basic Pattern of Electronic Work 12 ② Turn on the LED when it gets dark](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 = c42c3f78bd8af3af14 However, I have arranged it a lot. (In Raspeye Magazine, a semi-fixed resistor is used for the darkness threshold, but here the brightness is acquired using an A / D converter and the threshold is set by the argument of the program.)

procedure

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

1. Wiring

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) --AD Converter 8 Vdd --Illuminance sensor left side --Raspberry Pi 19th pin (MOSI) --AD converter 5 MOSI --Raspberry Pi 21st pin (MISO) --AD converter 6 MISO --Raspberry Pi 23rd pin (SCLK) --AD converter 7 CLK --Raspberry Pi 24th pin (CE0) --AD converter 1 CS --Raspberry Pi 6th pin (GND) --AD converter 4 Vss --Raspberry Pi 16th pin (GPIO23) --LED positive side --LED negative side --Resistance (100Ω)

From AD converter (difference only)

--AD converter pin 2 (CH0) --Illuminance sensor right side --Resistance (10kΩ) --GND

2. Program illuminance_led.py

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

Clone with git


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

illuminance_led.py


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

#GPIO definition LED
led1_pin = 23 #Terminal 16

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

# MCP3002(A/Specify the channel to which the D converter) is connected
SPI_CH = 0

#MCP3002 to be read (A/Specify the analog input channel of the D converter)
READ_CH = 0

#Specify the brightness threshold as an argument
param = sys.argv
set_illuminance = int(param[1])

#SPI initialization
wiringpi.wiringPiSPISetup( SPI_CH, 1000000 )

while True:
    #Turn off the LED
    wiringpi.digitalWrite( led1_pin, 0 )

    # MCP3002(A/Create data to send to D converter)
    buffer = 0x6800 | ( 0x1800 * READ_CH )
    buffer = buffer.to_bytes( 2, byteorder='big' )
    
    #Get the value of CH0 using SPI
    wiringpi.wiringPiSPIDataRW( SPI_CH, buffer )
    
    #The values are sent in 2 bytes, so combine them into one value.
    illuminance_value = ( buffer[0] * 256 + buffer[1] ) & 0x3ff
    
    #Output brightness threshold and illuminance
    print ("Brightness threshold: " + str(set_illuminance) )
    print ("Illuminance: " + str(illuminance_value) )
    
    #"Bright" is displayed when the brightness exceeds the standard value, and "Dark" is displayed when the brightness falls below the standard value.
    if ( illuminance_value > set_illuminance ):
        print ("Because it is bright, turn off the LED")
        wiringpi.digitalWrite( led1_pin, 0 )
    else:
        print ("LED is lit because it is dark")
        wiringpi.digitalWrite( led1_pin, 1 )
    
    #Detected by 1 second
    time.sleep(1)

3. Run the program

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

Set the brightness threshold to 500


$ sudo python3 illuminance_led.py 500

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! Use python on Raspberry Pi 3 and turn on the LED when it gets dark!

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 and turn on the LED when it gets dark!
Use python on Raspberry Pi 3 to illuminate the LED (Hello World)
Use the Grove sensor on the Raspberry Pi
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
Sound the buzzer using python on Raspberry Pi 3!
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
How to use the Raspberry Pi relay module Python
Install pyenv on Raspberry Pi and version control Python
Output to "7-segment LED" using python on Raspberry Pi 3!
Use NeoPixel on Raspberry Pi
Get the matched string with a regular expression and reuse it when replacing on Python3
Use vl53l0x with Raspberry Pi (python)
MQTT on Raspberry Pi and Mac
Sakura Use Python on the Internet
Control the motor with a motor driver using python on Raspberry Pi 3!
Resolved an error when putting pygame in python3 on raspberry pi
I want to run the Python GUI when starting Raspberry Pi
Home hack to buy automatically when it gets cheaper with Amazon Dash Button and Raspberry Pi
Finally ... Make a radio control using python on Raspberry Pi 3! (When the magnet is brought closer, the motor moves and stops automatically)
[Python] Use and and or when creating variables
Deploy Python face recognition model on Heroku and use it from Flutter ②
Detect "brightness" using python on Raspberry Pi 3!
Adafruit Python BluefruitLE works on Raspberry Pi.
Deploy Python face recognition model on Heroku and use it from Flutter ①
Deploy and use the prediction model created in Python on SQL Server
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
Detect temperature using python on Raspberry Pi 3!
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
Working with GPS on Raspberry Pi 3 Python
[Python] Precautions when retrieving data by scraping and putting it in the list
Moved Raspberry Pi remotely so that it can be LED attached with Python
Using the 1-Wire Digital Temperature Sensor DS18B20 from Python on a Raspberry Pi
Try to use up the Raspberry Pi 2's 4-core CPU with Parallel Python
Use Raspberry Pi Python to TMP36 analog temperature sensor and MCP3008 AD converter
Detect slide switches using python on Raspberry Pi 3!
How to use Raspberry Pi pie camera Python
Launch and use IPython notebook on the network
Detect magnet switches using python on Raspberry Pi 3!
Use AppSync on the front and back ends
Use Grove-Temperature & Humidity Sensor (DHT11) on 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
Draw a weakness graph in Python and save it in various formats (Raspberry Pi, macOS)
[Python3] Take a screenshot of a web page on the server and crop it further
What to do when python3 type venv does not work well on Raspberry Pi
Install PyCall on Raspberry PI and try using GPIO's library for Python from Ruby
Use kintone API SDK for Python on Raspberry Pi (easily store data in kintone from Raspberry Pi)
[Raspberry Pi] Store the timestamp in the Firebase Realtime Database when the motion sensor detects it.
Record temperature and humidity with systemd on Raspberry Pi
It's getting cold, so I tried to make it possible to turn on / off the AC heater automatically with Raspberry Pi!
Install selenium on Mac and try it with python
Try debugging Python on Raspberry Pi with Visual Studio.
Try using the temperature sensor (LM75B) on the Raspberry Pi.
About the --enable-shared option when building Python on Linux
Installation of Docker on Raspberry Pi and L Chika
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Install django on python + anaconda and start the server
Use raspberry Pi and Julius (speech recognition). ③ Dictionary creation
Use Python to monitor Windows and Mac and collect information on the apps you are working on
Input and output display images directly using the frame buffer (/ dev / fb0) on Raspberry Pi