Close

Code

A project log for Biotop

A mask that filters pollutants from the air in a biological way.

m-bindhammerM. Bindhammer 04/28/2023 at 12:240 Comments

The following code displays the gas sensor values on the AR glasses.

/* 
  Note: Because the sensor is warming up, 
  the first 10-20 readings will always be 
  TVOC 0 ppb and eCO2 400 ppm. 
 */

#include <Arduino.h>
#include <U8x8lib.h>
#include <Wire.h>
#include "Adafruit_SGP30.h"
Adafruit_SGP30 sgp1; // Define two classes
Adafruit_SGP30 sgp2;

U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);      

void setup(void)
{
  Serial.begin(9600);
  u8x8.begin();
  u8x8.setPowerSave(0);
  // Start I2C communication with the multiplexer
  Wire.begin();
  // Init sensor on bus number 0
  TCA9548A(0);
  sgp1.begin();
  // Init sensor on bus number 7
  TCA9548A(7);
  sgp2.begin();
}

void loop(void)
{
  printValues(sgp1, 0);
  printValues(sgp2, 7);
}

// Function to select I2C BUS
void TCA9548A(uint8_t bus)
{
  Wire.beginTransmission(0x70);  // TCA9548A address
  Wire.write(1 << bus);          // Send byte to select bus number
  Wire.endTransmission();
}

// Function to display sensor values on the OLED
void printValues(Adafruit_SGP30 sgp, int bus) 
{
  u8x8.setFont(u8x8_font_7x14B_1x2_r);
  TCA9548A (bus);
  Serial.println(bus);
  if(bus == 0) u8x8.drawString(2,10,"Inside: ");
  else u8x8.drawString(2,10,"Outside:");
  u8x8.drawString(2,20,"TVOC:");
  sgp.IAQmeasure();
  u8x8.setCursor(2,30);
  u8x8.print(sgp.TVOC);
  u8x8.print(" ppb");
  delay(3000);
  u8x8.drawString(2,20,"eCO2:");
  u8x8.setCursor(2,30);
  u8x8.print(sgp.eCO2);
  u8x8.print(" ppm");
  delay(3000);
}

Discussions