Magnetometer sensitivity

Posted on
  • Am I interpreting the docs correctly in that Puck can be woken up on a mag event. I can adjust the sample rate down but I still get readings even when the puck is stationary. I'm wondering if this is because the readings - despite the puck not moving - are actually changing. They differ each reading by a very small amount.

    Which leads me to ask, if this is the case and the small changes trigger a reading, can the sensitivity be turned down so that when it is stationary the Puck can sleep.

  • The senser will raise an event every time when a new measurement is available. There is no sensitivity or the like.

  • As @luwar says - the sensor itself will always wake Espruino up.

    I was vaguely considering adding code to make it only run JS if the reading changed by a certain amount, but realistically the power saving from doing that isn't that great.

  • @Gordon
    I'm new to puck js and this is just an amazing device, I'm glad with it and get it working in less than 5 minutes.
    Working everyday with ESP32, Lopy, Sodaq and LoraWan devices, your little puck open me new doors for business.
    I usually use accelerometer with IRQ detection with programmed thresold to get IRQ only when device is moved.
    So according this post it's not possible yet, so what's the best deal to detect move of the device with best consumption usage ? (compare values between wakes ?)
    Any example code would be welcome
    thanks for your help

  • I have a post here, which is what I've been using to detect movement:­301185/

    By default it measures the magnetic field at 0.63Hz, which keeps the wakeups to a minimum while still being fast enough to detect movement pretty well.

    Hope that helps!

  • @Gordon
    Thanks, I missed this one
    I tried something similar with start/stop movement detection with button, when pressed, Green led indicate it's on and then blink Red in case of movement. another push stop detection by blue blink
    New position is saved like this moving and leaving puckjs to another place will stop detection until it's moved again. Seems to works, need to play with threeshold.
    Here the code, I'll try your since your math detection looks better than mine ;)

    var th = 10;
    var x =0;
    var y =0;
    var z =0;
    var accFirstRead = true;
    var acc = false;
    function doJob(x,y,z) {
      var b = Puck.getBatteryPercentage();
      var t = E.getTemperature();
      var l = Puck.light()*100 ;
      console.log("Batt", b, "  Temp",t, "  Light",t );
      console.log("Diff X:",x, " Y:",y, "Z:",z);
    setWatch(function() {
      if (acc) {
        acc = false;
        digitalPulse(LED3, 1, 100);
        console.log("Accel OFF");
      } else {
        accFirstRead = true;
        acc = true;
        digitalPulse(LED2, 1, 100);
        console.log("Accel ON");
    }, BTN, {edge:"rising", debounce:50, repeat:true});
    Puck.on('mag', function(xyz) {
      var diffx = xyz.x-x;
      var diffy = xyz.y-y;
      var diffz = xyz.z-z;
      x = xyz.x;
      y = xyz.y;
      z = xyz.z;
      //digitalPulse(LED3, 1, 5);
      if (accFirstRead) {
        accFirstRead = false;
      } else {
        if (Math.abs(diffx)>th || Math.abs(diffy)>th*2 || Math.abs(diffz)>th*2) {
          digitalPulse(LED1, 1, 250);
          doJob(diffx, diffy, diffz);
        } else {
          //console.log("Diff X:",diffx, " Y:",diffy, "Z:",diffz);
  • Hi @Gordon,

    I have a magnetometer related question and think this post could host it, if not please move it.

    I need to reset the magnetometer upon my command, now can I do that ? Now when you upload a new code or restart the Puck, the magnetometer value is reset. Do you have a possibility somewhere to store the last value take it once restarted ? Can I do that custom implementation on my own ?

    Thank you!

  • At the moment there's no 'built-in' calibration, so it all has to be done by your own code anyway.

    A lot of code does something like this:

    var zero = Puck.mag();
    var doorOpen = false;
    function onMag(p) {
      p.x -= zero.x;
      p.y -= zero.y;
      p.z -= zero.z;
      var s = Math.sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
      var open = s<1000;
      if (open!=doorOpen) {
        doorOpen = open;
        digitalPulse(open ? LED1 : LED2, 1,1000);
    Puck.on('mag', onMag);

    So in that case, the calibration value is stored in the zero variable. If you want to reset the magnetometer value, all you have to do is run zero = Puck.mag(); again.

  • Hi @Gordon,

    I need first zero to be saved forever, my starting point for any calculations. So one approach could be, on first code upload to save externally the first zero, and also save it on the Puck which will survive during resets. If a new code is uploaded it must be saved, containing the first zero as value and discard the current Puck.mag() value. What do you think ?

    Thank you!

  • I'm not quite sure I understand, but it sounds ok.

    If you want to save just the value without doing a full save(), take a look at

  • Hi @Gordon,

    This is what I needed, it's working. When the FlashEEPROM data is cleared ? On firmware update and lack of power like battery change ?

    Thank you!

  • Right now, the data shouldn't ever get cleared - unless you call the erase function in the library.

    In future firmwares, doing a firmware update may clear it though - in a lot of cases it's a convenient way of resetting absolutely everything back to defaults.

  • Hi @Gordon,

    How can I read all the information stored ? When I try


    it dumps "xxxxx"..."xxxxxx", i.e. prints just the beginning and the end of the stored string if it is too long ?

    Thank you!

  • It's actually reading all the data, but the console is truncating it so you don't accidentally print lots of stuff to your screen.

    All you need to do is type print(E.toString( to print the info - print won't truncate it for you.

    If you still need it escaped and in String form, try: print(JSON.stringify(E.toString(­))))

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Magnetometer sensitivity

Posted by Avatar for Ollie @Ollie