Environmental Sample

This sample demonstrates reading temperature, humidity, pressure, and altitude.

To run the sample:

  1. Copy the sample code to a folder on the Raspberry Pi.

  2. Open terminal, and then go to the folder you copied the sample.

  3. Run the sample with the following command:

python3 env.py

Sample Code

env.py
#!/usr/bin/python

#This sample demonstrates reading temperature, relative humidity, and air pressure data.
#Install Sensor uHAT library with "pip3 install turta-sensoruhat"

import time
from turta_sensoruhat import Turta_Env_280

#Variables
#Sea level pressure in bar
slp = 1000.0 #Update this from weather forecast to get precise altitude

#Initialize
env = Turta_Env_280.EnvironmentalSensor()

try:
    while True:
        #Hint: To get temperature, pressure and humidity readings at the same time,
        #call EnvironmentalSensor.read_tph() method.

        #Read & print temperature
        print("Temperature.....: " + str(round(env.read_temperature(), 1)) + "C")

        #Read & print humidity
        print("Humidity........: %" + str(round(env.read_humidity(), 1)) + "RH")

        #Read & print pressure
        print("Pressure........: " + str(round(env.read_pressure(), 1)) + "Pa")

        #Read & print altitude
        print("Altitude........: " + str(round(env.read_altitude(slp), 1)) + "m")

        #Rest a bit
        print("-----")
        time.sleep(2.0)

#Exit on CTRL+C
except KeyboardInterrupt:
    print('Bye.')

Result

When you run the sample, it prints the environmental data. The example output should be like this:

Temperature.....: 25.0
Humidity........: 50.0
Pressure........: 1000.0
Altitude........: 100.0
-----

To stop the Python script, just press CTRL + C.

Last updated