• 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});
    
About

Avatar for Gordon @Gordon started