Raspberry Pi: 结合BH1750FVI光强计自动控制5110背光

BH1750FVI
感觉直接上python程序吧。。

#!/usr/bin/python
# -*- encoding: utf-8 -*-
import time
import smbus
import RPi.GPIO
bus = smbus.SMBus(1)
# BH1750FVI的i2c地址就是0x23
addr = 0x23
port = 25
RPi.GPIO.setmode(RPi.GPIO.BCM)
RPi.GPIO.setup(port, RPi.GPIO.OUT)
pwm = RPi.GPIO.PWM(port, 50)
pwm.start(0)
try:
    while True:
        data = bus.read_i2c_block_data(addr,0x11)
        # 计算光照度值lux
        lux = (data[1] + (256 * data[0])) / 1.2
        # print lux
        if lux > 100:
            pwm.ChangeDutyCycle(0)
        else:
            pwm.ChangeDutyCycle(100 - lux)
        time.sleep(0.2)
except KeyboardInterrupt:
    pass
pwm.stop()
RPi.GPIO.cleanup()

参考链接:BH1750FVI

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »