I once mentioned in a radiosonde hunting article that I change the temperature and humidity sensor of a RS41 sonde to a new one when reprogramming it because the calibration values for the original sensors get lost in the programming process. In this article I will describe how to connect the HUT21 temperature and humidity sensor to the radiosonde. At first you should know that the STM32 microcontroller of the sonde has an I2C port which is available on the extension port (white header connector). Moreover, programming the microcontroller via SWD is also possible on the extension port. For programming you will need an ST-Link and the free STM32CubeIDE software.
The connector pinout is as following (view into port from outside):
______________________| |______________________
| |
| 1 2 3 4 5 |
| |
| 6 7 8 9 10 |
|_______________________________________________________|
1 - SWDIO (PA13) (for programming)
2 – RST
3 - MCU switched 3.3V out to external device
4 - I2C2_SCL (PB10) (for I2C sensor)
5 – GND (for I2C sensor / programming)
6 – GND (for I2C sensor / programming)
7 - SWCLK (PA14) (for programming)
8 - +U_Battery (for I2C sensor)
9 - +VDD_MCU
10 - I2C2_SDA (PB11) (for I2C sensor)
I recommend buying a HUT21 breakout board from Amazon which already has a voltage regulator on board. This means you could use +U_Battery as supply voltage. But when using the sensor without a voltage regulator +VDD_MCU should be used. Furthermore, most breakout boards also have the bus pull up resistors on them, so you don’t need to use the internal pull ups of the microcontroller. I won’t go into detail about configuring the I2C port of the microcontroller in STM32CubeIDE since there should be some good tutorials on the web.
The connection between the sensor breakout board and the RS41 is rather simple:
RS41 | Sensor board |
+U_Battery (8) | VDD |
GND (6) | GND |
I2C2_SCL (4) | SCL |
I2C2_SDA (10) | SDA |
Finally, here is some code that I originally wrote for a chicken incubator (sorry for German comments they will be translated soon). It will give you the temperature in °C * 100 and the humidity in % * 10.
uint8_t get_mesurement()
{
//OE3TEC 2020
uint8_t i2c_Data[10], err = 0;
uint32_t pre_temp, pre_hum;
//Sind globale Variablen
hum = 0; //Feuchtigkeit [%] * 10 (503 -> 50,3%)
temp = 0; //Temperatur [°C] * 100 (2356 -> 23,56°C)
//Thermometer
i2c_Data[0] = 0xF3; //Register "Adresse" (0xF3 Temperatur Messung triggern)
//I2C sollte mit CubeMX konfiguriert werden (STM32CubeIDE)
HAL_I2C_Master_Transmit(&hi2c1,0x40 << 1,(uint8_t *) i2c_Data,1,1000); //Adresse ist 0x40 muss aber laut doku der HAL-Library um ein Bit nach rechts geschoben werden
HAL_Delay(60); //Temperatur Messdauer
//Messwerte auslesen
HAL_I2C_Master_Receive(&hi2c1,0x40 << 1,(uint8_t *) i2c_Data,3,1000);
//Temperatur berechnen
if( (0x01 & (i2c_Data[1] >> 1)) == 0) //Wurde die Temperatur gemessen?
{
pre_temp = 0;
pre_temp = i2c_Data[0] << 8;
pre_temp |= i2c_Data[1];
pre_temp &= ~0x03;
temp = (17572 * pre_temp / 65536) - 4685;
}
//Hygrometer
i2c_Data[0] = 0xF5; //Register "Adresse" (0xF5 Feuchtigkeits Messung triggern)
HAL_I2C_Master_Transmit(&hi2c1,0x40 << 1,(uint8_t *) i2c_Data,1,1000); //Adresse ist 0x40 muss aber laut doku der HAL-Library um ein Bit nach rechts geschoben werden
HAL_Delay(26); //Temperatur Messdauer
//Messwerte auslesen
HAL_I2C_Master_Receive(&hi2c1,0x40 << 1,(uint8_t *) i2c_Data,3,1000);
//Feuchtigkeit berechnen
if( (0x01 & (i2c_Data[1] >> 1)) == 1) //Wurde die Feuchtigkeit gemessen?
{
pre_hum = 0;
pre_hum = i2c_Data[0] << 8;
pre_hum |= i2c_Data[1];
pre_hum &= ~0x03;
hum = (10 * 125 * pre_hum / 65536) - 6 * 10;
}
if((temp > 5000) || (temp < 500) || (hum > 1000)) err = 1; //Error detection
return err;
}