Combining Sensors

Combining Sensors#

It is often powerful to connect more than one sensor to the pico to log different conditions at the same time

Here we will give an example of how to combine a DHT 11 temperature sensor with the SPG30 VOC sensor

How to combine#

In general the way to combine sensors is to go through the code for each sensor and combine them into one. Key points to note are

  1. There should be only a single while True: loop

  2. Only import a given library once

  3. Check that no sensor is attempting to use the same pins, alter the code (and wiring) to use new pins

So, you would, for example take the DHT code:

import time
import board
import adafruit_dht

dht = adafruit_dht.DHT11(board.GP0)

while True:
    try:
        temp = dht.temperature
        humidity = dht.humidity
        print(f"Temperature: {temp} C \t Humidity: {humidity}%")
    except RuntimeError as e:
        print("Reading from DHT failure: ", e.args)
    time.sleep(5)

and then merge the SPG30 code into it:

# These imports are already in the DHT code - we don't need them again
import time
import board
# Need to take the next two lines to import the sgp30 library
import busio
import adafruit_sgp30

#The next four lines of code set up the connection to the SGP30 and should be taken
i2c = busio.I2C(board.GP21, board.GP20, frequency=100000)
sgp30 = adafruit_sgp30.Adafruit_SGP30(i2c)

sgp30.set_iaq_relative_humidity(celsius=__, relative_humidity=__) # Set values for your ambient temperature and humidity
sgp30.iaq_init()

elapsed_sec = 0

while True:
    # The next couple of lines get the readings from the sensor
    eCO2 = sgp30.eCO2
    TVOC = sgp30.TVOC
    print(f"eCO2 = {eCO2} ppm \t TVOC = {TVOC} ppb")
    #There is already a sleep function in the DHT loop - we don't need another
    time.sleep(1)
    elapsed_sec += 1
    if elapsed_sec > 10:
        elapsed_sec = 0
        print(
            "**** Baseline values: eCO2 = 0x%x, TVOC = 0x%x"
            % (sgp30.baseline_eCO2, sgp30.baseline_TVOC)
        )

Final Code#

Here is the final merged code (with some small tweaks)

import time
import board
import busio
import adafruit_dht
import adafruit_sgp30

# Set up DHT connection
dht = adafruit_dht.DHT11(board.GP0)

#Set up the SPG30 connection
i2c = busio.I2C(board.GP21, board.GP20, frequency=100000)
sgp30 = adafruit_sgp30.Adafruit_SGP30(i2c)

#sgp30.set_iaq_relative_humidity(celsius=__, relative_humidity=__) # Set values for your ambient temperature and humidity
sgp30.iaq_init()

elapsed_sec = 0
while True:
    try:
        # Get the temp and humidity
        temp = dht.temperature
        humidity = dht.humidity
        # Now get the VOC concentration
        eCO2 = sgp30.eCO2
        TVOC = sgp30.TVOC
        # Can print out results separately 
        #print(f"eCO2 = {eCO2} ppm \t TVOC = {TVOC} ppb")
        #print(f"Temperature: {temp} C \t Humidity: {humidity}%")
        # Or we can print out in a combined line -in this form Mu will plot it
        print(f"({TVOC},{temp},{humidity})")
        # In this form it is very easy to save to file and then read into a programme for analysis
        print(f"{TVOC},{temp},{humidity}")
    except RuntimeError as e:
        print("Reading from Sensors failure: ", e.args)
    time.sleep(5)