Temperatur und Luftfeuchesensor-Modul GY-21
I2C Adresse 0x40
Weiterführende Internetseiten
Technische Daten
Betriebsspannung | 3.3 bis 5VDC |
Stromverbrauch | 150yA |
Temperaturbereich | -40°C bis 85°C |
Temperaturgenauigkeit | +/- 1°C |
Feuchtigkeitsbereich | 0 bis 100% RH |
Feuchtigkeitsgenauigkeit | +/- 3% |
Komunikationsschnittstelle | I2C |
Abmessungen | 9 x 11 x 2 mm |
Verbindung des Moduls mit dem Arduino
Das Modul GY-21 Feuchte- und Temperatursensormodul wird wie folgt mit dem Arduino verbunden:
Uno PIN | Modul | Farbe |
5 V | VIN – Power supply | Rot |
GND | GND – Ground | Schwarz |
A4 | SDA – Serial Data | Grün |
A5 | SCL – Serial Clock | Blau |
Arduino Beispiel-Sketch
//-----------------------------------------------------|
// I2C GY-21 HTU21D |
// Temperatur und Feuchtesensor |
// Ohne zusätzliche / spezielle Bibliothek |
// |
// I2C Grundadresse = 0X40 |
// |
// HTU21D Kommandos |
// TRIGGER_TEMP_MEAS_H = 0xE3 |
// TRIGGER_HUM_MEAS_H = 0xE5 |
// TRIGGER_TEMP_MEAS_NH = 0xF3 |
// TRIGGER_HUM_MEAS_NH = 0xF5 |
// WRITE_USER_REG = 0xE6 |
// READ_USER_REG = 0xE7 |
// SOFT_RESET = 0xFE |
// |
//-----------------------------------------------------|
// Includes
//-----------------------------------------------------
#include <Wire.h> // Bibliothek für I2C
// Variablen
//-----------------------------------------------------
const int ADDRESS = 0x40;
double temperature;
double humidity;
//*****************************************************
// Sensor initialisieren
//*****************************************************
void sensor_init(const int addr) {
Wire.begin();
delay(100);
Wire.beginTransmission(addr);
Wire.endTransmission();
}
//****************************************
// Temperatur vom Sensor lesen
//****************************************
double read_temperature(const int addr) {
double temperature;
int low_byte;
int high_byte;
int raw_data;
// Temperaturmessung starten
Wire.beginTransmission(addr);
Wire.write(0xE3);
Wire.endTransmission();
// Daten lesen - Temperatur
Wire.requestFrom(addr, 2);
if (Wire .available() <= 2) {
high_byte = Wire.read();
low_byte = Wire.read();
high_byte = high_byte << 8;
raw_data = high_byte + low_byte;
}
temperature = (175.72 * raw_data) / 65536;
temperature = temperature - 46.85;
return temperature;
}
//****************************************
// Luftfeuchtigkeit vom Sensor lesen
//****************************************
double read_humidity(const int addr) {
double humidity;
double raw_data_1;
double raw_data_2;
int low_byte;
int high_byte;
int container;
//Luftfeuchtigkeit Messung starten
Wire.beginTransmission(addr);
Wire.write(0xE5);
Wire.endTransmission();
// Daten lesen - Luftfeuchtigkeit
Wire.requestFrom(addr, 2);
if(Wire.available() <= 2) {
high_byte = Wire.read();
container = high_byte / 100;
high_byte = high_byte % 100;
low_byte = Wire.read();
raw_data_1 = container * 25600;
raw_data_2 = high_byte * 256 + low_byte;
}
raw_data_1 = (125 * raw_data_1) / 65536;
raw_data_2 = (125 * raw_data_2) / 65536;
humidity = raw_data_1 + raw_data_2;
humidity = humidity - 6;
return humidity;
}
//****************************************
void setup() {
Serial.begin(9600);
sensor_init(ADDRESS);
}
//****************************************
void loop() {
temperature = read_temperature(ADDRESS);
humidity = read_humidity(ADDRESS);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("*C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
delay(1000);
}
Sending a command
Kommando | Code | Kommentar |
Start Messung Temperatur | 0xE3 | Hold master |
Start Messung Luftfeuchtigkeit | 0xE5 | Hold master |
Start Messung Temperatur | 0xF3 | No hold master |
Start Messung Luftfeuchtigkeit | 0xF5 | No hold master |
Schreibe in das Benutzerregister | 0xE6 | |
Lese das Benutzerregister | 0xF7 | |
Soft reset | 0xFE |
User Register
Bit | Beschreibung | Default |
7,0 | Auflösung Messung | 00 |
6 | Status: End of Battery | 0 |
3,4,5 | Reserve | 0 |
2 | Enable one-chip heater | 0 |
1 | Disable OTP reload | 1 |
Auflösung Messung Bit 7 und 0
Bit 7 | Bit 0 | Feuchte | Temperatur |
0 | 0 | 12 Bit | 14 Bit |
0 | 1 | 8 Bit | 12 Bit |
1 | 0 | 10 Bit | 13 Bit |
1 | 1 | 11 Bit | 11 Bit |