Zum Inhalt springen

Umweltsensor auslesen

Schaltungsaufbau

Bosch Sensor und Display

Programmcode

Für externen BME280-Sensor.

python
import time
import board
from adafruit_bme280 import basic as adafruit_bme280

# Create sensor object, using the board's default I2C bus.
i2c = board.I2C()  # uses board.SCL and board.SDA
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=118)

# change this to match the location's pressure (hPa) at sea level
bme280.sea_level_pressure = 1013.25

while True:
    print("Temperature: {:.1f} C".format(bme280.temperature))
    print("Humidity: {:.1f} %".format(bme280.humidity))
    print("Barometric pressure: {:.1f} hPa".format(bme280.pressure))
    print("Altitude: {:.2f} m".format(bme280.altitude))
    time.sleep(2)

Für den BMP280-Sensor auf dem Feather Sense.

python
import time
import board
import busio
import adafruit_bmp280

# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
# change this to match the location's pressure (hPa) at sea level
bmp280.sea_level_pressure = 1013.25

while True:
    print("Temperature: {:.1f} C".format(bmp280.temperature))
    print("Barometric pressure: {:.1f} hPa".format(bmp280.pressure))
    print("Altitude: {:.2f} m".format(bmp280.altitude))
    time.sleep(2)

Passende Aufgaben zu „Messen und Bewegen“

Hängst du fest? Probestunde anfragen

Inhalte von Serlo Education e.V., lizenziert unter CC BY-SA 4.0. Original auf Serlo ansehen ↗