BakeBit - Light Sensor

From FriendlyELEC WiKi
Revision as of 10:59, 13 December 2016 by Yftan (Talk | contribs) (介绍)

Jump to: navigation, search

查看中文

1 Introduction

Light Sensor
  • The BakeBit - Light Sensor is a photoresistor module. The resistance of a photoresistor decreases with increasing incident light intensity.
  • If incident light on a photoresistor exceeds a certain frequency, photons absorbed by the semiconductor give bound electrons enough energy to jump into the conduction band. The resulting free electrons (and their hole partners) conduct electricity, thereby lowering resistance.

2 特性

  • 使用标准的2.0mm 4 Pin BakeBit接口
  • PCB尺寸(mm):20x24
  • 模拟信号输出

BakeBit - Light Sensor

  • 引脚说明:
名称 描述
GND
5V 电源5V
NC
SIG 信号

3 示例程序:Smart Lighting

利用亮度传感器,聪明地控制灯光(灯用LED Bar来模拟),环境越暗就让灯越亮,环境亮度足够时,就将灯变暗或者熄灭。 本示例需要配合BakeBit - LED Bar使用。

3.1 硬件连接

简单的将BakeBit - LED Bar模块插入 D3 接口,将BakeBit - Light Sensor插入 A0 接口,如下面这样:

Smart Lighting-1.jpg Smart Lighting-2.jpg

3.2 示例源代码

import time
import bakebit
 
# Connect the BakeBit Light Sensor to analog port A0
# SIG,NC,VCC,GND
light_sensor = 0
 
# Connect the BakeBit LED Bar to digital port D3
# DI,DCKI,VCC,GND
ledbar = 3
 
 
lowThreshold = 500
highTreshold = 600
 
bakebit.pinMode(light_sensor,"INPUT")
bakebit.pinMode(ledbar,"OUTPUT")
time.sleep(.2)
bakebit.bakeBitLedBar_Init(ledbar, 0, 5)
time.sleep(.5)
old_color = 0
 
while True:
    try:
        light_count = 0
 
        # Get sensor value
        sensor_value = bakebit.analogRead(light_sensor)
        if sensor_value > highTreshold:
            f=(1023-highTreshold)/4
            light_count =  (sensor_value-highTreshold)/f+1
 
            # turn on ledbar
            color16bit = bakebit.Green
            if light_count > 1:
                color16bit = color16bit | (bakebit.Green << 3)
            if light_count > 2:
                color16bit = color16bit | (bakebit.Green << 6)
            if light_count > 3:
                color16bit = color16bit | (bakebit.Green << 9)
            if light_count > 4:
                color16bit = 0
                color16bit = color16bit | bakebit.Blue
                color16bit = color16bit | (bakebit.Blue << 3)
                color16bit = color16bit | (bakebit.Blue << 6)
                color16bit = color16bit | (bakebit.Blue << 9)
                color16bit = color16bit | (bakebit.Blue << 12)
 
            if color16bit != old_color:
                old_color = color16bit
                lowBits = color16bit & 255
                highBits = (color16bit & (255 << 8)) >> 8
                bakebit.bakeBitLedBar_Show(ledbar, highBits, lowBits)
 
        elif sensor_value < lowThreshold:
            # turn off ledbar
            bakebit.bakeBitLedBar_Show(ledbar, 0, 0)
 
        print("sensor_value = %d light_count =%d" %(sensor_value,  light_count))
        time.sleep(.5)
 
    except KeyboardInterrupt:
        bakebit.bakeBitLedBar_Release(ledbar)
        time.sleep(.2)
        break
    except IOError:
        print ("Error")

Github


3.3 运行示例

假设你已经参考bakebit教程安装了BakeBit源代码,
要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_prj_SmartLighting.py:

cd ~/BakeBit/Software/Python
sudo python bakebit_prj_SmartLighting.py

3.4 运行结果

测试时可用物品盖住光敏电阻模拟环境变暗,或者用手机电筒照射模拟环境变亮,然后观察LED Bar的变化。
LED Bar上被点亮的LED个数由当前环境的光线决定,越暗,被点亮的就最多。

4 相关资料