STM32 drives temperature and humidity sensor HTU21D

Publisher:转眼人老Latest update time:2019-04-02 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 Introduction

The new generation of HTU21D temperature and humidity sensor from Humirel, France, sets new standards in terms of size and intelligence: it is embedded in a dual-row flat leadless DFN package suitable for reflow soldering, with a bottom surface of 3x3mm and a height of 1.1mm. The sensor outputs a calibrated digital signal in standard I2C format.


Write the picture description here

The HTU21D temperature and humidity sensor provides an accurate and reliable temperature and humidity measurement data for OEM applications. 

The interface and module of the controller are connected to achieve digital output of temperature and humidity. The resolution of HTU21D can be changed by inputting commands (8/12bit or even 12/14bit RH/T). The sensor can detect low battery state and output a checksum, which helps to improve the reliability of communication.


2. Specifications

Write the picture description here

The electrical characteristics at 25 degrees Celsius and 3.3V power supply are as follows:

Write the picture description here


Printed board design notes:


If the SCL and SDA signal lines are parallel to each other and very close to each other, signal crosstalk and communication failure may occur. The solution is to place VDD or GND between the two signal lines to separate the signal lines, or use shielded cables. In addition, reducing the SCL frequency may also improve the integrity of signal transmission. A 100nF decoupling capacitor must be added between the power pins (VDD, GND) for filtering. This capacitor should be as close to the sensor as possible.


Write the picture description here

Pin definition:

Write the picture description here

This experiment uses the finished HTU21D module, which can be directly connected to the STM32 development board.


Pin Description

Power supply pins (VDD, GND) The power supply range of HTU21 is 1.8VDC - 3.6VDC, and the recommended voltage is 3.0V. A 0.1uF decoupling capacitor must be connected between the power supply (VDD) and the ground (VSS), and the capacitor should be placed as close to the sensor as possible.

Serial Clock Input (SCK) SCK is used to synchronize the communication between the microprocessor and the HTU21. Since the interface consists of completely static logic, there is no minimum SCK frequency.

Serial Data (DATA) The DATA pin is a three-state structure and is used to read sensor data. When sending commands to the sensor, DATA is valid on the rising edge of SCK and must remain stable when SCK is high. DATA changes after the falling edge of SCK. When reading data from the sensor, DATA is valid after SCK goes low and remains until the next falling edge of SCK. To avoid signal conflicts, the microprocessor should drive DATA at a low level. An external pull-up resistor (for example: 10kΩ) is required to pull the signal to a high level. The pull-up resistor is usually included in the I/O circuit of the microprocessor.

3. Communication process

Htu21d follows the standard IIC for communication. For an introduction to IIC, please see —>IIC Special Topic (I) - Basic Knowledge Preparation. This article also refers to this article —>STM32F10x_Simulating I2C to Read and Write EEPROM.


3.1 IIC start signal

To start a transmission, sending a bit of data includes a transition of the DATA line to a low level while the SCK line is high. 

Write the picture description here

Write the picture description here

3.2 IIC stop signal

To terminate the transmission and stop sending data, the DATA line jumps to a high level during the high level period of the SCK line.


Write the picture description here

3.2 IIC data transmission

When the SCL clock level is low, the level of the SDA data line can be changed, and the SDA data is sent out during the rising edge of SCL. When SCL is high, the data on SDA remains stable.




I2C is transmitted in bytes (8 bits). After each byte is transmitted on the bus, there will be an acknowledge signal, which is always generated by the receiver. The clock of the communication process is provided by the master device (host).


IIC writes one byte:


Write the picture description here

Write the picture description here

After data transmission is completed, the response operation is performed. 

0: Indicates that the slave responds and can proceed to the next step; 

1: Indicates that the slave does not respond and the next operation cannot be performed.


IIC reads one byte:


The IIC read operation is similar to the send operation, except that the data is transmitted in the opposite direction.


Write the picture description here

Write the picture description here

3.3 IIC response signal processing

Waiting for reply:

Write the picture description here

Generates a response:


Write the picture description here

Generates a non-response:


Write the picture description here

4. HTU21D data measurement

The measurement commands are as follows:

Write the picture description here

This article uses a non-hold host: 

//Non-host mode


#define HTU_TEMP    0xf3

#define HTU_HUMI 0Xf5


In non-host mode, the MCU needs to query the sensor status. This process is completed by sending a start transmission timing, followed by the I2C first byte (1000'0001) as shown in the figure. If the internal processing work is completed, the microcontroller queries the confirmation signal sent by the sensor, and the relevant data can be read by the MCU. If the measurement processing work is not completed, the sensor has no confirmation bit (ACK) output, and the start transmission timing must be resent.


Write the picture description here

Sensor initialization:

Write the picture description here

Sensor data reading and conversion:


The default resolution of the sensor is set to 12 bits for relative humidity and 14 bits for temperature. The output data of SDA is converted into a two-byte data packet with the high byte MSB first (left-aligned). Each byte is followed by an acknowledge bit. The two status bits, i.e. the last two bits of the LSB, must be set to '0' before physical calculations are performed.


The conversion calculation is as follows:

Write the picture description here

Source code implementation:

Write the picture description here


Display method:


This experiment uses serial port printing and TFTLCD display. Serial port printing is helpful for program debugging.


Write the picture description here

As for how to display the measured float value directly on the LCD, the code briefly introduces two methods: 

LCD display mode 1: The sprintf function prints the result into the test array and converts it into a string for display (LCD_ShowString). 

LCD display mode 2: Split the obtained value into integers and decimals and display them directly on the LCD (LCD_ShowxNum). 

Liquid crystal displays are driven directly with reference to the atoms at the punctual point.



Reference address:STM32 drives temperature and humidity sensor HTU21D

Previous article:STM32CubeMX learning tutorial 10: Hardware I2C reading and writing AT24C02
Next article:Summary of i2c usage of stm32f3

Recommended ReadingLatest update time:2024-11-23 08:10

STM32 drives W25X64 memory
W25X64 is a high-capacity SPI FLASH products, W25X64 has a capacity of 64Mbit (8M), and the series also includes W25Q80/16/32, etc. W25X16, W25X32, W25X64 have 8192, 16384, 32768 programmable pages, 256 bytes per page, 16 pages can be erased each time with the sector erase command, 256 pages can be erased each time wi
[Microcontroller]
STM32 drives W25X64 memory
STM32 SPI drives TFT LCD
Using STM32F030's SPI to drive TFT LCD, the LCD I use is a 2.4-inch color screen with 240*320 pixels, and the driver IC is ili9341. Only four wires are needed to light up the LCD. Here is the SPI configuration. I'd like to state that the SPI configuration of F030 is somewhat different from that of F103, etc. /*LCD SPI
[Microcontroller]
Design of driving circuit for DMD projector based on STM32 microcontroller
DLP projection technology is a technology that uses the digital micromirror device (DMD) developed by Texas Instruments as the main key processing element to realize the digital optical processing process. The color displayed by DLP is high in clarity, bright, delicate and realistic, and it is a fully digital display
[Microcontroller]
Design of driving circuit for DMD projector based on STM32 microcontroller
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号