Difference between revisions of "BakeBit - Light Sensor"

From FriendlyELEC WiKi
Jump to: navigation, search
(特性)
(示例程序:Smart Lighting)
Line 26: Line 26:
 
|}
 
|}
  
== 示例程序:Smart Lighting ==
+
== Code Sample: Smart Lighting ==
  
利用亮度传感器,聪明地控制灯光(灯用LED Bar来模拟),环境越暗就让灯越亮,环境亮度足够时,就将灯变暗或者熄灭。
+
By running this code sample your system adjusts the LEDs on the LED module(LED Bar) according to surrounding lightness. When the surrounding environment becomes darker the LEDs will be brighter. When the surrounding becomes bright enough the LEDs will be completely off.
本示例需要配合[[BakeBit - LED Bar]]使用。
+
A [[BakeBit - LED Bar]] module is needed in this test case.
  
=== 硬件连接 ===
+
=== Hardware Connection ===
简单的将[[BakeBit - LED Bar]]模块插入 D3 接口,将[[BakeBit - Light Sensor]]插入 A0 接口,如下面这样:
+
Connect the [[BakeBit - LED Bar]] module to the NEO-Hub at D3 and the [[BakeBit - Light Sensor]] module to the NEO-Hub at A0:
  
 
::{| class="wikitable"
 
::{| class="wikitable"
Line 40: Line 40:
 
|}
 
|}
  
=== 示例源代码 ===
+
=== Source Code ===
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
Line 115: Line 115:
  
  
=== 运行示例 ===
+
=== Run Code Sample ===
 
   
 
   
假设你已经参考[http://wiki.friendlyarm.com/bakebit bakebit教程]安装了BakeBit源代码,<br />
+
Before you run the code sample you need to follow the steps in [http://wiki.friendlyarm.com/bakebit bakebit tutorial] to intall the BakeBit package.<br />
要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_prj_SmartLighting.py:
+
Enter the "BakeBit/Software/Python" directory and run the "bakebit_prj_SmartLighting.py" program:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
cd ~/BakeBit/Software/Python
 
cd ~/BakeBit/Software/Python
Line 124: Line 124:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== 运行结果 ===
+
=== Observation ===
  
测试时可用物品盖住光敏电阻模拟环境变暗,或者用手机电筒照射模拟环境变亮,然后观察LED Bar的变化。<br />
+
When you cover the smart lighting’s photo sensor or make the surrounding environment brighter the LEDs will be on or off.<br />
LED Bar上被点亮的LED个数由当前环境的光线决定,越暗,被点亮的就最多。
+
The LEDs on the LED Bar module will be turned on or off according to surrounding environment’s lightness. When the the surrounding environment becomes darker more LEDs will be turned on otherwise the LEDs will be off.
  
 
==相关资料==
 
==相关资料==

Revision as of 11:12, 13 December 2016

查看中文

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 Hardware Spec

  • Standard 2.0mm pitch 4-Pin BakeBit Interface
  • PCB dimension(mm): 20 x 24
  • Analog Outpu

BakeBit - Light Sensor

  • Pin Description:
Pin Description
GND Ground
5V 5V Supply Voltage
NC Ground
SIG Signal

3 Code Sample: Smart Lighting

By running this code sample your system adjusts the LEDs on the LED module(LED Bar) according to surrounding lightness. When the surrounding environment becomes darker the LEDs will be brighter. When the surrounding becomes bright enough the LEDs will be completely off. A BakeBit - LED Bar module is needed in this test case.

3.1 Hardware Connection

Connect the BakeBit - LED Bar module to the NEO-Hub at D3 and the BakeBit - Light Sensor module to the NEO-Hub at A0:

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

3.2 Source Code

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 Run Code Sample

Before you run the code sample you need to follow the steps in bakebit tutorial to intall the BakeBit package.
Enter the "BakeBit/Software/Python" directory and run the "bakebit_prj_SmartLighting.py" program:

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

3.4 Observation

When you cover the smart lighting’s photo sensor or make the surrounding environment brighter the LEDs will be on or off.
The LEDs on the LED Bar module will be turned on or off according to surrounding environment’s lightness. When the the surrounding environment becomes darker more LEDs will be turned on otherwise the LEDs will be off.

4 相关资料