A few days ago there was a conversation on the comments for the Puck.js electricity meter video
about whether an external light sensor was actually needed.
It turns out it isn't!
Puck.js has a light measurement function, but it just uses the red LED as a photodiode to generate a little bit of voltage which can be measured when there is light.
You can use the on-chip low-voltage comparator to look at the light value from the photodiode and to wake Puck.js up - which should be pretty good for low power operation. Because Espruino doesn't natively implement an interrupt for the low power comparator you have to do some magic with the PPI system and use the fact that D11 and D12 are joined together by a resistor, but it works great!
// Uses internal resistor between D11 + D12 (capsense) to connect GPIOTE output and input
// Triggers when there's a pulse of light!
var ll = require("NRF52LL");
// set up D12 as an output and create 'toggle' task
digitalWrite(D12, 0);
var tog = ll.gpiote(1, {type:"task",pin:D12,lo2hi:1,hi2lo:1,initialState:0});
// compare LED1 with LPCOMP
analogRead(LED1);
var comp = ll.lpcomp({pin:LED1,vref:2}); //<-------- 2/16 - could need changing depending on ambient light level
// use a PPI to trigger the toggle event
ll.ppiEnable(0, comp.eCross, tog.tOut);
// Now watch D11
setWatch(print,D11,{repeat:true, edge:1});
Users should just be aware of not using the red LED LED1 before or any of the other LED while reading, because it most likely messes with measuring... it is light after all.
Yes - originally I'd got it lighting the Blue LED and even that was enough to set up an oscillation!
Post a reply
Bold
Italics
Link
Image
List
Quote
code
Preview
Formatting Help
Don't worry about formatting, just type in the text and we'll take care of making sense of it. We will auto-convert links, and if you put asterisks around words we will make them bold.
Tips:
Create headers by underlining text with ==== or ----
To *italicise* text put one asterisk each side of the word
To **bold** text put two asterisks each side of the word
Embed images by entering: ![](https://www.google.co.uk/images/srpr/logo4w.png) That's the hard one: exclamation, square brackets and then the URL to the image in brackets.
* Create lists by starting lines with asterisks
1. Create numbered lists by starting lines with a number and a dot
> Quote text by starting lines with >
Mention another user by @username
For syntax highlighting, surround the code block with three backticks:
```
Your code goes here
```
Just like Github, a blank line must precede a code block.
If you upload more than 5 files we will display all attachments as thumbnails.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
A few days ago there was a conversation on the comments for the Puck.js electricity meter video
about whether an external light sensor was actually needed.
It turns out it isn't!
Puck.js has a light measurement function, but it just uses the red LED as a photodiode to generate a little bit of voltage which can be measured when there is light.
You can use the on-chip low-voltage comparator to look at the light value from the photodiode and to wake Puck.js up - which should be pretty good for low power operation. Because Espruino doesn't natively implement an interrupt for the low power comparator you have to do some magic with the PPI system and use the fact that D11 and D12 are joined together by a resistor, but it works great!