D5-D6 KX023 accelerometer

Posted on
  • Hi wonderful Espruino folks!

    Whilst I'm waiting eagerly for my Bangle, figured I'd have a play with the D6 bracelet thing I have.

    It has an KX023 accelerometer, but I'm looking for using the device as a sleep and step counter.

    Using an accel as a step counter will drain the battery fast, but I wondered if anyone had attempted this? There's some clever features that I don't really understand of the accelerometer but the datasheet is sparse ( https://eu.mouser.com/datasheet/2/217/KX­023%20Sell%20Sheet_0-542331.pdf ).

    Anyone used this particular part and got any tips?



  • Fri 2019.11.29

    '. . . but the datasheet is sparse'

    Hello Martyn @iMartyn and thank you for posting the link to the datasheet.

    From that, I grabbed the URL (bottom p.1) to that company and opened the DDL for 'Developers':


    and scrolling down, found the detail datasheet for the KX023 (nine references total)


    Google'ing 'using the kx023 as a step counter'

    yielded this paper with pseudo code:


  • Using an accel as a step counter will drain the battery fast

    For using it as step counter (which is why it is in the device) it probably helps to use the buffer and interrupt and set the performance mode so it does not produce so much data. Then the main CPU can sleep most of the time. Original firmware lasts week or more in this mode so it is not so bad for the battery.

    I haven't used it for step counting yet but I tried the tap and doubletab features which can automatically guess that you hit the tracker from some side (once or twice) which can be used as additional buttons (check more info and code in this github issue)

    Now I am actually trying to sync DS-D6 build with all the new stuff for bangle.js including tensorflow. That could possibly be used for step counting too instead of some random guesswork which movement means one step.

    Tried yesterday evening and currently the build with tensorflow is too large to fit (by some small amount) so I need to trim it down a bit. I am also working on procedure for moving the bootloader so there is a bit more space (it takes 24KB now, can fit into 12KB and be moved by 12KB up).

  • @fanoush

    I tried the tap and doubletab features which can automatically guess that you hit the tracker from some side (once or twice) which can be used as additional buttons

    Nice and extremely creative! I like that very much.

  • Hey @fanoush, great that you're active here. To reduce the power drain, does espruino on the D6 use deep sleep at all? I'd like to use setTimeout() but if there's a better way for battery life, I'd love to know. If we can get to the same level (1 week on battery) as the stock firmware, that'd be incredible!

  • Sun 2019.12.01

    This suggests automatically:

    Description: 'This only works on STM32 Espruino Boards (nRF52 boards sleep automatically)'


  • does espruino on the D6 use deep sleep at all?

    yes, on nrf5x platform it works automatically out of box, it sleeps almost always. I got to the point that my simple watch app checking for button (and RX pin of serial console) lasts 30-40 days just sitting on the table with bluetooth advertising on (= default state).

    You can verify it by setting busy or sleep indicator (not sure what is the difference)
    temporarily to pin 25 which is vibration motor and see how long it vibrates when you interact with the watch.

    However recently I tried espruino bootloader made for SDK12 and Softdevice 3.1.0 and I don't see such great power management with that now. I have same build of (latest) espruino for SDK11 and SDK12 and with exactly same javascript code the one with SDK12/SD3.x lasts days while SDK11 lasts weeks so I am investigating what is different. It is built from same git source the only difference seems to be the SoftDevice. I also tried to call NRF.sleep() on SDK12/SD3.x and it still discharges faster than SDK11 with advertising on. Will need more tests, maybe that DS-D6 used has already worse battery (it is the first DS-D6 I got). I don't have any measurement tools for such low currents so I just keep some idle on my desk and check battery voltage after few days.

  • Hi, I am also looking at hacking the DS-D6, well, actually the Lenovo HX03W. I am a bit disappointed that the accelerometer does not include step counting out of the box!
    As for the algorithm I can point at this research we made some time ago exactly with the idea of having an open and validated step counting algorithm. The only problem is that the implementation is either in python or Java, not in C.
    If anyone has some time, you could port it to C, the algorithm is quite simple and does not require any strange dependency.

  • A followup on this: we have a C implementation of the algorithm, in case anyone wants to import it: https://github.com/Oxford-step-counter/C­-Step-Counter

  • Hello, nice, just checked it briefly and I am not sure about the 64bit time in processSample, what is the precision? miliseconds? can it be 32bit? or can I perhaps set frequency once? or send zero if this is just next sample with no delay. I guess I get batch of values from kx023 per interrupt but don't know the time, but values are probably spaced evenly with some preset frequency. But I guess I can start with time zero and then just increment 64bit value by some constant with each sample.

    EDIT: also looking into the code and ther is 64bit math used, is this really needed?, wouldn't 32bit integer or possibly 32bit float type be enough for such task?

    x,y,z data from kx023 is only 16bit

  • Hi fanoush,

    the time is meant to be in ms and it can be since the start of the system (the most common case) or since the start of the algorithm. If you don't have a clock, you can just feed a variable which you increment of +sampling_frequency each time, as you say yourself.

    It should be OK to reduce the type it to 32 bits, but there is no check when the clock is reset.

    Alternatively, you could just get rid of the timestamp altogether, but whenever time is used later in the algorithm, you need to assume that it has increased of a given period.

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

D5-D6 KX023 accelerometer

Posted by Avatar for iMartyn @iMartyn