-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino-M5StickC-HTU21DF.ino
87 lines (75 loc) · 2.3 KB
/
Arduino-M5StickC-HTU21DF.ino
1
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
#include <M5StickC.h>
#define I2C_ADR_HTU21D 0x40
#define reset 0xFE
#define CMD_MESURE_TEMP 0xE3
#define CMD_MESURE_HUMID 0xE5
byte readbuffer[6];
int humid = 0;
int humidBlack = 0;
float dispTemp = 0.0;
void setup()
{
Serial.begin(115200);
Serial.println("\nStart");
Wire.begin(0, 26, 10000); //SDA(G0), SCL(G26)
Wire.beginTransmission(I2C_ADR_HTU21D);
Wire.write(reset);
Wire.endTransmission();
delay(100);
M5.begin();
M5.Axp.ScreenBreath(8);
M5.Lcd.setRotation(1);
M5.Lcd.fillScreen(TFT_BLACK);
// frame(humidity progress bar)
M5.Lcd.drawLine(7, 28, 7, 73, WHITE);
M5.Lcd.drawLine(7, 73, 38, 73, WHITE);
M5.Lcd.drawLine(38, 73, 38, 28, WHITE);
M5.Lcd.drawLine(7, 28, 38, 28, WHITE);
}
void loop()
{
Wire.beginTransmission(I2C_ADR_HTU21D);
Wire.write(CMD_MESURE_TEMP);
Wire.endTransmission();
delay(100);
Wire.requestFrom(I2C_ADR_HTU21D,3);
readbuffer[0] = Wire.read();
readbuffer[1] = Wire.read();
readbuffer[2] = Wire.read();
int Temp0 = ( readbuffer[0] << 8 | readbuffer[1] ) ;
float Temp = -46.85 + 175.72 * Temp0 * 0.00001525878L;
Serial.print("Temp ");Serial.println(Temp,1);
Wire.beginTransmission(I2C_ADR_HTU21D);
Wire.write(CMD_MESURE_HUMID);
Wire.endTransmission();
delay(100);
Wire.requestFrom(I2C_ADR_HTU21D,3);
readbuffer[0] = Wire.read();
readbuffer[1] = Wire.read();
readbuffer[2] = Wire.read();
uint16_t RH0 = readbuffer[0] << 8 | readbuffer[1] ;
float RH = -6 + 125.0 * RH0 * 0.00001525878L;
Serial.print(" ");Serial.print(RH,0);Serial.println(" RH");
Serial.println("--- ");
dispTemp = Temp; //temperature
M5.Lcd.fillRect(40, 30, 125, 50, BLACK);
M5.Lcd.setCursor(50, 30, 1);
M5.Lcd.setTextFont(4);
if(dispTemp>0){
M5.Lcd.printf("+%.1f 'C\r\n",dispTemp);
}else if(dispTemp<0){
M5.Lcd.printf("-%.1f 'C\r\n",dispTemp);
}else{
M5.Lcd.println(" 0.0 'C\r\n");
}
humid = (int)(RH);
humidBlack = map((100-humid),0, 100, 0,50);
//humidity[%]
M5.Lcd.setCursor(6, 0, 2);
M5.Lcd.setTextFont(2);
M5.Lcd.printf("%3d%%\r\n",humid);
//humidity progress bar
M5.Lcd.fillRect(8, 29, 30, 44, GREEN);
M5.Lcd.fillRect(8, 29, 30, humidBlack - 6, BLACK);
delay(2000);
}