Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed C12832 I2CLCD TextLCD
Diff: Main.cpp
- Revision:
- 5:973401999337
- Parent:
- 4:67b457cac0ec
- Child:
- 6:5992fbd136ad
--- a/Main.cpp Wed Dec 11 11:48:52 2019 +0000
+++ b/Main.cpp Thu Dec 12 09:30:45 2019 +0000
@@ -5,10 +5,12 @@
#include "C12832.h"
C12832 lcd(p5, p7, p6, p8, p11); // set up instance of LCd screen
-I2C i2c(p28, p27); //I2c build into mbed.h, define pincs
+I2C i2c(p28, p27); //I2c built into mbed.h, define standard pins (data, clock)
+DigitalOut CO2Alarm (LED1);
+DigitalOut VOCAlarm (LED2);
-const int SGP30I2CAddress7 = 0x58; // 7-bit I2C address
-const int SGP30I2CAddress8 = 0x58 << 1; // 8-bit I2C address, shifted (0xA2)
+const int SGP30I2CAddress8 = 0x58 << 1; // 8-bit I2C address=0x58, shifted left (0xA2)
+
int main()
{
@@ -18,21 +20,41 @@
cmd[0] = 0x20; //address 0X0203 initialises sensor
cmd[1] = 0x03;
+ wait_ms(1); //give SGP30 time to stabilise after power up
i2c.write(SGP30I2CAddress8, cmd, 2); //2=two bytes of data
+ lcd.printf("Initiailising...");
while(1) {
wait(1.0f); //wait 1 second between reads
lcd.locate(0,0); //LCD cursor to top left
cmd[0] = 0x20;
cmd[1] = 0x08; //command 0X0208 reads six bits of air quality
- i2c.write(SGP30I2CAddress8, cmd, 2);
- wait(0.015); // MUST awiat 15mS after read request before read
- i2c.read(SGP30I2CAddress8, data, 6); //read six bytes from sensor into data array
- int CO2 = ((data[0]*256) + data[1]); //first two bits are CO2. Ignore checksums
- int VOC = ((data[3]*256) + data[4]); //bits 3 & 4 are VOC. Ignore checksums
- lcd.printf("SGP30 Gas Sensor \n\r"); //pretty heading
- lcd.printf("CO2:%d ppm VOC:%d ppb \n\r",CO2,VOC);
- lcd.printf("Raw: %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX", data[0], data[1], data[2], data[3], data[4], data[5]);
- //line above is for debugging only; it displays the six bytes read from sensor
+ if(i2c.write(SGP30I2CAddress8, cmd, 2)==false) {// check if I2C data was returned
+ wait_ms(15); // MUST awiat 15mS after read request before read
+ i2c.read(SGP30I2CAddress8, data, 6); //read six bytes from sensor into data array
+ int CO2 = ((data[0]*256) + data[1]); //first two bits are CO2. Ignore checksums
+ int VOC = ((data[3]*256) + data[4]); //bits 3 & 4 are VOC. Ignore checksums
+
+ if (CO2>1000) { //check if thresholds reached, sound alarm
+ CO2Alarm=true;
+ } else {
+ CO2Alarm=false;
+ }
+ if (VOC>500) {
+ VOCAlarm=true;
+ } else {
+ VOCAlarm=false;
+ }
+
+ lcd.printf("SGP30 Gas Sensor "); //pretty heading
+ lcd.locate(0,10);
+ lcd.printf("CO2:%d ppm VOC:%d ppb ",CO2,VOC);
+ lcd.locate(0,20);
+ lcd.printf("Data: %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX", data[0], data[1], data[2], data[3], data[4], data[5]);
+ //line above is for debugging only; it displays the six bytes read from sensor
+ } else { //if no I2C response
+ lcd.cls();
+ lcd.printf("Sensor offline - check connection.");
+ }
}
}