2021-03-17 15:53:33 +00:00
|
|
|
// SGP30 Air Quality Sensor demo
|
|
|
|
// Initalises connection, retrieves unique chip ID, starts measurement.
|
|
|
|
// Call returns immediately so that air quality can be measured every second.
|
|
|
|
// After 30 seconds, resets the chip, and restarts measurement,
|
|
|
|
// but that call waits up to 20 seconds for the readings to start being useful.
|
|
|
|
// Reports status and results to Serial connection on GPIO 0 and 1.
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "pico/stdlib.h"
|
|
|
|
|
|
|
|
#include "breakout_sgp30.hpp"
|
|
|
|
|
|
|
|
using namespace pimoroni;
|
|
|
|
|
2021-05-14 18:12:37 +01:00
|
|
|
I2C i2c(BOARD::BREAKOUT_GARDEN);
|
|
|
|
BreakoutSGP30 sgp30(&i2c);
|
2021-03-17 15:53:33 +00:00
|
|
|
|
|
|
|
int main() {
|
|
|
|
uint8_t prd;
|
|
|
|
uint16_t eCO2, TVOC;
|
2021-05-17 16:20:42 +01:00
|
|
|
uint16_t rawCO2, rawTVOC;
|
2021-03-17 15:53:33 +00:00
|
|
|
|
2023-01-26 15:11:57 +00:00
|
|
|
#ifdef PICO_DEFAULT_LED_PIN
|
2021-03-17 15:53:33 +00:00
|
|
|
gpio_init(PICO_DEFAULT_LED_PIN);
|
|
|
|
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
|
2023-01-26 15:11:57 +00:00
|
|
|
#endif
|
2021-03-17 15:53:33 +00:00
|
|
|
|
|
|
|
stdio_init_all();
|
|
|
|
|
2021-05-05 12:21:57 +01:00
|
|
|
bool init_ok = sgp30.init();
|
|
|
|
if(init_ok) {
|
2021-03-17 15:53:33 +00:00
|
|
|
printf("SGP30 initialised - about to start measuring without waiting\n");
|
|
|
|
sgp30.start_measurement(false);
|
|
|
|
printf("Started measuring for id %012llx\n", sgp30.get_unique_id());
|
2021-05-05 12:21:57 +01:00
|
|
|
if(sgp30.get_air_quality(&eCO2, &TVOC)) {
|
2021-03-17 15:53:33 +00:00
|
|
|
prd = 1;
|
2021-05-05 12:21:57 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-03-17 15:53:33 +00:00
|
|
|
printf("SGP30 not found when reading air quality\n");
|
|
|
|
prd = 2;
|
|
|
|
}
|
2021-05-05 12:21:57 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-03-17 15:53:33 +00:00
|
|
|
printf("SGP30 not found when initialising\n");
|
|
|
|
prd = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t j = 0;
|
|
|
|
while(true) {
|
|
|
|
j++;
|
2023-01-26 15:11:57 +00:00
|
|
|
#ifdef PICO_DEFAULT_LED_PIN
|
2021-05-05 12:21:57 +01:00
|
|
|
for(uint8_t i=0; i<prd; i++) {
|
2021-03-17 15:53:33 +00:00
|
|
|
gpio_put(PICO_DEFAULT_LED_PIN, true);
|
|
|
|
sleep_ms(50);
|
|
|
|
gpio_put(PICO_DEFAULT_LED_PIN, false);
|
|
|
|
sleep_ms(50);
|
|
|
|
}
|
2023-01-26 15:11:57 +00:00
|
|
|
#endif
|
2021-05-05 12:21:57 +01:00
|
|
|
sleep_ms(1000 - (100 * prd));
|
|
|
|
if(prd == 1) {
|
2021-03-17 15:53:33 +00:00
|
|
|
sgp30.get_air_quality(&eCO2, &TVOC);
|
2021-05-17 16:20:42 +01:00
|
|
|
sgp30.get_air_quality_raw(&rawCO2, &rawTVOC);
|
|
|
|
printf("%3d: CO2 %d TVOC %d, raw %d %d\n", j, eCO2, TVOC, rawCO2, rawTVOC);
|
2021-05-05 12:21:57 +01:00
|
|
|
if(j == 30) {
|
2021-03-17 15:53:33 +00:00
|
|
|
printf("Resetting device\n");
|
|
|
|
sgp30.soft_reset();
|
|
|
|
sleep_ms(500);
|
|
|
|
printf("Restarting measurement, waiting 15 secs before returning\n");
|
|
|
|
sgp30.start_measurement(true);
|
|
|
|
printf("Measurement restarted, now read every second\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|