Sunday, 11 February 2024

Microbits and external temperature/humidity sensors

 Microbits come with a built-in temperature sensor, but sadly it's

  • built into the chip, so gets affected by the chip heat
  • built into the board, so hard to measure stuff other than by dropping your microbit into the water you're trying to measure the temperature of (for example!)
There are lots of temp/humidity sensors, but I happened to have these digital sensors lying around:
  • DS18B20 - the model in a can, so relatively waterproof and on a 1m lead
  • SHT30 - this is a T/RH measuring chip, which I have on a Wemos-style PCB, convenient for normal people to use
Also, at the Science Oxford Creative Computing session this weekend I bumped into a lady who is fundraising to buy a bunch of data sensors for her primary school - looking for ~£700 to buy maybe 5!! That seems like madness, as they already have a bunch of microbits and do coding on them. Why not look at combining the two resources?

DS18B20

This package uses the Dallas Semi OneWire protocol, which is basically serial data in a specific format. 

This was by far the easiest, as there is actually an official Makecode Extension for this. In the Makecode Editor, use the Extensions menu item, and search for "dstemp". This brings up a very simple Block code extension which accesses the DS18B20 that is connected to the specified pin.

I used a microbit extension board and breadboard to connect the DS18B20, along with a 4K7 pull-up resistor (connected between DS18B20 Data wire and 3V pin). It worked instantly! Total result. Of course, the microbit output can easily be sent to the Chrome browser Makecode console where it can be graphed and downloaded as a CSV. Fab.

SHT30

This chip uses 12C, a bus-based protocol which is already used in the microbit for the Accelerometer and Compass, on pins 19 & 20. So the SHT30 can be added to the bus with no problem. 

Making this work was a bit more complicated, probably because 
  • I didn't look hard enough
  • The use of separate modules in Mu/Mbit MicroPython wasn't obvious 
I ended up finding some MicroPython which needed a lot of adaptation, but there is a much simpler approach here, which if I'd found earlier I'd have borrowed immediately.

Again, the microbit extender board allowed me to connect the +3V, Gnd, pin 19 and 20 (default SCL/SDA pins). I'd already mounted pins on the Wemos board so I could plug it into the breadboard. 

My code, like that referred to above, ends up as 2 files:
  • main.py, which imports SHT30.py as a module (it's a class)
  • SHT30.py
The tricky bit was realising I had to load the SHT30.py file into the microbit file system using the Files menu item in Mu. Once I did that, it started to work. And of course it provides T and RH, which will be great for a primary school doing data collection and analysis.



No comments:

Post a Comment