Puck.js trigger on light pulses (no components!)

Posted 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!

    // 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});
    
  • Neat!

    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
About

Puck.js trigger on light pulses (no components!)

Posted by Avatar for Gordon @Gordon

Actions