ESP32 KY-052 Pressure and Temperature Sensor Module
Overview
The KY-052 module features the BMP280 sensor, offering accurate measurements of barometric pressure and temperature. It utilizes the I2C interface for communication, making it ideal for environmental monitoring projects.
About KY-052 Pressure and Temperature Sensor Module
The KY-052 module integrates the BMP280 sensor, capable of measuring both barometric pressure and temperature. It communicates via the I2C bus, providing precise environmental data suitable for applications like weather stations, altimeters, and mobile devices.
Technical Specifications
Pinout Configuration
The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32.
VCC:
Power supply pin, can be connected to 3.3V or 5V.GND:
Ground pin.SDA:
I2C data line.SCL:
I2C clock line.SDO:
Not connected in I2C mode; used for SPI communication.CSB:
Chip select for SPI; connect to VCC for I2C mode.
Wiring with ESP32
VCC:
Connect to 3.3V or 5V on the microcontroller.GND:
Connect to the ground of the microcontroller.SDA:
Connect to the SDA pin of the microcontroller (e.g., GPIO21 on ESP32).SCL:
Connect to the SCL pin of the microcontroller (e.g., GPIO22 on ESP32).SDO:
Leave unconnected for I2C communication.CSB:
Connect to VCC to select I2C mode.
Troubleshooting Guide
Common Issues
❌ Sensor Not Responding
Issue: The sensor does not provide any data.
Solutions:
- Ensure proper wiring connections, especially the I2C lines (SDA and SCL).
- Verify that the CSB pin is connected to VCC to enable I2C mode.
- Check that the correct I2C address is used in the code (default is 0x76 or 0x77).
🌡️ Incorrect Temperature or Pressure Readings
Issue: The sensor outputs inaccurate environmental data.
Solutions:
- Ensure the sensor is not exposed to rapid temperature changes or drafts.
- Calibrate the sensor if necessary, especially for altitude measurements.
- Verify that the sensor is operating within its specified temperature range (-40°C to +85°C).
Debugging Tips
🔍 Serial Monitor
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
⚡ Voltage Checks
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
Code Examples
Arduino Example
#include <Wire.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp;
void setup() {
Serial.begin(9600);
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
}
}
void loop() {
Serial.print(F("Temperature = "));
Serial.print(bmp.readTemperature());
Serial.println(" °C");
Serial.print(F("Pressure = "));
Serial.print(bmp.readPressure());
Serial.println(" Pa");
Serial.print(F("Altitude = "));
Serial.print(bmp.readAltitude(1013.25)); // Adjust to local sea level pressure
Serial.println(" m");
delay(2000);
}
This Arduino code initializes the BMP280 sensor and reads temperature, pressure, and altitude data. The altitude calculation requires adjusting the sea level pressure value (1013.25 hPa) to your local value for accurate readings. The sensor data is printed to the serial monitor every 2 seconds.
ESP-IDF Example
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c.h"
#include "bmp280.h"
#define I2C_MASTER_SCL_IO 22
#define I2C_MASTER_SDA_IO 21
#define I2C_MASTER_NUM I2C_NUM_0
#define I2C_MASTER_FREQ_HZ 100000
void app_main() {
printf("Initializing BMP280 sensor...\n");
bmp280_t bmp;
bmp280_params_t params;
bmp280_init_default_params(¶ms);
i2cdev_init();
if (bmp280_init_desc(&bmp, BMP280_I2C_ADDRESS_0, I2C_MASTER_NUM, I2C_MASTER_SDA_IO, I2C_MASTER_SCL_IO) != ESP_OK ||
bmp280_init(&bmp, ¶ms) != ESP_OK) {
printf("BMP280 sensor initialization failed!\n");
return;
}
while (1) {
float temperature, pressure, altitude;
if (bmp280_read_float(&bmp, &temperature, &pressure, &altitude) == ESP_OK) {
printf("Temperature: %.2f°C, Pressure: %.2f hPa, Altitude: %.2f m\n", temperature, pressure / 100.0, altitude);
} else {
printf("Failed to read data from BMP280\n");
}
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
This ESP-IDF code initializes the BMP280 sensor using the I2C interface on GPIO21 (SDA) and GPIO22 (SCL). It reads temperature, pressure, and altitude values and prints them to the console every 2 seconds.
ESPHome Example
sensor:
- platform: bmp280
temperature:
name: "KY-052 Temperature"
pressure:
name: "KY-052 Pressure"
address: 0x76
update_interval: 2s
This ESPHome configuration sets up the KY-052 sensor (BMP280) to read temperature and pressure data via I2C with an update interval of 2 seconds.
PlatformIO Example
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
adafruit/Adafruit BMP280 Library
adafruit/Adafruit Unified Sensor
PlatformIO Example Code
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp;
void setup() {
Serial.begin(115200);
if (!bmp.begin(0x76)) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
}
void loop() {
Serial.print("Temperature = ");
Serial.print(bmp.readTemperature());
Serial.println(" °C");
Serial.print("Pressure = ");
Serial.print(bmp.readPressure() / 100.0);
Serial.println(" hPa");
Serial.print("Altitude = ");
Serial.print(bmp.readAltitude(1013.25)); // Adjust sea level pressure
Serial.println(" m");
delay(2000);
}
This PlatformIO code configures the KY-052 sensor (BMP280) for temperature, pressure, and altitude measurement using I2C communication. It reads data every 2 seconds and prints it to the serial monitor.
MicroPython Example
from machine import Pin, I2C
import time
import bmp280
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
sensor = bmp280.BMP280(i2c)
while True:
temperature = sensor.temperature
pressure = sensor.pressure
altitude = sensor.altitude
print("Temperature:", temperature, "°C", "Pressure:", pressure, "hPa", "Altitude:", altitude, "m")
time.sleep(2)
This MicroPython script configures the KY-052 sensor (BMP280) using I2C on GPIO21 (SDA) and GPIO22 (SCL). It continuously reads temperature, pressure, and altitude and prints the values every 2 seconds.
Conclusion
The ESP32 KY-052 Pressure and Temperature Sensor Module is a powerful KY-0xx module sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.