-
Replacing floating point by fixed point for the AHTxx sensors
05/31/2026 at 08:35 • 0 comments![]()
The AHTxx series of sensors, e.g. AHT20, AHT30, measure temperature and humidity. The sensor returns 20-bit values for measurements. They are to be converted to °C and %RH by the formulae given above.
All the libraries I've seen so far do the calculation in floating point. For a small MCU that doesn't have native floating point this pulls in extra library code. I wanted to see if this could be done with fixed point arithmetic.
First the humidity calculation. If we load the raw value into a 32-bit variable, multiplying by 100 will not overflow as the max value is 2^20*100. So we do this:
uint32_t l = raw * 100;
Next instead of dividing by 2^20, we divide by 2^16. This can be done by shifting right 16 bits (effectively taking the MSW of the 32-bit value).
uint16_t w = l >> 16;Now the integer part of the humidity % is in the top 12 bits and the sixteenths of % in the bottom 4 bits. We store these separately. For display we can convert the fractional part to a number between 0 and 10000 in steps of 625, and then use as many decimal digits of that as we want (the accuracy won't be as good as 1/16th %RH or °C anyway). Even a 16-entry lookup table will suffice.
hum->frac = w & 0x0F; hum->whole = w >> 4;A similar calculation is applied to the temperature, except that we multiply by 200. For the temperature range of this sensor this also will not overflow 32 bits. After isolating the integral part we subtract 50. Here's a catch: If the result is negative, then we must add 1 to the integral part and subtract the fractional part from 1 (actually 0b10000 since it's in sixteenths) so that both parts are non-positive.
This has been tested with an ESP8266 module (because of the 3.3V supply requirement) driven by an Arduino sketch.
-
I'd been doing resistor binning wrong
05/29/2026 at 09:58 • 0 comments![]()
I have a collection of axial resistors from long ago; I think it was a grab bag purchase. I had organised them into 12 containers for the E12 series (non-E12 values like 50k went into the closest bin). I realised I had been organising it the wrong way. When I want a value like 50k, I went looking in the 47 or 56 container, but I might be happy to accept anything from 39k to 68k.
So I reorganised them into half-decades, splitting a decade at sqrt(10) or roughly 3.12. Thus 1 to 3.3 in one bin and 3.3 to 10 in the next bin.
Curious as to how many I have in the bins, I weighed the containers, taring with an empty container. The results are in the chart where the vertical axis is grams, and the horizontal axis is the value in ohms. As expected my projects have depleted the middle bins. But I don't think I'll get to use up the 2.2M resistors before I move over to surface mount resistors.
-
Cheap programmable time modules
05/04/2026 at 12:41 • 2 comments![]()
These modules can be found on AliExpress for a few cents each if you buy 5 upwards.
![]()
It's a 12x12 mm module with the chip under the blob, typical of other cheapies like doorbell generators. The circuit shows that a resistor "programs" the delay, between 2 seconds and 1000
secondshours. It is powered by 2V to 5V so suitable for digital logic. It's effectively a non-retriggerable monostable where a low transition on the input causes the output to go low for the programmed period. There is a reference to a selectable multiplier. I think this refers to the solder jumpers which can be used to increase the delay by 8 to the power of 0 to 3. The documentation is very scarce, there is no formula provided for calculating the resistance required. A reviewer said just as much. I'm putting a link to this on the stack in the hope that someone knows more about these.You'll need to buy $15 worth of stuff for free shipping so they could be used to top up an order to reach the threshold. I might get a few next order.
Ken Yap



Ian Dunn
Alex Xia
CanHobby.ca
Guy Dupont
BlackIoT SAGL
Joren Heit
Michael
Just4Fun
Michael Wessel
Adam Wakelin
Alan Boris
supermarioprof
Markus Loeffler
Dave
twodogstar
Bohan Xu
Wouter Minjauw
Silica Gel
Hi Ken, thanks for liking my #Isetta TTL computer !