DHT11 example f

Dependencies:   mbed C12832 I2CLCD TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Main.cpp Source File

Main.cpp

00001 //SGP30 Air Quality sensor
00002 //Laurence Wilkins CU Coventry University
00003 
00004 #include "mbed.h"
00005 #include "C12832.h"
00006 
00007 C12832 lcd(p5, p7, p6, p8, p11);                    // set up instance of LCd screen
00008 I2C i2c(p28, p27);                                  //I2c built into mbed.h, define standard pins (data, clock)
00009 DigitalOut CO2Alarm (LED1);
00010 DigitalOut VOCAlarm (LED2);
00011 
00012 const int SGP30I2CAddress8 = 0x58 << 1;             // 8-bit I2C address=0x58, shifted left (0xA2)
00013 
00014 
00015 int main()
00016 {
00017     char cmd[2];                                    //two byte array for 16 bit address/command
00018     char data[6];                                   // six byte array for data
00019     lcd.cls();                                      //clear LCD screen
00020 
00021     cmd[0] = 0x20;                                  //address 0X0203 initialises sensor
00022     cmd[1] = 0x03;
00023     wait_ms(1);                                     //give SGP30 time to stabilise after power up
00024     i2c.write(SGP30I2CAddress8, cmd, 2);            //2=two bytes of data
00025     lcd.printf("Initiailising...");
00026 
00027     while(1) {
00028         wait(1.0f);                                 //wait 1 second between reads
00029         lcd.locate(0,0);                            //LCD cursor to top left
00030         cmd[0] = 0x20;
00031         cmd[1] = 0x08;                              //command 0X0208 reads six bits of air quality
00032         if(i2c.write(SGP30I2CAddress8, cmd, 2)==false) {// check if I2C data was returned
00033             wait_ms(15);                                // MUST awiat 15mS after read request before read
00034             i2c.read(SGP30I2CAddress8, data, 6);        //read six bytes from sensor into  data array
00035             int CO2 = ((data[0]*256) + data[1]);        //first two bits are CO2. Ignore checksums
00036             int VOC = ((data[3]*256) + data[4]);        //bits 3 & 4 are VOC. Ignore checksums
00037 
00038             if (CO2>1000) {                             //check if thresholds reached, sound alarm
00039                 CO2Alarm=true;
00040             } else {
00041                 CO2Alarm=false;
00042             }
00043             if (VOC>500) {
00044                 VOCAlarm=true;
00045             } else {
00046                 VOCAlarm=false;
00047             }
00048 
00049             lcd.printf("SGP30 Gas Sensor    ");        //pretty heading
00050             lcd.locate(0,10);
00051             lcd.printf("CO2:%d ppm VOC:%d ppb   ",CO2,VOC);
00052             lcd.locate(0,20);
00053             lcd.printf("Data: %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX", data[0], data[1], data[2], data[3], data[4], data[5]);
00054             //line above is for debugging only; it displays the six bytes read from sensor
00055         } else {                                        //if no I2C response
00056             lcd.cls();
00057             lcd.printf("Sensor offline - check connection.");
00058         }
00059     }
00060 }