Difference between revisions of "BakeBit - Light Sensor/zh"

From FriendlyELEC WiKi
Jump to: navigation, search
(介绍)
(硬件连接)
 
(2 intermediate revisions by the same user not shown)
Line 24: Line 24:
 
|SIG  || 信号
 
|SIG  || 信号
 
|}
 
|}
==硬件连接==
+
== 示例程序:Smart Lighting ==
使用标准4 Pin连接线与NEO Hub连接。
+
 
 +
利用亮度传感器,聪明地控制灯光(灯用LED Bar来模拟),环境越暗就让灯越亮,环境亮度足够时,就将灯变暗或者熄灭。
 +
本示例需要配合[[BakeBit - LED Bar]]使用。
 +
 
 +
=== 硬件连接 ===
 +
简单的将[[BakeBit - LED Bar]]模块插入 D3 接口,将[[BakeBit - Light Sensor]]插入 A0 接口,如下面这样:
 +
 
 +
::{| class="wikitable"
 +
|-
 +
|[[File:Smart Lighting-1.jpg|frameless|300px]]    || [[File:Smart Lighting-2.jpg |frameless|300px]]
 +
|-
 +
|}
 +
 
 +
=== 示例源代码 ===
 +
 
 +
<syntaxhighlight lang="python">
 +
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")
 +
 
 +
</syntaxhighlight>
 +
[https://github.com/friendlyarm/BakeBit/blob/master/Software/Python/bakebit_prj_SmartLighting.py Github]
 +
 
 +
 
 +
=== 运行示例 ===
 +
 +
假设你已经参考[http://wiki.friendlyarm.com/bakebit bakebit教程]安装了BakeBit源代码,<br />
 +
要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_prj_SmartLighting.py:
 +
<syntaxhighlight lang="bash">
 +
cd ~/BakeBit/Software/Python
 +
sudo python bakebit_prj_SmartLighting.py
 +
</syntaxhighlight>
 +
 
 +
=== 运行结果 ===
 +
 
 +
测试时可用物品盖住光敏电阻模拟环境变暗,或者用手机电筒照射模拟环境变亮,然后观察LED Bar的变化。<br />
 +
LED Bar上被点亮的LED个数由当前环境的光线决定,越暗,被点亮的就最多。
  
参考下图连接模块:
 
[[File:BakeBit - Light Sensor_nanopi_NEO.jpg|frameless|600px|BakeBit - Light Sensor_nanopi_NEO]]
 
 
==相关资料==
 
==相关资料==
 
*[Schematic]([http://wiki.friendlyarm.com/wiki/images/e/e8/06-SCHEMATIC_Light_Sensor.pdf BakeBit - Light Sensor.pdf])
 
*[Schematic]([http://wiki.friendlyarm.com/wiki/images/e/e8/06-SCHEMATIC_Light_Sensor.pdf BakeBit - Light Sensor.pdf])
 +
*[BakeBit Github项目](https://github.com/friendlyarm/BakeBit)
 +
*[BakeBit Starter Kit手册](http://wiki.friendlyarm.com/bakebit/bakebit_starter_kit_manual_cn.pdf)

Latest revision as of 09:03, 28 November 2016

English

1 介绍

Light Sensor
  • BakeBit - Light Sensor是一个光敏电阻模块,主要器件是一个光敏电阻。光敏电阻的制作材料具有在特定波长的光照射下,其阻值迅速减小的特性。
  • 这是由于光照产生的载流子都参与导电,在外加电场的作用下作漂移运动,电子奔向电源的正极,空穴奔向电源的负极,从而使光敏电阻器的阻值迅速下降。在电源激励下,输出端分压减小。

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 相关资料