Detect low or bright light using the Light sensor.
レッスン6"光センサーを使用して、弱い光または明るい光を検出する"をやっていく。
What will you learn
このレッスンを終了すると、次のことができるようになります。
- 光センサーを使用して、弱い光と明るい光を検出する方法を学習します
What will you need
- 初期設定後のCrowPiボード
Requires switching modules using the switch
- いいえ
CrowPi上での光センサーの位置
↑ここの上を手で覆うと表示される値が小さくなる
光センサーの操作
このレッスンで使用するPythonスクリプトは以下の通り。
Examples/light_sensor.py
#!/usr/bin/python # -*- coding: utf-8 -*- # Author: Matt Hawkins # Author's Git: https://bitbucket.org/MattHawkinsUK/ # Author's website: https://www.raspberrypi-spy.co.uk # http://elecrow.com/ import RPi.GPIO as GPIO import smbus import time # Find the right revision for bus driver if(GPIO.RPI_REVISION == 1): bus = smbus.SMBus(0) else: bus = smbus.SMBus(1) class LightSensor(): def __init__(self): # Define some constants from the datasheet self.DEVICE = 0x5c # Default device I2C address self.POWER_DOWN = 0x00 # No active state self.POWER_ON = 0x01 # Power on self.RESET = 0x07 # Reset data register value # Start measurement at 4lx resolution. Time typically 16ms. self.CONTINUOUS_LOW_RES_MODE = 0x13 # Start measurement at 1lx resolution. Time typically 120ms self.CONTINUOUS_HIGH_RES_MODE_1 = 0x10 # Start measurement at 0.5lx resolution. Time typically 120ms self.CONTINUOUS_HIGH_RES_MODE_2 = 0x11 # Start measurement at 1lx resolution. Time typically 120ms # Device is automatically set to Power Down after measurement. self.ONE_TIME_HIGH_RES_MODE_1 = 0x20 # Start measurement at 0.5lx resolution. Time typically 120ms # Device is automatically set to Power Down after measurement. self.ONE_TIME_HIGH_RES_MODE_2 = 0x21 # Start measurement at 1lx resolution. Time typically 120ms # Device is automatically set to Power Down after measurement. self.ONE_TIME_LOW_RES_MODE = 0x23 def convertToNumber(self, data): # Simple function to convert 2 bytes of data # into a decimal number return ((data[1] + (256 * data[0])) / 1.2) def readLight(self): data = bus.read_i2c_block_data(self.DEVICE,self.ONE_TIME_HIGH_RES_MODE_1) return self.convertToNumber(data) def main(): sensor = LightSensor() try: while True: print("Light Level : " + str(sensor.readLight()) + " lx") time.sleep(0.5) except KeyboardInterrupt: pass if __name__ == "__main__": main() |
無限ループとなるので、終了するには、CTRL+C。
※コメント投稿者のブログIDはブログ作成者のみに通知されます