DHT11 example f
Dependencies: mbed C12832 I2CLCD TextLCD
Main.cpp@3:93078a6f3bad, 2019-12-11 (annotated)
- Committer:
- LaurenceW
- Date:
- Wed Dec 11 11:41:14 2019 +0000
- Revision:
- 3:93078a6f3bad
- Child:
- 4:67b457cac0ec
Basic SGP30 gas sensor program to display CO2 and VOC values on embedded LCD. As simple as it gets!
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 | 3:93078a6f3bad | 8 | I2C i2c(p28, p27); //I2c build into mbed.h, define pincs |
LaurenceW | 3:93078a6f3bad | 9 | |
LaurenceW | 3:93078a6f3bad | 10 | const int SGP30I2CAddress7 = 0x58; // 7-bit I2C address |
LaurenceW | 3:93078a6f3bad | 11 | const int SGP30I2CAddress8 = 0x58 << 1; // 8-bit I2C address, shifted (0xA2) |
LaurenceW | 3:93078a6f3bad | 12 | |
LaurenceW | 3:93078a6f3bad | 13 | int main() |
LaurenceW | 3:93078a6f3bad | 14 | { |
LaurenceW | 3:93078a6f3bad | 15 | char cmd[2]; //two byte array for 16 bit address/command |
LaurenceW | 3:93078a6f3bad | 16 | char data[6]; // six byte array for data |
LaurenceW | 3:93078a6f3bad | 17 | lcd.cls(); //clear LCD screen |
LaurenceW | 3:93078a6f3bad | 18 | |
LaurenceW | 3:93078a6f3bad | 19 | cmd[0] = 0x20; //address 0X0203 initialises sensor |
LaurenceW | 3:93078a6f3bad | 20 | cmd[1] = 0x03; |
LaurenceW | 3:93078a6f3bad | 21 | i2c.write(SGP30I2CAddress8, cmd, 2); //2=two bytes of data |
LaurenceW | 3:93078a6f3bad | 22 | |
LaurenceW | 3:93078a6f3bad | 23 | while(1) { |
LaurenceW | 3:93078a6f3bad | 24 | wait(1.0f); //wait 1 second btween reads |
LaurenceW | 3:93078a6f3bad | 25 | lcd.locate(0,0); //LCD cursor to top left |
LaurenceW | 3:93078a6f3bad | 26 | cmd[0] = 0x20; |
LaurenceW | 3:93078a6f3bad | 27 | cmd[1] = 0x08; //command 0X0208 reads six bits of air quality |
LaurenceW | 3:93078a6f3bad | 28 | i2c.write(SGP30I2CAddress8, cmd, 2); |
LaurenceW | 3:93078a6f3bad | 29 | wait(0.015); // MUST awiat 15mS after read request before read |
LaurenceW | 3:93078a6f3bad | 30 | i2c.read(SGP30I2CAddress8, data, 6); //read six bytes from sensor into data array |
LaurenceW | 3:93078a6f3bad | 31 | int CO2 = ((data[0]*256) + data[1]); //first two bits are CO2. Ignore checksums |
LaurenceW | 3:93078a6f3bad | 32 | int VOC = ((data[3]*256) + data[4]); //bits 3 & 4 are VOC. Ignore checksums |
LaurenceW | 3:93078a6f3bad | 33 | lcd.printf("SGP30 Gas Sensor \n\r"); //pretty heading |
LaurenceW | 3:93078a6f3bad | 34 | lcd.printf("CO2:%d ppm VOC:%d ppb \n\r",CO2,VOC); |
LaurenceW | 3:93078a6f3bad | 35 | lcd.printf("Raw: %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX", data[0], data[1], data[2], data[3], data[4], data[5]); |
LaurenceW | 3:93078a6f3bad | 36 | //line above is for debugging only; it displays the six bytes read from sensor |
LaurenceW | 3:93078a6f3bad | 37 | } |
LaurenceW | 3:93078a6f3bad | 38 | } |