SHT30 Temperature and Humidity Sensor
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a1z09.2.0.0.204e2e8d2hPeAl&id=556043263770&_u=h2t4uge5cf4f Materials download link: https://pan.baidu.com/s/1kisMJspcV6Qdr1ye9ElOlQ
Specifications
Operating voltage: 2.4-5.5V Operating current: 0.2~1500uA Temperature measurement range: -40~125℃ Temperature measurement accuracy: ±0.3℃ Humidity measurement range: 0~100%RH Humidity measurement accuracy: ±2%RH Output method: IIC Number of pins: 4 Pin
Principle Analysis
Module Schematic
SHT30 Address According to the data sheet, when the ADDR pin is connected to VSS (ground), the address is 0X44. The schematic already grounds it through the pull-down resistor R14. However, note that the actual address is 0X44 shifted left by one bit, because the lowest bit must be reserved for the read/write bit, so the actual address is 0X44<<1.
Measurement Mode The SHT30 has two measurement modes: single-shot mode and periodic mode. In single-shot mode, issuing a measurement command triggers one data acquisition. Each data set consists of a 16-bit temperature value and a 16-bit humidity value (in that order). During transmission, each data value is always followed by a CRC checksum. However, this mode is further divided into clock stretching mode and no clock stretching mode, as shown in the figure below.
In single-shot mode, different measurement commands can be selected. They differ in repeatability (low, medium, high) and clock stretching (enabled or disabled). The repeatability setting here affects the measurement duration and thus the overall energy consumption of the sensor.
In periodic measurement mode, clock stretching is disabled, but it can be divided into high, medium, and low repeatability measurements. The measurement period can be 0.5, 1, 2, 4, or 10 (in times/second) (the fastest measurement rate in this mode is 10 times per second). If the sensor is currently measuring data in one operating mode and you want to send other commands (it is recommended to first send a break command), let the sensor stop the current measurement and enter single-shot mode, then send the command. Note: if the measurement frequency is too high, it will cause the sensor to self-heat.
After setting the measurement period and repeatability level for the periodic measurement mode, you can read data at any time by sending a read command (0XE000). Once the read sequence ends, the values in the register are cleared; reading again at this point will return 0. After the next measurement is completed, the register value will be written again.
Porting Process
Pin Selection
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting. For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here. The only difference is that we replace the file names bsp_dht11.c and bsp_dht11.h with bsp_sht30.c and bsp_sht30.h, and rename the folder to SHT30.
Write Code
In the file bsp_sht30.c, write the following:
#include "bsp_sht30.h"
#include "stdio.h"
double Temperature = 0.0, Humidity = 0.0;
void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: SHT30_GPIO_Init
* Function Description: SHT30 pin initialization
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void SHT30_GPIO_Init(void)
{
gpio_config_t SHT30_config = {
.pin_bit_mask = (1ULL<<SHT30_SCL_PIN)|(1ULL<<SHT30_SDA_PIN), // Configure pins
.mode =GPIO_MODE_OUTPUT, // Output mode
.pull_up_en = GPIO_PULLUP_ENABLE, // Enable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down
.intr_type = GPIO_INTR_DISABLE // Disable pin interrupt
};
gpio_config(&SHT30_config);
}
/******************************************************************
* Function Name: IIC_Start
* Function Description: IIC start sequence
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Start(void)
{
SDA_OUT();
SCL(1);
SDA(0);
SDA(1);
delay_us(5);
SDA(0);
delay_us(5);
SCL(0);
}
/******************************************************************
* Function Name: IIC_Stop
* Function Description: IIC stop signal
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Stop(void)
{
SDA_OUT();
SCL(0);
SDA(0);
SCL(1);
delay_us(5);
SDA(1);
delay_us(5);
}
/******************************************************************
* Function Name: IIC_Send_Ack
* Function Description: Master sends ACK or NACK signal
* Function Parameters: 0=send ACK 1=send NACK
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Send_Ack(unsigned char ack)
{
SDA_OUT();
SCL(0);
SDA(0);
delay_us(5);
if(!ack) SDA(0);
else SDA(1);
SCL(1);
delay_us(5);
SCL(0);
SDA(1);
}
/******************************************************************
* Function Name: I2C_WaitAck
* Function Description: Wait for slave acknowledge
* Function Parameters: None
* Function Return: 0=ACK received 1=timeout, no ACK
* Author: LC
* Notes: None
******************************************************************/
unsigned char I2C_WaitAck(void)
{
char ack = 0;
unsigned char ack_flag = 50;
SCL(0);
SDA(1);
SDA_IN();
SCL(1);
while( (SDA_GET()==1) && ( ack_flag ) )
{
ack_flag--;
delay_us(1);
}
if( ack_flag <= 0 )
{
IIC_Stop();
return 1;
}
else
{
SCL(0);
SDA_OUT();
}
return ack;
}
/******************************************************************
* Function Name: Send_Byte
* Function Description: Write one byte
* Function Parameters: dat=data to write
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void Send_Byte(u8 dat)
{
int i = 0;
SDA_OUT();
SCL(0);//Pull clock low to start data transfer
for( i = 0; i < 8; i++ )
{
SDA( (dat & 0x80) >> 7 );
delay_us(2);
SCL(1);
delay_us(5);
SCL(0);
delay_us(5);
dat<<=1;
}
}
/******************************************************************
* Function Name: Read_Byte
* Function Description: IIC read sequence
* Function Parameters: None
* Function Return: Data read
* Author: LC
* Notes: None
******************************************************************/
unsigned char Read_Byte(void)
{
unsigned char i,receive=0;
SDA_IN();//Set SDA as input
for(i=0;i<8;i++ )
{
SCL(0);
delay_us(5);
SCL(1);
delay_us(5);
receive<<=1;
if( SDA_GET() )
{
receive|=1;
}
delay_us(5);
}
SCL(0);
return receive;
}
/******************************************************************
* Function Name: SHT31_Write_mode
* Function Description: Set measurement period and repeatability command in periodic mode
* Function Parameters: dat=command to set
* Common values: 0.5 times per second 0x2024
* 1 time per second 0x2126
* 2 times per second 0x2220
* 4 times per second 0x2334
* 10 times per second 0x2721
* Function Return:
* Author: LC
* Notes:
******************************************************************/
char SHT31_Write_mode(uint16_t dat)
{
IIC_Start();
// << 1 sets the last bit to 0, indicating a write command
Send_Byte((0X44 << 1) | 0 );
//Returns 0 if ACK received, 1 means communication failed
if( I2C_WaitAck() == 1 )return 1;
//Send the high 8 bits of the command
Send_Byte((dat >> 8 ) );
//Returns 0 if ACK received, 1 means communication failed
if( I2C_WaitAck() == 1 )return 2;
//Send the low 8 bits of the command
Send_Byte(dat & 0xff );
//Returns 0 if ACK received, 1 means communication failed
if( I2C_WaitAck() == 1 )return 3;
// IIC_Stop();
return 0;
}
/******************************************************************
* Function Name: crc8
* Function Description: CRC checksum
* Function Parameters: data=address of data to check len=length to check
* Function Return: Checksum value
* Author: LC
* Notes: None
******************************************************************/
unsigned char crc8(const unsigned char *data, int len)
{
const unsigned char POLYNOMIAL = 0x31;
unsigned char crc = 0xFF;
int j, i;
for (j=0; j<len; j++)
{
crc ^= *data++;
for ( i = 0; i <8; i++ )
{
crc = ( crc & 0x80 ) ? (crc << 1) ^ POLYNOMIAL : (crc << 1);
}
}
return crc;
}
/******************************************************************
* Function Name: SHT30_Read
* Function Description: Read temperature and humidity values
* Function Parameters: dat=read command
* Periodic mode command: 0xe000
* Single-shot mode command: 0x2c06 or 0x2400
* Function Return: 0=read success others=failure
* Author: LC
* Notes: Currently reads in periodic mode. To use single-shot mode,
* comment out the command under [Set periodic mode command].
******************************************************************/
char SHT30_Read(uint16_t dat)
{
uint16_t i = 0;
unsigned char buff[6] = {0};
uint16_t data_16 = 0;
float temp = 0;
float humi = 0;
//Set periodic mode command
SHT31_Write_mode(0x2130);//High repeatability measurement once per second (only valid in periodic mode)
IIC_Start();
Send_Byte( (0x44<<1) | 0);
if( I2C_WaitAck() == 1 )return 1;
Send_Byte((dat >> 8 ));
if( I2C_WaitAck() == 1 )return 2;
Send_Byte( dat & 0xff );
if( I2C_WaitAck() == 1 )return 3;
//Without timeout check, data can easily become garbled
do
{
//Timeout check
i++;
if( i > 20 ) return 4;
delay_ms(2);
IIC_Start();
Send_Byte((0X44 << 1) | 1 );//Read
}while(I2C_WaitAck() == 1); //End read command when temperature and humidity data is read
//Temperature high 8 bits
buff[0] = Read_Byte();
IIC_Send_Ack(0);
//Temperature low 8 bits
buff[1] = Read_Byte();
IIC_Send_Ack(0);
//Temperature CRC checksum value
buff[2] = Read_Byte();
IIC_Send_Ack(0);
//Humidity high 8 bits
buff[3] = Read_Byte();
IIC_Send_Ack(0);
//Humidity low 8 bits
buff[4] = Read_Byte();
IIC_Send_Ack(0);
//Humidity CRC checksum value
buff[5] = Read_Byte();
IIC_Send_Ack(1);
IIC_Stop();
//CRC check (substitute the values to check and verify the computed checksum matches the read checksum)
if( (crc8(buff,2) == buff[2]) && ( crc8(buff+3,2) == buff[5]) )
{
//Calculate temperature value
data_16 =(buff[0]<<8) | buff[1];
Temperature = (data_16/65535.0)*175.0 - 45;
//Calculate humidity value
data_16 = 0;
data_16 =(buff[3]<<8) | buff[4];
Humidity = (data_16/65535.0) * 100.0;
printf("temp = %.2f\r\n",Temperature);
printf("humi = %.2f\r\n",Humidity);
return 0;
}
else
{
printf("Check failure!\r\n");
}
return 5;
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
In the file bsp_sht30.h, write the following:
#ifndef _BSP_SHT30_H_
#define _BSP_SHT30_H_
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gpio.h"
extern double Temperature, Humidity;
//Port porting
#define SHT30_SCL_PIN 10
#define SHT30_SDA_PIN 40
//Set SDA output mode
#define SDA_OUT() {gpio_set_direction(SHT30_SDA_PIN, GPIO_MODE_OUTPUT);}
//Set SDA input mode
#define SDA_IN() {gpio_set_direction(SHT30_SDA_PIN, GPIO_MODE_INPUT);}
//Get SDA pin level change
#define SDA_GET() gpio_get_level(SHT30_SDA_PIN)
//SDA and SCL output
#define SDA(x) gpio_set_level(SHT30_SDA_PIN, x?1:0)
#define SCL(x) gpio_set_level(SHT30_SCL_PIN, x?1:0)
#define u8 unsigned char
void delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void SHT30_GPIO_Init(void);
char SHT30_Read(uint16_t dat);
#endif2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Porting Verification
In main.c, input the following code:
#include <stdio.h>
#include "bsp_sht30.h"
void app_main(void)
{
SHT30_GPIO_Init();
printf("SHT30 Start...\n");
while(1)
{
int i = SHT30_Read(0xe000);
delay_ms(1000);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
Power-on effect:
Driver code (adapted):
File Download
📌 Materials Download Center (click to jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.