DHT11 example f
Dependencies: mbed C12832 I2CLCD TextLCD
Main.cpp@5:973401999337, 2019-12-12 (annotated)
- Committer:
- LaurenceW
- Date:
- Thu Dec 12 09:30:45 2019 +0000
- Revision:
- 5:973401999337
- Parent:
- 4:67b457cac0ec
SGP30 Hello World
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LaurenceW | 3:93078a6f3bad | 1 | //SGP30 Air Quality sensor |
LaurenceW | 3:93078a6f3bad | 2 | //Laurence Wilkins CU Coventry University |
LaurenceW | 3:93078a6f3bad | 3 | |
LaurenceW | 3:93078a6f3bad | 4 | #include "mbed.h" |
LaurenceW | 3:93078a6f3bad | 5 | #include "C12832.h" |
LaurenceW | 3:93078a6f3bad | 6 | |
LaurenceW | 3:93078a6f3bad | 7 | C12832 lcd(p5, p7, p6, p8, p11); // set up instance of LCd screen |
LaurenceW | 5:973401999337 | 8 | I2C i2c(p28, p27); //I2c built into mbed.h, define standard pins (data, clock) |
LaurenceW | 5:973401999337 | 9 | DigitalOut CO2Alarm (LED1); |
LaurenceW | 5:973401999337 | 10 | DigitalOut VOCAlarm (LED2); |
LaurenceW | 3:93078a6f3bad | 11 | |
LaurenceW | 5:973401999337 | 12 | const int SGP30I2CAddress8 = 0x58 << 1; // 8-bit I2C address=0x58, shifted left (0xA2) |
LaurenceW | 5:973401999337 | 13 | |
LaurenceW | 3:93078a6f3bad | 14 | |
LaurenceW | 3:93078a6f3bad | 15 | int main() |
LaurenceW | 3:93078a6f3bad | 16 | { |
LaurenceW | 3:93078a6f3bad | 17 | char cmd[2]; //two byte array for 16 bit address/command |
LaurenceW | 3:93078a6f3bad | 18 | char data[6]; // six byte array for data |
LaurenceW | 3:93078a6f3bad | 19 | lcd.cls(); //clear LCD screen |
LaurenceW | 3:93078a6f3bad | 20 | |
LaurenceW | 3:93078a6f3bad | 21 | cmd[0] = 0x20; //address 0X0203 initialises sensor |
LaurenceW | 3:93078a6f3bad | 22 | cmd[1] = 0x03; |
LaurenceW | 5:973401999337 | 23 | wait_ms(1); //give SGP30 time to stabilise after power up |
LaurenceW | 3:93078a6f3bad | 24 | i2c.write(SGP30I2CAddress8, cmd, 2); //2=two bytes of data |
LaurenceW | 5:973401999337 | 25 | lcd.printf("Initiailising..."); |
LaurenceW | 3:93078a6f3bad | 26 | |
LaurenceW | 3:93078a6f3bad | 27 | while(1) { |
LaurenceW | 4:67b457cac0ec | 28 | wait(1.0f); //wait 1 second between reads |
LaurenceW | 3:93078a6f3bad | 29 | lcd.locate(0,0); //LCD cursor to top left |
LaurenceW | 3:93078a6f3bad | 30 | cmd[0] = 0x20; |
LaurenceW | 3:93078a6f3bad | 31 | cmd[1] = 0x08; //command 0X0208 reads six bits of air quality |
LaurenceW | 5:973401999337 | 32 | if(i2c.write(SGP30I2CAddress8, cmd, 2)==false) {// check if I2C data was returned |
LaurenceW | 5:973401999337 | 33 | wait_ms(15); // MUST awiat 15mS after read request before read |
LaurenceW | 5:973401999337 | 34 | i2c.read(SGP30I2CAddress8, data, 6); //read six bytes from sensor into data array |
LaurenceW | 5:973401999337 | 35 | int CO2 = ((data[0]*256) + data[1]); //first two bits are CO2. Ignore checksums |
LaurenceW | 5:973401999337 | 36 | int VOC = ((data[3]*256) + data[4]); //bits 3 & 4 are VOC. Ignore checksums |
LaurenceW | 5:973401999337 | 37 | |
LaurenceW | 5:973401999337 | 38 | if (CO2>1000) { //check if thresholds reached, sound alarm |
LaurenceW | 5:973401999337 | 39 | CO2Alarm=true; |
LaurenceW | 5:973401999337 | 40 | } else { |
LaurenceW | 5:973401999337 | 41 | CO2Alarm=false; |
LaurenceW | 5:973401999337 | 42 | } |
LaurenceW | 5:973401999337 | 43 | if (VOC>500) { |
LaurenceW | 5:973401999337 | 44 | VOCAlarm=true; |
LaurenceW | 5:973401999337 | 45 | } else { |
LaurenceW | 5:973401999337 | 46 | VOCAlarm=false; |
LaurenceW | 5:973401999337 | 47 | } |
LaurenceW | 5:973401999337 | 48 | |
LaurenceW | 5:973401999337 | 49 | lcd.printf("SGP30 Gas Sensor "); //pretty heading |
LaurenceW | 5:973401999337 | 50 | lcd.locate(0,10); |
LaurenceW | 5:973401999337 | 51 | lcd.printf("CO2:%d ppm VOC:%d ppb ",CO2,VOC); |
LaurenceW | 5:973401999337 | 52 | lcd.locate(0,20); |
LaurenceW | 5:973401999337 | 53 | lcd.printf("Data: %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX", data[0], data[1], data[2], data[3], data[4], data[5]); |
LaurenceW | 5:973401999337 | 54 | //line above is for debugging only; it displays the six bytes read from sensor |
LaurenceW | 5:973401999337 | 55 | } else { //if no I2C response |
LaurenceW | 5:973401999337 | 56 | lcd.cls(); |
LaurenceW | 5:973401999337 | 57 | lcd.printf("Sensor offline - check connection."); |
LaurenceW | 5:973401999337 | 58 | } |
LaurenceW | 3:93078a6f3bad | 59 | } |
LaurenceW | 3:93078a6f3bad | 60 | } |