The XY-MD01 SHT20 is a digital temperature and humidity sensor module that integrates the SHT20 sensor from Sensirion. It is designed for precise environmental monitoring, offering high accuracy and stability in measuring temperature and relative humidity. The module communicates using the I2C protocol, making it easy to interface with microcontrollers and development boards like the Arduino UNO.
Pin Name | Pin Number | Description |
---|---|---|
VCC | 1 | Power supply input (3.3V to 5.5V) |
GND | 2 | Ground |
SCL | 3 | I2C clock line |
SDA | 4 | I2C data line |
Below is an example of how to interface the XY-MD01 SHT20 with an Arduino UNO using the Wire library:
#include <Wire.h>
// I2C address of the SHT20 sensor
#define SHT20_ADDRESS 0x40
// Commands for temperature and humidity measurement
#define TRIGGER_TEMP_MEASURE 0xF3
#define TRIGGER_HUMD_MEASURE 0xF5
void setup() {
Serial.begin(9600); // Initialize serial communication
Wire.begin(); // Initialize I2C communication
Serial.println("XY-MD01 SHT20 Sensor Test");
}
void loop() {
float temperature = readTemperature();
float humidity = readHumidity();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
delay(2000); // Wait 2 seconds before the next reading
}
float readTemperature() {
Wire.beginTransmission(SHT20_ADDRESS);
Wire.write(TRIGGER_TEMP_MEASURE); // Send temperature measurement command
Wire.endTransmission();
delay(85); // Wait for measurement to complete
Wire.requestFrom(SHT20_ADDRESS, 2); // Request 2 bytes of data
if (Wire.available() == 2) {
uint16_t rawData = (Wire.read() << 8) | Wire.read();
return -46.85 + 175.72 * (rawData / 65536.0); // Convert to temperature
}
return NAN; // Return NaN if data is unavailable
}
float readHumidity() {
Wire.beginTransmission(SHT20_ADDRESS);
Wire.write(TRIGGER_HUMD_MEASURE); // Send humidity measurement command
Wire.endTransmission();
delay(29); // Wait for measurement to complete
Wire.requestFrom(SHT20_ADDRESS, 2); // Request 2 bytes of data
if (Wire.available() == 2) {
uint16_t rawData = (Wire.read() << 8) | Wire.read();
return -6.0 + 125.0 * (rawData / 65536.0); // Convert to humidity
}
return NAN; // Return NaN if data is unavailable
}
No Data Received from the Sensor:
Inaccurate Readings:
Arduino Freezes or Crashes:
Q: Can the XY-MD01 SHT20 operate at 5V?
A: Yes, the module supports an operating voltage range of 3.3V to 5.5V.
Q: Do I need external pull-up resistors for the I2C lines?
A: Some modules include built-in pull-up resistors, but if they are not present, you should add 4.7kΩ resistors to the SCL and SDA lines.
Q: How do I protect the sensor in harsh environments?
A: Use a protective enclosure or filter membrane to shield the sensor from dust, water, and corrosive gases.