How to read BBC:microbit infrared and magnetometer sensor values?

106 Views Asked by At

How can I read the value of the BBC micro:bit's infrared (IR) and magnetometer sensors in JavaScript and microPyhon?

Seems that in Javascript let irValue = input.temperature() uses he IR sensor to provide temperature in Celsius instead of infrared light intensity.

And let heading = input.compassHeading() presents a text message asking to to tilt the microbit vs. providing the heading angle.

1

There are 1 best solutions below

1
Clifford On

The microbit has no in board IR sensor. It has a temperature sensor which is actually the core temperature sensor in the MCU. It can be used to at best estimate ambient temperature, since to a variable extent it will be influenced by how hard the core is working and self-heating. The documentation is clear on that:

The micro:bit checks how hot its CPU (main computer chip) is. Because the micro:bit does not usually get very hot, the temperature of the CPU is usually close to the temperature of wherever you are. The micro:bit might warm up a little if you make it work hard, though!

The fact that you have found code that misleadingly assigns the temperature sensor output to a variable that implies it is measureing IR intensity does not make it true.

You could use an IR photo-diode to measure IR intensity via one if the available analogue input pins. Be aware that many external IR sensors are intended for IR remote control and data and are digital in nature, not analogue. They detect a modulated IR signal as binary (on/off) states not intensity.

With respect to the magnetometer, it is a three axis device, and some math and calibration is required to get a compass heading. input.compassHeading() is part of a higher level API that performs that math. The documentation includes:

Calibration

Every time you start to use the compass (for example, if you have just turned the micro:bit on), the micro:bit will start a calibrate compass (adjust itself). The calibration step will ask you to draw a fill pattern on the screen by tilting the micro:bit. The message asking you to tilt the micro:bit is no doubt some sort of first-time calibration procedure.

Which is what you are seeing in first-time use of the compass. You can run the calibration separately if you want to do that before actually reading the compass. input.calibrateCompass().