|
本帖最后由 sunplus 于 2016-5-9 09:46 编辑
自己使用数码管做了一个数字时钟,参考了坛友lcw_swust的代码:
代码中提到数码管可以作为类似光敏元件
发光二极管不仅可以作为发光元件,而且还可以作为光敏二极管检测光强度。
网上的例子并不多
感谢坛友lcw_swust
http://www.amobbs.com/forum.php?mod=viewthread&tid=5480294
以下是 网上的实现方法:
自己用了下面的例子,已经得到验证。
LEDs are commonly used as lights, but then can also be used as photodiodes to detect light. This example circuit uses a single LED to sample the ambient light level and then glow at an appropriate brightness. It is tragically flawed by a slow refresh rate in the dark, but it shows how to sense.
//
// This example shows one way of using an LED as a light sensor.
// You will need to wire up your components as such:
//
// + digital2
// |
// <
// > 100 ohm resistor
// <
// |
// |
// -----
// / \ LED, maybe a 5mm, clear plastic is good
// -----
// |
// |
// + digital3
//
// What we are going to do is apply a positive voltage at digital2 and
// a low voltage at digital3. This is backwards for the LED, current will
// not flow and light will not come out, but we will charge up the
// capacitance of the LED junction and the Arduino pin.
//
// Then we are going to disconnect the output drivers from digital2 and
// count how long it takes the stored charge to bleed off through the
// the LED. The brighter the light, the faster it will bleed away to
// digital3.
//
// Then just to be perverse we will display the brightness back on the
// same LED by turning it on for a millisecond. This happens more often
// with brighter lighting, so the LED is dim in a dim room and brighter
// in a bright room. Quite nice.
//
// (Though a nice idea, this implementation is flawed because the refresh
// rate gets too long in the dark and it flickers disturbingly.)
//
#define LED_N_SIDE 2
#define LED_P_SIDE 3
void setup()
{}
void loop()
{
unsigned int j;
// Apply reverse voltage, charge up the pin and led capacitance
pinMode(LED_N_SIDE,OUTPUT);
pinMode(LED_P_SIDE,OUTPUT);
digitalWrite(LED_N_SIDE,HIGH);
digitalWrite(LED_P_SIDE,LOW);
// Isolate the pin 2 end of the diode
pinMode(LED_N_SIDE,INPUT);
digitalWrite(LED_N_SIDE,LOW); // turn off internal pull-up resistor
// Count how long it takes the diode to bleed back down to a logic zero
for ( j = 0; j < 30000; j++) {
if ( digitalRead(LED_N_SIDE)==0) break;
}
// You could use 'j' for something useful, but here we are just using the
// delay of the counting. In the dark it counts higher and takes longer,
// increasing the portion of the loop where the LED is off compared to
// the 1000 microseconds where we turn it on.
// Turn the light on for 1000 microseconds
digitalWrite(LED_P_SIDE,HIGH);
digitalWrite(LED_N_SIDE,LOW);
pinMode(LED_P_SIDE,OUTPUT);
pinMode(LED_N_SIDE,OUTPUT);
delayMicroseconds(1000);
// we could turn it off, but we know that is about to happen at the loop() start
}
A more elaborate version with 20 LEDs and thorough documentation can be found here: http://blog.blinkenlight.net/experiments/measurements/led-camera/ |
阿莫论坛20周年了!感谢大家的支持与爱护!!
一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
|