Steven XU
Published © GPL3+

Environmental sensors cooperate with Seeeduino XIAO

This is a little demo with Seeeduino XIAO - Arduino microcontroller. The demo uses 4 modules. 2 sensors, 1 switch, and 1 OLED display.

BeginnerProtip1 hour2,177
Environmental sensors cooperate with Seeeduino XIAO

Things used in this project

Hardware components

Seeeduino XIAO - Arduino Microcontroller - SAMD21 Cortex M0+
×1
Grove - Touch Sensor SKU 101020037
Seeed Studio Grove - Touch Sensor SKU 101020037
×1
Grove - Digital Light Sensor - TSL2561
×1
Grove - Temperature & Humidity Sensor (DHT11)
×1
Grove - OLED Display 0.96" (SSD1315)
×1
Grove - 4 pin Male Jumper to Grove 4 pin Conversion Cable (5 PCs per Pack)
×1
Seeed - 400 Point Solderless Breadboard for Raspberry Pi and Arduino Project
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Environmental sensors with Seeeduino XIAO

Arduino
#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <Digital_Light_TSL2561.h>
#include <DHT.h>

#define DHTPIN    9
#define DHTTYPE DHT11   // DHT 11
#define TouchPin  8     // Grove touch module
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);// Init I2C U8g2 library
DHT dht(DHTPIN, DHTTYPE); // declare DHT11 sensor
uint16_t x1 = 0;

void setup()
{
  Wire.begin();
  Serial.begin(115200);
  TSL2561.init();
  dht.begin();
  u8g2.begin(); 
  pinMode(TouchPin, INPUT); 
 
}

void loop()
{  
         
   int sensorValue = digitalRead(TouchPin); // Read touch module value
   if(sensorValue == 1){  
   float temp_hum_val[2] = {0};
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    
    
    if(!dht.readTempAndHumidity(temp_hum_val)){
        Serial.print("Humidity: "); 
        Serial.print(temp_hum_val[0]);
        Serial.print(" %\t");
        Serial.print("Temperature: "); 
        Serial.print(temp_hum_val[1]);
        Serial.println(" *C");
    }
    else{
       Serial.println("Failed to get temprature and humidity value.");
    }
  uint16_t x2 = TSL2561.readVisibleLux(); // Read Light sensor value
  if(x1-x2>10000 || x2-x1>10000 )  // verify light sensor value
  {
    x1 = x1;
    }
  else {
    x1 = x2;
    } 
  Serial.print("The Light value is: ");
  Serial.println(x1);
  float h = dht.readHumidity(); // Read humidity value
  float t = dht.readTemperature(); // Read temperature value
  Serial.print("The Temperature is: ");
  Serial.println(t);
  Serial.print("The Humidity is: ");
  Serial.println(h);
  u8g2.firstPage(); // use U8g2 library to print data
  do {
    u8g2.setFont(u8g2_font_prospero_bold_nbp_tf);
    u8g2.setCursor(0, 20);
    u8g2.print(F("Light value: "));
    u8g2.print(x1);
    u8g2.print(F("LUX"));
    u8g2.setCursor(0,40);
    u8g2.print(F("Temperature: "));
    u8g2.print(t);
    u8g2.print(F("*C"));
    u8g2.setCursor(0,60);
    u8g2.print(F("Humidity: "));
    u8g2.print(h);
    u8g2.print(F("%"));
  } while ( u8g2.nextPage() );
//  delay(1000);
}
else {
 u8g2.clearDisplay(); // clear the OLED display
}
}

Credits

Steven XU

Steven XU

2 projects • 5 followers

Comments