HomeTurtlegraficsGPanelRoboticsGameGrid WebTigerPython
 Python - Online
i2csensoren
Deutsch   English   

12. I2C SENSORS


 

YOU LEARN HERE...

 

how to connect high-precision sensors to the micro:bit via the I2C interface and use them to measure air quality, temperature and humidity.

 

 

SGP30 AIR QUALITY SENSOR

 

The SGP30 air quality sensor provides highly accurate measurements of the CO2concentration in the air. When the CO2 concentration increases, the air in a room is no longer fresh, which can cause tiredness and increase the risk of virus transmission.

 


The SGP30 can be ordered, for example, from www.brack.ch or from www.mouser.ch . The connection cable is included.

 
 


With very little effort, you can set up a simple measuring station to monitor the CO2concentration in your classroom.

 

.

 


The sensor is connected to the micro:bit using the supplied I2C cable, for example via an I2C-Hub. The I2C hub can be ordered from TJGroup for CHF 6.00. Shipping costs CHF 1.00 within Switzerland and CHF 3.00 outside Switzerland. The hub also allows you to connect other sensors to the micro:bit.

To place an order, send an email to the following address:

 

 

EXAMPLES

 


EXAMPLE 1: Measuring and displaying the CO2 concentration

In your program, you first need to define how the measured values from the SGP30 sensor are read via I2C (sensor specification).
The function getValues() returns two values:
  • CO2 concentration in ppm*
  • TVOC Total Volantile Organic Componds*

Both values are displayed in the output window using print(). The command sleep(500)specifies the measurement interval.

 

Program:

from microbit import *

# sgp---------------------
raw = [0, 0]
buf = [0,0,0,0,0,0]
i2c.write(0x58, bytearray([0x20,0x03]))
sleep(1500) 

def getValues(a=0x58):
    i2c.write(0x58, bytearray([0x20,0x08]))
    sleep(600)
    block = i2c.read(0x58, 6)
    buf = [0,0,0,0,0,0]
    for i in range(0,6):
            buf[i]=block[i]
    for i in range(0,2):
            raw[i]= buf[i*3]
            raw[i]<<=8
            raw[i]|=buf[i*3+1]
    return (raw[0], raw[1])
#------------------------------

while True:
    co2, voc = getValues()
    print ("CO2 = ", co2, "  TVOC = ", voc)   
    sleep(500)
► Copy to clipboard

After the program starts, the sensor first goes through an initialisation phase and returns a CO2 value of 400 during the first 10 seconds. After that, the measured CO2 values are displayed.

The CO2 concentration in the air is expressed in parts per million (ppm). The SGP30 sensor returns values between 400 and 60,000 ppm. At values above 1000 ppm, the air is no longer considered fresh.

Indoor environments contain many sources of pollutants, such as lamps, floor coverings and cleaning products. The sensor returns TVOC values between 0 and 60,000.


 


Example 2: Sensirion SHT85 temperature and humidity sensor

 

Sensirion, based in Stäfa, Switzerland, is one of the leading manufacturers of digital microsensors. The SHT85 measures temperatures from -40°C to +105°C and relative humidity from 0% to 100%. Both values are provided with four decimal places.

To connect the sensor to the micro:bit, you can use the SHT kit. It consists of:

  • mbHub
  • Senso rlink
  • SHT85
  • Connection cable
 

The kit can be ordered from TJGroup for CHF 19.00. Shipping costs CHF 1.00 within Switzerland and CHF 3.00 outside Switzerland. The kit also allows you to connect other sensors and electronic components to the micro:bit.

To place an order, send an email to the following address


In your program, you first define how the sensor data is read via I2C. The function getValues()returns two values:
  • Temperature in °C
  • Relative humidity in %

Both values are displayed in the output window using print(). The command sleep(500) specifies the measurement interval.

 

Program:

from microbit import *

# sht-------------------------
def getValues(a = 0x44):
    i2c.write(a, b'\x2c\x06')
    raw = i2c.read(a, 6)
    t, h = (raw[0] << 8) + raw[1], (raw[3] << 8) + raw[4]
    return -45 + (175 * (t / 65535)), 100 * (h / 65535)
#-----------------------------

while True:
   temp, humi = getValues()
   print(temp, humi)
   sleep(500)
► Copy to clipboard

 

 

REMEMBER...

 

The SGP30 sensor provides CO2-equivalent (eCO2) values in ppm (parts per million), ranging from 400 to 60,000. Values below 1000 indicate good air quality, while values above 1400 suggest that the room should be ventilated immediately. Poor ventilation can increase the risk of airborne virus transmission.

The SHT85 sensor measures temperature in °C and relative humidity in %, returning values with four decimal places.

Since these external sensors are not directly supported by the microbit module, you need to include the corresponding program block #sgp-------- or #sht------- in each application.

The sensors are connected to the micro:bit via a hub with several I2C connectors. The mbHub and SensorLink are designed so that they can also be used to connect additional I2C sensors and electronic components.

 

 

EXERCISES

 

 


1.


Build a CO2 measuring station
Build a device that measures the CO2 concentration in your classroom.

Use the following thresholds:

  • Below 1000 ppm: The air is fresh.
  • 1000–1400 ppm: Ventilate the room soon.
  • Above 1400 ppm: Open the windows.

You can use the LEDs on your micro:bit to display different symbols for these three ranges.

The program remains stored on the micro:bit. You can therefore disconnect it from the computer and connect it to another power source, such as a power bank.

 
 
2.

Temperature alarm
Build a temperature monitoring system that plays an audible signal when the temperature has increased by more than 2°C.

 

3.

Compare temperature sensors
Measure the temperature simultaneously using the micro:bit's built-in temperature sensor and the external SHT85 sensor. Compare the results.