我目前正在做智能光,通过使用Grove光传感器来检索感应值(光强度)来控制Grove LED插座套件的亮度,我没有使用GPIO。我遵循了seeed wiki http://wiki.seeedstudio.com/Grove-Light_Sensor/的代码,并修改了一些代码,但当周围的光线强度很高时,我仍然无法将发光二极管的亮度更改为暗淡,我只是继续获得明亮的发光二极管。我正在尝试让LED的亮度在周围光线强度较低的情况下变得更亮。这是我的代码。
import time
from grovepi import *
import datetime
from guizero import App, Text, PushButton, Box
# Connect the Grove Light Sensor to analog port A0
light_sensor = 0
# Connect the LED to digital port D3
led = 3
# Connect the Grove PIR Motion Sensor to digital port D8
pir_sensor = 8
# Turn on LED once sensor exceeds threshold resistance (light sensor gets covered will turn on the LED)
threshold = 20
pinMode(pir_sensor,"INPUT")
pinMode(light_sensor,"INPUT")
pinMode(led,"OUTPUT")
# Title and format
app = App(title="Smart Light")
Titletext = Text(app, text="Smart Light Controller", size=40,
font="Times New Roman", color="lightblue")
# define actions
def action1():
Statustext.value = "Light Turn On"
digitalWrite(led,1)
print ("Light ON!")
def action2():
Statustext.value = "Light Turn Off"
digitalWrite(led,0)
print ("Light OFF!")
def action3():
while True:
Statustext.value = "Light Auto"
# Get sensor value = light intensity
sensor_value = analogRead(light_sensor)
g = str(sensor_value)
a = analogRead(light_sensor)
max_pwn = 100
max_analog_read = 1023
pwn_desired = (float)(a * max_pwn) / max_analog_read
# Calculate resistance of sensor in K
resistance = (float)(1023 - sensor_value) * 10 / sensor_value
# Sense motion, usually human, within the target range
if digitalRead(pir_sensor) and resistance > threshold:
print ('Motion Detected')
print ('Light On')
# Send HIGH to switch on LED
digitalWrite(led,1)
analogWrite(led, pwn_desired)
time.sleep(3)
else :
# Send LOW to switch off LED
# NO motion but intensity low = off
# Motion detected but intensity high = off
digitalWrite(led,0)
print ('No Motion')
print ('Light Off')
print("sensor_value = %d resistance = %.2f" %(sensor_value, resistance))
time.sleep(2)
def line():
Text(app, "----------------------------------")
# Buttons' format
buttons_box = Box(app, layout="grid")
Onbutton = PushButton(buttons_box, action1, text="On",
width=20, height=10,grid=[0,0] )
Offbutton = PushButton(buttons_box, action2, text="Off",
width=20, height=10,grid=[1,0])
Autobutton = PushButton(buttons_box, action3, text="Auto",
width=20, height=10,grid=[2,0])
# Status title
Status = Text(app, text="Light Status", size = 30)
line()
# The status text will change when the each light button is clicked
Statustext = Text(app, text="Idle", color="red", size = 20)
line()
#all code must before app.display
app.display()发布于 2019-08-21 05:47:25
脉宽调制...脉宽调制。
换句话说,在GPIO ( LED)上应用占空比。
更亮的光?使用较高的占空比。例如,当传感器值指示需要更多来自LED的光时?
暗淡的光线?使用较低的占空比。例如,当传感器的值表明需要较少的光从LED?
将模数转换analogRead输入到计算中,以求出占空比。
您的等式需要反映最小analogRead是最小占空比,而最大analogRead是最大占空比。
假设您的最大analogRead是1023。
伪代码:
a = analogRead
max_pwm = 100
max_analog_read = 1023
pwm_desired = (a * max_pwm) / max_analog_read如果使用情况是较低的模拟传感器读数需要较高的pwm,则计算补码:
// ...
pwm_desired = (a * max_pwm) / max_analog_read
inverted_pwm_desired = max_pwm - pwm_desired在任何一种情况下,我相信在您计算它之后,您可以将GPIO配置为输出PWM引脚。从你的问题来看,这似乎就是我们所需要的。
基于注释进行编辑:
它可以通过多种方式实现。让我们选择一个简单的。
另一种看待它的方式是:
想象一个方波。它是高的或低的。它的高持续时间是一个活跃的高占空比。一个完整的周期是从上升沿到下一个上升沿。一部分时间是“高”的,另一部分时间是“低”的。我们需要模仿这一点。
在您的循环中,您需要模拟一个“值”,该值在一段时间内与传感器值成正比。
当“值”高的时候,你打开LED,当它低的时候,你把它关掉。
过渡需要迅速(上升边缘的开始--周期的开始是快速的)。在几毫秒的数量级上--这取决于您的硬件以及在LED封装中扩散了多少光,等等。“视情况而定”。
不管怎样,
通过使LED脉冲接通计算的时间长度来模拟LED亮度。LED是一个数字组件,至少从这个角度来看是这样。一旦它的偏置超过了“二极管下降”,它就会饱和--开。如果LED偏置超过0.6V衰减,则它将亮起。这就是你的低电平LED驱动器代码实际上要做的--断言GIO (3.3V左右),并串联一些电阻(10k欧姆左右),这样通过GIO的电流就不会太大。如果电压偏置改变,LED不会改变亮度。这不是白炽灯,也不是模拟灯泡。
实验。
假设我们不知道你的循环迭代的频率是多少。我们可以选择接近的指标和转换。在尝试了一组指标和转换因子后,您可以更改它们以获得正确的PWM效果。
让我们开始吧:
捕获模拟传感器读数。将其标准化为一个百分比(0到100%),如前面所示。这个值就是我们的占空比。
然后,打开LED。然后等待,在循环中旋转等待一段与占空比成比例的时间。等待结束后(简单地模拟延迟计数器),然后关闭LED。然后等待占空比的补足。重复一遍。
在伪代码中:
while (true)
{
pwm = computePercentageBasedOnAnalogSignal(analog_signal)
turn_on_led();
on_counter = MAX_LOOP * pwm
off_counter = MAX_LOOP - on_counter
// scale on/off counter values appropriately if you have a sleep()
// replace with a sleep(on_counter) function if you have it
while(on_counter) {
on_counter--
}
// now turn it off.
turn_off_led()
// same comment re: sleep. remember to scale the value as necessary
while (off_counter) {
off_counter--
}
// The LED is either on or off for a MAX_LOOP total time.
// If the LED was on 90% of the MAX_LOOP and off 10% of MAX_LOOP
// AND the LOOP iterates rapidly enough it will appear to be brighter
// than if the pwm were swtiched. The loop needs to iterate rapidly
// for best effect!
}https://stackoverflow.com/questions/57581813
复制相似问题