One thing that is critical for the Cool Wave project is to know what temperature it is both instead and outside. I have a number of modules to test for this, but one I am confident that I will use is the Grove Temperature and Humidity Sensor with a DHT20 at the heart of it.
The DHT20 module has a lot of parallels with the DHT11 I have used many times before. The key differences are that it is more accurate, and it is communicated with the I²C protocol. The Grove module is effectively a DHT20 with voltage management built around it.
It is worth noting that the Seeeduino XIAO has a dedicated I²C controller on pins A4/D4 for SDA (GPIO 8) and A5/D5 for SCL (GPIO 9). This is great as talking to the HDT20 is not trivial.
First let's check that we can talk to the hardware using the I²C bus. Ideally I would use a Grove Base for Seeeduino XIAO to connect things up, but I don’t have one, so I am using jumper wires as photographed. Don’t judge me, but if you do judge me then make certain it is a comment with a good pun in it. :-)
SDA on the module goes to D4 in the Seeeduino XIAO, SCL goes to D5, GND to GND, and VCC to 3V3. The Grove module’s voltage regulator can use the 5V VCC instead of 3V3, but for initial testing we will do less damage if we mess up white using the 3.3V supply.
Now here is some test Python code that we can run through the Thonny IDE. It will search the I²C bus and list all the devices it can find. Note that we need to use the GPIO references in MicroPython and not the pin numbers.
from machine import Pin, I2C i2c = I2C(0, scl=Pin(9), sda=Pin(8)) print('Scanning I2C bus.') devices = i2c.scan() # this returns a list of devices device_count = len(devices) if device_count == 0: print('No i2c devices found.') if device_count == 1: print('1 device found.') else: print(device_count, 'device(s) found.') for device in devices: print('Decimal address:', device, ", Hex address: ", hex(device))
When this is run we should see that we have one device with the address of 56, or 0x38 expressed in hexadecimal.
Now we need to get some data from the sensor and convert it to a temperature. The good news is that there are many libraries that will do the heavy lifting for us. I have used the library from https://github.com/flrrth/pico-dht20 but others are available. To use this library download https://github.com/flrrth/pico-dht20/blob/main/dht20/dht20.py and use Thonny to save it to the Seeeduino XIAO. After that is done the following code should run and continually read the current temperature and humidity.
from machine import Pin, I2C from dht20 import DHT20 i2c = I2C(0, scl=Pin(9), sda=Pin(8)) dht20 = DHT20(0x38, i2c) while True: measurements = dht20.measurements temper = measurements["t"] humidity = measurements["rh"] print("temper : " + str(temper)) print("humidity : " + str(humidity))
And that is that done. Sadly we can only have one DHT20 on the I²C hardware bus as only one device ID is allowed pre bus, and we can not change the device ID of the DHT20, but I intend to use this for inside and a different sensor for outside so this is not an issue on this occasion. We should be able to use some other pins and implement I²C in software, but that is not needed on this occasion and what I lovingly call scope creep so let’s leave that test for another day.