https://blog.jgc.org/2024/06/two-ways-to-use-led-as-light-sensor.html John Graham-Cumming's blog 2024-06-19 Two ways to use an LED as a light sensor with Arduino I needed to log when a light switched on and off during the night as part of debugging an oddly behaving movement sensor. To do that I built a really simple light sensor logger using an Arduino Leonardo, a large LED and a resistor or three. Here it is: [light-sense-1] And here's the entire Arduino code: void setup() { Serial.begin(9600); analogReference(EXTERNAL); } void loop() { Serial.println(analogRead(A1)); delay(1000); } The main loop() just reads the voltage across the LED from analog input A1 and writes it to the serial port. Then it waits for a second. The setup() routine tells the ADC converter to use the voltage on AREF as the reference maximum voltage for the ADC. The LED has a voltage of between about 0mV and 400mV depending on the amount of light. By default the voltage range of the ADC on the Arduino Leonardo is 0V to 5V but it's possible to scale it with a call to analogReference(). Calling analogReference(INTERNAL) would set the range to 0V to 2.56V which is still a bit large given the small voltage across the LED. AREF to the rescue! The AREF pin allows a range to be set for the ADC. It has an internal resistance of 32K and I added a 66K resistor (made from three resistors I had lying around) connected to the Arduino's 3.3V pin. The 66K and 32K form a voltage divider giving an AREF of 1.1V (which is the low end of valid AREF values). Then it's just necessary to call analogReference(EXTERNAL). Here's the output of the program when I had covered the LED with my hand and then uncovered it. [light-sense-2] You can also use the built in Arduino Serial Plotter to get an instant chart. Here I covered the LED with my hand, took my hand away and then shone a torch directly into the LED. [light-sense-3] To get timestamped output I am using CoolTerm which will automatically save to a file and add timestamps. [light-sense-5] That light sensor via LED relies on the photovoltaic effect: when light strikes the PN junction inside the LED it creates a voltage (albeit a small one). But there's another way to measure the light hitting an LED. Reverse bias If the LED is reverse biased (i.e. connected to a voltage source the wrong way round) then it won't light up but it will act as a capacitor that charges when light hits it. So the idea is to reverse bias the LED using two digital pins on the Arduino (one set HIGH and one set LOW) and then remove the bias and measure how long the LED (acting as a capacitor) takes to discharge. The brighter the light the faster the discharge. This can be achieved with a single LED connected between two digital pins like this: the +ve lead of the LED goes into the pin identified by LED_P and the -ve into the pin identified by LED_N. [light-sense-6] And here's the code. #define LED_N 4 #define LED_P 5 void setup() { Serial.begin(9600); } void loop() { unsigned long maxtime = 5000; pinMode(LED_N, OUTPUT); pinMode(LED_P, OUTPUT); digitalWrite(LED_N, HIGH); digitalWrite(LED_P, LOW); pinMode(LED_N, INPUT); digitalWrite(LED_N,LOW); unsigned long elapsed = 0; unsigned long start = millis(); while ((digitalRead(LED_N) != 0) && (elapsed < maxtime)) { elapsed = millis() - start; } Serial.println(elapsed); if (elapsed < maxtime) { delay(maxtime - elapsed); } } I found this worked better than the first version with more sensitive output. Note that smaller numbers mean brighter here (whereas in the previous code smaller meant darker). Here's a plot showing me covering the LED twice with my hand, followed by shining a torch at it. Each time I let the LED be exposed to ambient light between tests. [light-sense-4] In action Certainly looks like that motion sensor is turning the light on in the night uselessly. [light-sense-7] at June 19, 2024 Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest Labels: hardware 1 comment: [blo] Dennis said... Hi, another way is following: +5v -> LED(-) LED(+) -> Resistor_1 Resistor_2 -> Ground +5V---|<---A----===----GND This way you can use Location A as a Voltage input to your ADC. You have to play around with the resistor, but it should be in the KOhm range. 8:15 PM [icon_delet] Post a Comment Older Post Home Subscribe to: Post Comments (Atom) Labels * pseudo-randomness * hardware * babbage * anti-spam * gnu make * retro * security * codes and ciphers * the geek atlas * mathematics * minitel * behind the screens * popfile * privacy * radio Popular Posts * [AEn0k_sBy7] Controlling the Taylor Swift Eras Tour wristbands with Flipper Zero Many large concerts feature wristbands that light up on command. They are used to produce varied visual effects across a stadium. One compan... * [light-sens] Two ways to use an LED as a light sensor with Arduino I needed to log when a light switched on and off during the night as part of debugging an oddly behaving movement sensor. To do that I built... * [acs-1] Acorn Computer Systems catalogue circa 1983 I unearthed a catalogue that I'd picked up in around 1983 of Acorn Computer Systems . This catalogue overlaps the BBC Micro era (which w... * [255k-1] Fixing my iPhone 13 Pro cameras at -18C Look, I don't recommend you do this with your phone because bad things may happen, but the front and back cameras on my iPhone 13 Pro st... * [Picture] Your last name contains invalid characters My last name is "Graham-Cumming". But here's a typical form response when I enter it: Does the web site have any idea how rud... * [proposal-1] The original WWW proposal is a Word for Macintosh 4.0 file from 1990, can we open it? The W3C has a page with the original WWW proposal from Tim Berners-Lee. One of the downloads says The original document file (I think - I ... * [tsb-1] "Hacker News" for retro computing and gaming I noticed over time that I was drawn to the retro computing or gaming posts on Hacker News . So, I've set up a dedicated web site in the... * [mac-1] My daily driver is older than I thought; it's positively vintage! I was doing some clean up on my main laptop and realized it had been a while since bought a new computer. Turns out it was a lot older than ... * [firecuda-2] Retrieving 1TB of data from a faulty Seagate Firecuda 530 drive with the help woodworking tools So, a while ago I built a gaming PC with the following specification: Case Dan A4 v4.1 Motherboard Z690I Strix Gaming Pro... * [economist-] The Economist's ugly new print typeface Turning to the back page of a recent copy of The Economist to read the obituary of Paul Alexander the eye is assaulted by The Economist'... Blog Archive * V 2024 (11) + V June (3) o Two ways to use an LED as a light sensor with Ard... o Acorn Computer Systems catalogue circa 1983 o Fixing my iPhone 13 Pro cameras at -18C + > May (1) + > April (1) + > March (3) + > February (2) + > January (1) * > 2023 (30) + > December (2) + > November (5) + > October (3) + > September (4) + > August (1) + > July (5) + > June (1) + > May (3) + > April (3) + > March (3) * > 2022 (24) + > December (2) + > November (3) + > October (8) + > September (1) + > July (2) + > June (2) + > April (2) + > March (4) * > 2021 (6) + > November (1) + > July (1) + > May (2) + > April (1) + > January (1) * > 2020 (2) + > June (2) * > 2018 (2) + > December (2) * > 2017 (4) + > May (1) + > April (3) * > 2016 (11) + > November (1) + > September (1) + > July (2) + > June (1) + > May (3) + > April (1) + > March (2) * > 2015 (11) + > November (1) + > July (2) + > May (1) + > April (5) + > March (1) + > January (1) * > 2014 (4) + > November (2) + > July (1) + > June (1) * > 2013 (39) + > September (4) + > August (1) + > July (4) + > June (2) + > May (5) + > April (12) + > March (3) + > February (2) + > January (6) * > 2012 (77) + > December (3) + > November (6) + > October (6) + > September (7) + > August (6) + > July (6) + > June (8) + > May (4) + > April (9) + > March (7) + > February (9) + > January (6) * > 2011 (79) + > December (5) + > November (7) + > October (2) + > September (5) + > August (2) + > July (8) + > June (10) + > May (6) + > April (9) + > March (6) + > February (12) + > January (7) * > 2010 (95) + > December (13) + > November (9) + > October (16) + > September (19) + > August (2) + > July (9) + > June (12) + > May (1) + > April (1) + > March (4) + > February (2) + > January (7) * > 2009 (31) + > December (1) + > November (8) + > October (3) + > September (6) + > August (9) + > June (1) + > May (1) + > January (2) * > 2008 (19) + > December (2) + > September (1) + > June (3) + > May (3) + > March (2) + > February (6) + > January (2) * > 2007 (34) + > December (2) + > November (3) + > October (5) + > August (2) + > July (2) + > June (4) + > May (3) + > April (1) + > March (6) + > February (4) + > January (2) * > 2006 (25) + > December (2) + > November (2) + > October (3) + > September (5) + > July (1) + > June (1) + > April (6) + > February (1) + > January (4) * > 2005 (3) + > December (1) + > November (2) Copyright (c) 1999-2024 John Graham-Cumming. Awesome Inc. theme. Powered by Blogger.