KY-052 Pressure-sensor / Temperature-sensor (BMP280)

From SensorKit X40 Wiki

Revision as of 12:05, 6 March 2017 by Webmaster Wiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Picture

none

Technical data / Short description

This pressure sensor measures the air pressure at the sensor output ( small whole at the silver sensor case) and gives the result encoded to the I2C-bus.


You will need additional software for this sensor

Pinout

none

  • *You can connect this sensor with 5V systems and with 3,3V systems. Please pay attention that only one of the power supply pins is connected with the wished voltage - you can get additional informations at the example below: Connection Arduino (5V) and Connection Raspberry-Pi (3,3V).

Software example Arduino

This sensor does not sends its signal via output pin, it sends the signal via I2C-bus. With that you can control the sensor and start the pressure and temperature measurements and analyse the results.

It gives different ways to control the sensor, the best is the Adafruit_BMP085 library from the company Adafruit which you can find here: Link published under the OpenSource BSD-License BSD-License.

The example below is using this library - we advise to download it from Github, decompress it and copy it into the Arduino-library folder which could be found under the path  (C:\user\[username]\documents\arduino\libraries). After you have done it, the library is ready for the example below and following projects. Alternative you can find it in the download package below.

// Needed libraries will be imported and configured
#include 
#include 
#include 
Adafruit_BMP085_Unified BMPSensor = Adafruit_BMP085_Unified(10085);
 
void setup(void) 
{
  Serial.begin(9600);
  
  Serial.println("KY-052 SensorTest:");
  
  // If the sensor will not be detected, an error message will be shown
  if(!BMPSensor.begin())
  {
    Serial.println("KY-053-Sensor not detected!");
    Serial.print("Please check the connection");
    while(1);
  }
}
 
void loop(void) 
{
  // Initialisation of the Adafruit BMP library
  sensors_event_t event;
  BMPSensor.getEvent(&event);
 
  // If the sensor is ready, the measurement will start
  if (event.pressure)
  {
    Serial.println("------------------------");
    
    // Measurement of the air pressure
    Serial.print("Air pressure:    ");
    Serial.print(event.pressure);
    Serial.println(" hPa");
    
    // Measurement of the current temperature
    float temperature;
    BMPSensor.getTemperature(&temperature);
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.write(176); // UniCode-input for the "°-Symbol"
    Serial.println(" C");
 
    // Calculating of the altitude above the sea level,
    // from the recorded data (SLP=1013.25 hPa)
    float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
    Serial.print("Sea level:    "); 
    Serial.print(BMPSensor.pressureToAltitude(seaLevelPressure,
                                        event.pressure,
                                        temperature)); 
    Serial.println(" m");
    Serial.println("------------------------");
    Serial.println("");
  }
  // Error message if no sensor is detected
  else
  {
    Serial.println("Sensor-Error");
  }
  delay(1000);
}

Example program download:

KY-052-DruckSensor_TemperaturSensor.zip

Connections Arduino:

VCC = [Pin 5V]
GND = [Pin GND]
SCL = [Pin SCL]
SDA = [Pin SDA]
3,3 = [N.C.]

Software example Raspberry Pi

The program controlls the BMP180, which is included in this sensor module, with the BMP085/180 and I2C python libraries from the company Adafruit. You can find it at the Link where it is published under the MIT OpenSource-License.

It has to be installed first. To install it follow these steps: 


First you have to install the github software on your Raspberry-Pi

sudo apt-get install git

For this you have to make sure that the Raspberry Pi has a connection to the internet. You can downlaod and decompress the latest version of the Adafruit_BMP085 library with the command...

git clone https://github.com/adafruit/Adafruit_Python_BMP.git

After that we can jump into the downloaded folder with the command...

cd Adafruit_Python_BMP/

... and install the library with ....

sudo python setup.py install

After that you can use the library.

In order that the Raspberry Pi and the sensor can communicate via I2C-bus, you have to start the I2C-function of the Raspberry Pi first.

To do that you have to add the following line to the end of the file "/boot/config.txt:

    dtparam=i2c_arm=on

You can edit the file with the following command:


sudo nano /boot/config.txt

You can save and close the file with the key sequence [ctrl + x -> y -> enter].

Also you need additional libraries if you want to use I2C in with python. To install it, you have to enter the following command to the console:

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

After that you can use the following python code example. The program starts the measurement for air pressure, temperature and the altitude above sea level.

#!/usr/bin/python
# coding=utf-8
# Copyright (c) 2014 Adafruit Industries

# Needed libraries will be imported and configured 
import Adafruit_BMP.BMP085 as BMP085
import time

# You can set the break between the measurements here 
sleeptime = 1

try:
    # Sensor will be initialized
    BMPSensor = BMP085.BMP085()
# Check if the sensor is rightly connected
# If not an error message will be shown
except IOError:
    print("------------------------------")
    print ("KY-053 Sensor not detected!")
    print ("Check the connection")
    print("------------------------------")
    while(True):
        time.sleep(sleeptime)
except KeyboardInterrupt:
    GPIO.cleanup()


# Main program loop
# The program starts the measurement at the sensor and shows,
# the measured data at the console<br />try:
    while(1):
        print("------------------------------")
        # Temperatur
        print('Temperature = {0:0.2f}°C'.format(BMPSensor.read_temperature()))
        # Luftdruck
        print('Air pressure = {0:0.2f}hPa'.format(BMPSensor.read_pressure()/100))
        # Meereshöhe
        print('Sea level = {0:0.2f}m'.format(BMPSensor.read_altitude()))
        print("------------------------------")
        print("")
        time.sleep(sleeptime)

# Scavenging work after the end of the program
except KeyboardInterrupt:
    GPIO.cleanup()

Connections Raspberry Pi:

VCC = - [N.C]
GND = GND [Pin 06]
SCL = GPIO03 / SCL [Pin 05]
SDA = GPIO02 / SDA [Pin 03]
3.3 = 3,3V [Pin 01]

Example program downlaod 

KY-052-RPi_DruckSensor_TemperaturSensor.zip

To start with the command:

sudo python KY-052-RPi_DruckSensor_TemperaturSensor.py