KY-052

This pressure sensor measures the air pressure at the sensor output (small hole on the silver sensor housing) and outputs the result coded on the I2C bus.

A corresponding library is required for this module - see code examples below.

Technical specifications

Measuring range (pressure) 300 to 1100 hPa (equivalent to +9000 to -500 m above / below sea level)
Relative accuracy ± 0.12 hPa, equiv. To ± 1 m (950 to 1050 hPa at 25 ° C)
Absolute accuracy ± 1 hPa (950 to 1050 hPa, 0 to +40 ° C)
Temperature coefficient offset 1.5 Pa / K, corresponds to 12.6 cm / K (25 to 40 ° C at 900hPa)
Digital interfaces I²C (up to 3.4 MHz), SPI (3- and 4-wire, up to 10 MHz)
Power consumption 2.7µA at 1 Hz sampling rate
Measuring range (temperature) -40 to +85 ° C

Pin assignment

  • This sensor allows it to be connected to and operated on both 5V and 3.3V systems. Please note that only one of the respective power supply pins is connected; suitable for the voltage level of the system used - see the examples below for connecting the Arduino (5V) or the Rasperry Pi (3.3V)

Code example Arduino

Pin assignment Arduino

Arduino Sensor
5V SDO
5V COD
Pin A4 SDA
Pin A5 SCL
5V + V
Mass GND

This sensor does not output its measurement result as a signal on an output pin, but communicates it via the I2C bus. This can be used to control the sensor and to start and evaluate the respective measurements for pressure and temperature.

There are several ways to control this sensor module - the Adafruit_BMP280 library has proven to be particularly accessible, which is provided by Adafruit published under the OpenSource BSD license.

The example below uses this library - we recommend downloading it from Github, unpacking it and placing it in the Arduino library folder, which is located by default under (C: \ Users \ [username] \ Documents \ Arduino \ libraries), so that it is available for this code example and the following projects. Alternatively, this is also included in the download package below.

/ ************************************************* **************************
  This is a library for the BMP280 humidity, temperature & pressure sensor
 
  Designed specifically to work with the Adafruit BMEP280 Breakout
  ----> http://www.adafruit.com/products/2651
 
  These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  to interface.
 
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing products
  from Adafruit!
 
  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ************************************************** ************************* /
 
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
 
 
#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11
#define BMP_CS 10
 
Adafruit_BMP280 bmp; // I2C
// Adafruit_BMP280 bmp (BMP_CS); // hardware SPI
// Adafruit_BMP280 bmp (BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
 
void setup () {
  Serial.begin (9600);
  Serial.println (F ("BMP280 test"));
   
  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 ("Approx altitude ="));
    Serial.print (bmp.readAltitude (1013.25)); // this should be adjusted to your local forcase
    Serial.println ("m");
     
    Serial.println ();
    delay (2000);
}

Sample program download

KY052-Arduino.zip

Code example Raspberry Pi

Pin assignment Raspberry Pi

Raspberry Pi Sensor
3.3V SDO
3.3V COD
GPIO 2 / SDA [Pin 3] SDA
GPIO 3 / SCL [Pin 5] SCL
3.3V + V
Mass GND

The program uses the corresponding BMP280 Python Librarie to control the BMP280, which is installed on this sensor module of the Adafruit company. This was published under the WITH OpenSource license .

This must first be installed. To do this, proceed as follows:

First the library from Adafruit must be installed:

sudo pip3 install adafruit-circuitpython-bmp280

In order for the Raspberry Pi to communicate with the sensor on the I2C bus, the I2C function must also be enabled on the Raspberry Pi in advance. To do this, the following lines must be added to the end of the "/boot/config.txt" file:

dtparam=i2c_arm=on

The file can be edited with the following command:

sudo nano /boot/config.txt

With the key sequence [Ctrl+X -> Y -> Enter] the file, after adding the line at the bottom, can be saved and closed.

Furthermore additional libraries are needed to use I2C within Python. To install them the following command has to be entered into the console:

sudo apt-get install python-smbus i2c-tools -y

After this, the following Python code example can be used. The program starts the measurement at the sensor and outputs the measured values for air pressure, temperature and height above sea level.

# Required modules are imported and set up
import time
import board
# import digitalio # for use with SPI
import adafruit_bmp280

# Create a sensor object that communicates via the RPi's standard I2C bus
i2c = board.I2C () # uses board.SCL and board.SDA
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C (i2c)

# OR create a sensor object that communicates via the RPi's standard SPI bus
# spi = board.SPI ()
# bmp_cs = digitalio.DigitalInOut (board.D10)
# bmp280 = adafruit_bmp280.Adafruit_BMP280_SPI (spi, bmp_cs)

# Change this value so that it corresponds to the air pressure (hPa) at sea level at your location.
bmp280.sea_level_pressure = 1013.25

while True:
    print ("\ nTemperature:% 0.1f C"% bmp280.temperature)
    print ("Pressure:% 0.1f hPa"% bmp280.pressure)
    print ("Altitude =% 0.2f meters"% bmp280.altitude)
    time.sleep (2)

Sample program download

KY052-RPi.zip

To start with the command

sudo python3 KY052-RPi.py

Code example Micro: Bit

Pin assignment Micro: Bit:

Micro: bit Sensor
Pin 20 SDA
Pin 19 SCL
3v COD
3v SDO
3V + V
Mass GND

This is a MakeCode example for Micro: Bit which essentially does the same as the examples of the other two variants. However, this example is closer to the example of the Raspberry Pi than the example of the Arduino.

An additional library is required for the following code example:

BMP280 by makecode-extensions | published under the MIT License

You have to add this library to your IDE before using the code.

You do this by going to Extensions in your IDE and looking for the library with the following URL ** https: //github.com/makecode-extensions/BMP280.git** and then adding it.

Sample program download

microbit-KY-052.zip