Micro:bit analog read is agonizingly slow?

Hi @MCbubba77 ,

Since you mention a forever loop, I’m assuming you are working in MakeCode. MicroPython is probably different.

The forever loop has a 20ms pause built in on each iteration. There may be two reasons for this: 1) to allow other fibers to run to handle events, etc. and 2) to ensure the loop is slow enough to be noticeable to novice users (maybe). See here to see the actual loop. The period is at least 20ms, so the maximum on anything in a forever loop is 50Hz.

Here are some interesting examples/data from a V2:

  • Analog Read in a forever loop: This was able to do about 50 cycles through the loop per second (50Hz), so the read itself is negligible in comparison to the 20mS sleep in the forever loop.
  • Analog Read in Tight Loop: You can use a different style of infinite loop inTypeScript and omit the pause on each iteration. It was able to do about 29300 reads per second (and other logic). So a bit over 29kHz and maybe a little under 34.1 uS between reads. Of course, this may starve out other tasks.
  • You can include a pause(0) in a tight loop to ensure other events are handled. That appears to significantly impact the period. It was able to do about 250Hz.

It looks like the analog read itself isn’t really slow. The forever is just throttled.

Bill

2 Likes