This is the code for version 1 microbits and version 2 microbits, version 1 microbits work on pins 0 to 16 inclusive excluding pins 5 and 11 and the is the same for version 2 microbits except they don't also work on pin 16. on a version 2 microbit for some reason there are a lot of exceptions on reading the sensor, both invald checksum and too few bits, i don't know why this is.
This sensor uses 1 wire for communication, the receipt of bits is written in assembler, it's the only way i could manage to receive all of the data.
There is one user method .read this will either return a tuple of valid temperature and humidity data or raise an exception. The only exception to be trapped with a try: except: block is DataError. The sensor may also raise a ValueError for using an unsuitable pin or a general Exception if the are issues with the assembler routines.
There is some example code at the end of files version_1.py and version_2.py ,you need to run a REPL session to see the data output.
The sensor is wired as following :
| Pin | |
| 1 | 3 volts (Vcc) |
| 2 | Data IO, this needs a 5k pull up resistor |
| 3 | Not connected |
| 4 | Ground |