Skip to main content

SI7021, HTU21, SHT21 Temperature and Humidity Sensor

SI7021, HTU21 and SHT21 are nearly identical I2C temperature (-10 to 85C) and humidity (0-80%) sensors.

Connections

Bus PirateSHT21Description
SDASDAI2C Data
SCLSCLI2C Clock
Vout/VrefVDD3.3volt power supply
GNDGNDGround

Setup

Bus Pirate [/dev/ttyS0]
HiZ> m

Mode selection
1. HiZ
2. 1-WIRE
3. UART
4. I2C
5. SPI
6. LED
x. Exit
Mode > 4

I2C speed
1KHz to 1000KHz
x. Exit
KHz (400KHz*) >
Data bits
1. 8*
2. 10
x. Exit
Bits (1) >
Mode: I2C

I2C> W
Power supply
Volts (0.80V-5.00V)

x to exit (3.30) >
3.30V requested, closest value: 3.30V
Set current limit?
n

Power supply:Enabled

Vreg output: 3.3
V, Vref/Vout pin: 3.3V, Current sense: 4.3mA

I2C> P
Pull-up resistors: Enabled (10K ohms @ 3.3V)
I2C>
  • Use the m mode command and select I2C
  • Configure I2C for 400kHz and 8bits of data
  • Enable the onboard power supply with the W command, and configure it for 3.3volts output. Optionally select a current limit of at least 50mA.
  • Enable the onboard pull-up resistors with the P command.

Measure humidity

Bus Pirate [/dev/ttyS0]
I2C> [0x80 0xf5] D:23 [0x81 r:2]

I2C START
TX: 0x80 ACK 0xF5 ACK
I2C STOP
Delay: 23ms
I2C START
TX: 0x81 ACK
RX: 0xAA ACK 0x02 NACK
I2C STOP

Start a measurement

  • The SI7021 I2C address is 0x80 (write) and 0x81 (read).
  • Command 0xf5 triggers a humidity and temperature measurement
  • The full I2C command begins with I2C START [, the device write address 0x80, the begin measurement command 0xf5 and ends with I2C STOP ]

Delay

  • It takes up to 23ms to complete a humidity and temperature measurement.
  • The SI7021 will not acknowledge its read or write address while the measurement is in progress. In software we can use this feature to repeatedly poll the device until the measurement is complete.
  • For this demo we'll just delay for 23ms D:23 before attempting to read.

Read measurement

  • After a 23ms delay a measurement is ready to read.
  • Send an I2C START [, the device read address 0x81, read two bytes r:2, end with an I2C Stop ].
  • Our sensor returned 0xaa 0x02.

Convert to humidity

Bus Pirate [/dev/ttyS0]
I2C> = 0xaa02
=0xAA02.16 =43522.16 =0b1010101000000010.16
I2C>
  • The two bytes can now be converted into a humidity measurement.
  • Use the = convert number format command to find the decimal equivalent of 0xaa02 for easier calculation (43522).

%RH= ((125 * value)/65536)-6
%RH= ((125 * 43522)/65536)-6
%RH= 77.01%

  • Percent humidity is calculated using the formula above.

Measure temperature

Bus Pirate [/dev/ttyS0]
I2C> [0x80 0xf3] D:100 [0x81 r:2]

I2C START
TX: 0x80 ACK 0xF3 ACK
I2C STOP
Delay: 100ms
I2C START
TX: 0x81 ACK
RX: 0x5E ACK 0xD4 NACK
I2C STOP
  • Use the 0xf3 command to measure temperature, similar to measuring humidity
  • Send command 0xf3 to the write address: [0x80 0xf3]
  • Delay according to the datasheet, but I used 100ms to be safe D:100
  • Now grab two bytes from the read address: [0x81 r:2]
  • The temperature value is 0x53 0xd4.

Convert to temperature

Bus Pirate [/dev/ttyS0]
I2C> = 0x5ed4
=0x5ED4.16 =24276.16 =0b0101111011010100.16
I2C>
  • Use the = convert number format command to find the decimal equivalent of 0x5ed4 for easier calculation (24276).

Temp C = ((175.72 * value)/65536)-46.85
Temp C = ((175.72 * 24276)/65536)-46.85
Temp C = 18.24 C

  • Temperature is calculated using the formula above.

Read serial number

Bus Pirate [/dev/ttyS0]
I2C>
I2C> [0x80 0xfa 0xf0] [0x81 r:8]

I2C START
TX: 0x80 ACK 0xFA ACK 0xF0 ACK
I2C STOP
I2C START
TX: 0x81 ACK
RX: 0x90 ACK 0x39 ACK 0x3C ACK 0xB8 ACK 0xA6 ACK 0x5A ACK 0xCC ACK 0x3A NACK
I2C STOP
  • Each chip has a unique 8 byte serial number. The serial number is mixed in with CRC error checking bytes, some assembly is required
  • Write command 0xfa 0xf0 to retrieve eight bytes: [0x80 0xfa 0xf0] [0x81 r:8]
  • Byte 1, 3, 5 and 6 (0x903ca6cc) the middle four bytes of the serial number
  • The other values are CRC error checking values that we'll ignore
Bus Pirate [/dev/ttyS0]
I2C> [0x80 0xfc 0xc9] [0x81 r:6]

I2C START
TX: 0x80 ACK 0xFC ACK 0xC9 ACK
I2C STOP
I2C START
TX: 0x81 ACK
RX: 0x01 ACK 0x42 ACK 0xAB ACK 0x00 ACK 0x80 ACK 0x7A NACK
I2C STOP
I2C>
  • Write command 0xfc 0xc9 and read 6 bytes: [0x80 0xfc 0xc9] [0x81 r:6]
  • The first and second byte (0x0142) are the last two bytes of the serial number
  • The fourth and fifth byte (0x0080) are the first two bytes of the serial number
  • The complete serial number of this chip is 0x0080903ca6cc0142.

Read firmware version

Bus Pirate [/dev/ttyS0]
I2C> [0x80 0x84 0xb8] [0x81 r]

I2C START
TX: 0x80 ACK 0x84 ACK 0xB8 ACK
I2C STOP
I2C START
TX: 0x81 ACK
RX: 0x20 NACK
I2C STOP
I2C>
danger

This command is only supported by the SI7021

  • A single byte indicates the sensor firmware version
  • Write command 0x84 0xb8 and then read one byte: [0x80 0x84 0xb8] [0x81 r]
ValueVersion
0xFF1.0
0x202.0
  • This chip has firmware version 2.0

Macro

Bus Pirate [/dev/ttyS0]
I2C> (2)
SI7021/HTU21/SHT21/HDC1080 Temp & Humidity sensor
Humidity:
[0x80 0xf5] D:23 [0x81 r:2]
70.14% (0x9b 0xee)
Temperature:
[0x80 0xf3] D:100 [0x81 r:2]
19.77C (0x61 0x10)
Serial Number:
[0x80 0xfa 0xf0] [0x81 r:8] [0x80 0xfc 0xc9] [0x81 r:6]
0x0080903ca6cc0142
I2C>

Macro (2) in I2C mode automates the commands on this page.

Get Bus Pirate 5

Community