Making an RP2040 temperature sensor

By Jo Hinchliffe. Posted

In this final project of the KiCad series, we’re going to adapt our RP2040 layout to make a rudimentary weather station. The PCB will host an AHT20 sensor, which is a good-quality sensor that can detect both temperature and humidity via I2C. It’s made by the same company that created the venerable DHT22, which has long been a popular sensor for this type of project.

Scouring the JLCPCB parts library revealed that they have two versions of the AHT20 in surface-mount packages. It’s important to double-check components carefully, as one of the options (the F variant) was built so that the vent hole for the sensor was placed underneath the package, so required a very accurate footprint that included a matching hole through the PCB under the footprint. With our correct package identified, we created a KiCad footprint for the 6-pin device (although two pins on the device are not connected).

The next step is a quick read of the datasheet. It’s pretty straightforward to connect the AHT20, with SDA and SCL pins having a pull-up resistor and a bypass capacitor placed near the package. So far, straightforward. The datasheet also recommends, where possible, that the package has a routed channel around it to isolate it thermally from the rest of the system. You can get away without this, but your temperature readings will be influenced by the rest of the components on the board. You can never fully thermally isolate a temperature sensor, but the more you can disconnect it from the rest of the board, the more accurate your readings will be.

There are two ways we can create this cutout in KiCad: add the cutout in the footprint, or place the component in the PCB design and then add the cutout manually. Obviously, if you add the cutout to the footprint, you then have to edit the footprint if you want to change the cutout design. Then, if you have placed a footprint with the channel in the PCB Editor, but not created an overall edge-cut geometry, you can’t correctly preview your board as the 3D viewer will consider the small channel the outside edge of the entire design.

Figure 1: Adding a slot around the AHT20 sensor to thermally isolate it somewhat from the PCB

We opted to add the channel in the PCB editor, having placed and routed the AHT20 sensor. We’ve often, in this series, used Inkscape externally to create and then import SVGs for graphic elements and edge-cut geometries but, for simple items like this, using the ‘Draw a basic polygon’ tool in KiCad is simple and convenient.

It’s important that you check what are the minimum slot dimensions that your fabricator can handle, as this will be limited by the size of the bit they use for routing. At time of writing, this is 1 mm on JLCPCB, so we made our channel around 1.5 mm wide (Figure 1).

The rest of the board lay up was pretty straightforward. We wanted to be able to hook this board up to a wide range of outputs, so we broke out a healthy number of GPIO pins, and also added a small pin header with numerous power connections.

Our completed PCB layout

After the usual session of silkscreen labelling and tidying, we then went through the familiar process of creating Gerbers, BOM, and Centroid positional files, and uploaded the board for fabrication and assembly.

Bring it to life In the previous issue, we looked at creating a custom version of CircuitPython but, this issue, we’ll use the easier (though perhaps a little more boring) method of using the CircuitPython build for Raspberry Pi Pico, and incorporating any changes we need in the code.

When you receive your boards, they should come with blank flash chips, which means that when you plug them into a computer, an RP2 flash drive appears. You can copy the Raspberry Pi Pico build of CircuitPython onto this. Once this has fully copied over, the RP2 drive should disappear and a CircuitPython drive should appear. The only thing we need to add to this is the DotStar module that we’ll need to control our LEDs. You can get this from the Library bundle. This comes as a zip file, so extract it, and copy the adafruit_dotstar.mpy file from the lib folder to the lib folder on the CircuitPython drive. Your device is now all set up and ready to start coding.

This is smaller and more robust than a dev board and separate temperature sensor

The AHT20 sensor on this board is connected via I2C, and we can connect to it using the adafruit_ahtx0 module. We just need to set up an I2C object first. This is done with:

<b>i2c = busio.I2C(board.GP1, board.GP0) </b>

<b>sensor = adafruit_ahtx0.AHTx0(i2c)</b>

Once this is set up, you can access the temperature and humidity with


Now we’ve got temperature and humidity readings on the board, the next question is what to do with them. We’ve broken out the spare GPIO pins, so we can hook up almost any other hardware we want. We’ve opted for two strips of DotStar LEDs. These are addressable RGB LEDs, so we can set any of them to be any colour. We’re going to use them as thermometer-style outputs, with one LED lit up at a time. As the temperature or humidity goes up, the lit LED moves up the relevant strip.

The DotStar LED strips have four connections, VCC goes to 5 V, GND goes to GND (and there are two of each on the board, so they can connect directly to the pin). Each strip also has a Clock and Data input connection, labelled SCK and DIN. We’ve used pins 23 and 24, and 27 and 28 for these, but any of the GPIO pins will work.

A break in the PCB thermally isolates the temperature sensor as much as possible

We can create the two DotStar objects with:

<b>dots_temp = dotstar.DotStar(board.GP28, board.GP27, 14, brightness=0.2, auto_write=False)</b>

<b>dots_humid = dotstar.DotStar(board.GP24, board.GP23, 14, brightness=0.2, auto_write=False)</b>

You can then access them like lists and assigning an RGB tuple to an element in the list with. For example, dots_temp[0] = (100,0,0). There are also some methods that we can use including fill(), which sets all LEDs in the string to a particular colour. The full code for our temperature and humidity thermometers is:

<b>@import time</b>

<b>import board</b>

<b>import busio</b>

<b>import adafruit_ahtx0</b>

<b>import board</b>

<b>import adafruit_dotstar as dotstar</b>

<b>dots_temp = dotstar.DotStar(board.GP28, board.GP27, 14, brightness=0.2, auto_write=False)</b>

<b>dots_humid = dotstar.DotStar(board.GP24, board.GP23, 14, brightness=0.2, auto_write=False)</b>

<b>i2c = busio.I2C(board.GP1, board.GP0) </b>

<b>sensor = adafruit_ahtx0.AHTx0(i2c)</b>



<b>temp_range = (10,30)</b>

<b>temp_colour = (100,0,0)</b>

<b>humid_range = (0,100)</b>

<b>humid_colour = (100,100,0)</b>

<b>while True:</b>

<b>print(“\nTemperature: %0.1f C” % sensor.temperature)</b>

<b>print(“Humidity: %0.1f %%” % sensor.relative_humidity)</b>



<b>temp_index = int(((sensor.temperature-temp_range[0])/(temp_range[1]-temp_range[0])) * len(dots_temp))</b>

<b>dots_temp[temp_index] = temp_colour</b>


<b>humid_index = int(((sensor.relative_humidity-humid_range[0])/(humid_range[1]-humid_range[0])) * len(dots_humid))</b>

<b>dots_humid[humid_index] = humid_colour</b>



You can save this to your board and you should see the appropriate LEDs light up. Hold your thumb over the temperature sensor and you should see the LEDs change.

Make sure you attach wires to the right end of the LED strip (the arrow shows the direction the information travels through the strip)

Going further We’ve now come to the end of our series on KiCad, and we’ve worked our way up from simple boards that connect modules and breakout pins, to creating our own microcontroller boards with the hardware we want, and we’ve programmed them along the way. From here, you can take this hobby wherever you like. If you’re more interested in practical solutions, then you can keep designing boards as you need them. If you’re interested in the skill of PCB design, then you can work on technically more challenging boards. For example, you could try starting with some of our examples and seeing how small you can make them. You could work on interesting or artistic boards, such as those used for badges at hacker-focused tech events.

From HackSpace magazine store


Subscribe to our newsletter