Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**************************************************************************
- This is an example for our Monochrome OLEDs based on SSD1306 drivers
- Pick one up today in the adafruit shop!
- ------> http://www.adafruit.com/category/63_98
- This example is for a 128x64 pixel display using I2C to communicate
- 3 pins are required to interface (two I2C and one reset).
- Adafruit invests time and resources providing this open
- source code, please support Adafruit and open-source
- hardware by purchasing products from Adafruit!
- Written by Limor Fried/Ladyada for Adafruit Industries,
- with contributions from the open source community.
- BSD license, check license.txt for more information
- All text above, and the splash screen below must be
- included in any redistribution.
- **************************************************************************/
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include "Adafruit_SGP30.h"
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
- #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
- #define SCREEN_ADDRESS 0x3C
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- // Declaration for an SGP30 display connected to I2C (SDA, SCL pins)
- Adafruit_SGP30 sgp;
- int counter = 0;
- int fanPin = 9;
- void setup() {
- Serial.begin(9600);
- pinMode(fanPin, OUTPUT);
- // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
- if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
- Serial.println(F("SSD1306 allocation failed"));
- for(;;); // Don't proceed, loop forever
- }
- while (!Serial) { delay(10); } // Wait for serial console to open!
- Serial.println("SGP30 test");
- if (! sgp.begin()){
- Serial.println("Sensor not found :(");
- while (1);
- }
- Serial.print("Found SGP30 serial #");
- Serial.print(sgp.serialnumber[0], HEX);
- Serial.print(sgp.serialnumber[1], HEX);
- Serial.println(sgp.serialnumber[2], HEX);
- // Clear the buffer
- display.clearDisplay();
- display.display();
- delay(2000);
- testdrawtriangle(); // Draw triangles (outlines)
- testdrawchar(); // Draw characters of the default font
- }
- void loop() {
- // If you have a temperature / humidity sensor, you can set the absolute humidity to enable the humditiy compensation for the air quality signals
- //float temperature = 22.1; // [°C]
- //float humidity = 45.2; // [%RH]
- //sgp.setHumidity(getAbsoluteHumidity(temperature, humidity));
- if (! sgp.IAQmeasure()) {
- Serial.println("Measurement failed");
- return;
- }
- Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppb\t");
- Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.println(" ppm");
- if (! sgp.IAQmeasureRaw()) {
- Serial.println("Raw Measurement failed");
- return;
- }
- Serial.print("Raw H2 "); Serial.print(sgp.rawH2); Serial.print(" \t");
- Serial.print("Raw Ethanol "); Serial.print(sgp.rawEthanol); Serial.println("");
- delay(1000);
- counter++;
- if (counter == 30) {
- counter = 0;
- uint16_t TVOC_base, eCO2_base;
- if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
- Serial.println("Failed to get baseline readings");
- return;
- }
- Serial.print("****Baseline values: eCO2: 0x"); Serial.print(eCO2_base, HEX);
- Serial.print(" & TVOC: 0x"); Serial.println(TVOC_base, HEX);
- }
- // Fan off
- analogWrite(fanPin, 0);
- delay(3000);
- // Medium speed
- analogWrite(fanPin, 150);
- delay(3000);
- // Max speed
- analogWrite(fanPin, 255);
- delay(3000);
- }
- void testdrawtriangle(void) {
- display.clearDisplay();
- for(int16_t i=0; i<max(display.width(),display.height())/2; i+=5) {
- display.drawTriangle(
- display.width()/2 , display.height()/2-i,
- display.width()/2-i, display.height()/2+i,
- display.width()/2+i, display.height()/2+i, SSD1306_WHITE);
- display.display();
- delay(1);
- }
- delay(2000);
- }
- void testdrawchar(void) {
- display.clearDisplay();
- display.setTextSize(2); // Normal 1:1 pixel scale
- display.setTextColor(SSD1306_WHITE); // Draw white text
- display.setCursor(0, 0); // Start at top-left corner
- display.cp437(true); // Use correct CP437 character codes
- display.printf("TVOC: %d ppb\n", sgp.TVOC);
- display.printf("eCO2: %d ppm\n", sgp.eCO2);
- display.println(); // New line
- display.display();
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment