Türchen Nr. 18

It's hard to believe, but Christmas is only 6 days away and today the Christmas elves have a little surprise with them. In the past few days you have seen many projects with the Uno R3 Type 1 and the Nano V3, but also some projects with micro controllers from the ESP32 series! Today a small project with the ESP32 Dev Kit C is to be implemented again and components from previous Christmas contributions are also to be used.

Andreas Wolter has already presented the GY-BME280 barometric sensor for temperature, humidity and air pressure in his Christmas blog and the 16x2 LCD display with I2C adapter has also been used several times. Today's blog also shows that the ESP32 Dev Kit C has not just one, but two I2C interfaces.

What is needed?

  • Arduino IDE in the latest version
  • Contents of door no.18
  • Micro USB cable
  • BME / BMP280
  • 16x2 LCD display with I2C adapter
  • The libraries Adafruit BME280 Library and LiquidCrystal_I2C with all dependencies

The preparation

Start the Arduino IDE and open the preferences via File -> Preferences, see illustration 1. If you have not yet installed the Arduino IDE, please do so beforehand.

 Illustration 1: Open preferences

Add the address to the additional board administrator URLs https://dl.espressif.com/dl/package_esp32_index.json add, see Figure 2.

Illustration 2: Add additional url

It is best to restart the Arduino IDE, even if this is not absolutely necessary, but this can prevent problems. Immediately afterwards you open the board manager, see Figure 3.

Illustration 3: Open the board manager

Use the search bar to search for the ESP32 library and install the latest version of esp32 by Espressif Systems, see Figure 4.

Illustration 4: Install ESP board library

This completes the preparations and you can now insert door number 18.

The BME / BMP280 monitor

The wiring for the BME / BMP280 monitor is in Figure 5 shown. Please note at this point that we want to use two separate I2C interfaces, so the BME / BMP280 and the LCD display are on separate I2C pins.

Illustration 5: Wiring the components

Since the LiquidCrystal_I2C library is quite old, only the default I2C interface for configuration via GPIO 21 and 22 is possible. With the BME / BMP280, we can freely configure the pins on the ESP32 Dev Kit C.

 

The source code is pretty simple too, see Code 1, and provided with some comments. Otherwise you can derive the function from the serial outputs.

//-----------------------------------------------------
// ESP32 Dev Kit C BME / BMP280 monitor
// Author: Joern Weise
// License: GNU GPl 3.0
// Created: Dec 11, 2020
// Update: Dec 11, 2020
//-----------------------------------------------------
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <LiquidCrystal_I2C.h>

#define I2C_SDA2 17
#define I2C_SCL2 16
#define SEALEVELPRESSURE_HPA (1013.25)
#define TIMEUPDATESENSOR 30000
#define TIMESWITCHDISPLAY 10000

unsigned long lLastUpdateSensor, iLastSwitchDisplay;
float fTemp, fPress, fHumi, fAlt;
int iNewView;
TwoWire I2Ctwo = TwoWire (1);
Adafruit_BME280 bme; // I2C
LiquidCrystal_I2C lcd (0x27, 16, 2);

/*
=================================================================
Function: setup
Returns: void
Description: Needed setup () - function
=================================================================
*/
void setup () {
Serial.begin (115200);
while (! Serial); // time to get serial running
Serial.print ("Init BME / BMP280 - Sensor ....");
unsigned status;
I2Ctwo.begin (I2C_SDA2, I2C_SCL2, 400000);
status = bme.begin (0x76, & I2Ctwo);
if (! status) {
Serial.println ("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16);
while (1) delay(10);
  }
Serial.println(" DONE");
Serial.print("Init LCD-Display ....");
lcd.init();           // on error, try lcd.begin();
lcd.backlight();
lcd.clear();
lcd.home();
lcd.print(" BME/BMP280 ");
lcd.setCursor(0, 1);
lcd.print(" Monitoring ");
Serial.println(" DONE");
delay(5000);
Serial.println();
updateSensorData();
iNewView = -1;
}

/*
=================================================================
Function: loop
Returns: void
Description: Needed loop()-function that control sensor
and update display
=================================================================
*/
void loop() {
//Check if update for sensor-data is needed
if (millis() - lLastUpdateSensor > TIMEUPDATESENSOR)    {
updateSensorData();
lLastUpdateSensor = millis();
  }
//Check if new display view is needed
if (millis() - iLastSwitchDisplay > TIMESWITCHDISPLAY)    {
iNewView++;
(iNewView >= 2) ? iNewView = 0 : iNewView = 1;
iLastSwitchDisplay = millis();
Serial.println("Update view on display");
updateDisplay(iNewView);
  }
}

/*
=================================================================
Function: updateSensorData
Returns: void
Description: Update sensor for the newest sensor-data
=================================================================
*/
void updateSensorData() {
fTemp = bme.readTemperature();
fPress = (bme.readPressure() / 100.0F);
fAlt = bme.readAltitude(SEALEVELPRESSURE_HPA);
fHumi = bme.readHumidity();
Serial.println("----- NEXT UPDATE SENSOR -----");
Serial.println("Temperature = " + String(fTemp) + " *C");
Serial.println("Humidity = " + String(fHumi) + " %");
Serial.println("Pressure = " + String(fPress) + " hPa");
Serial.println("Approx. Altitude = " + String(fAlt) + " m");
Serial.println("------------------------------");
}

/*
=================================================================
Function: updateDisplay
IN iView: Index, which view is needed
Returns: void
Description: Update sensor for the newest sensor-data
=================================================================
*/
void updateDisplay(int iView) {
lcd.clear();
lcd.home();
if (iView == 0) //View temp and humidity    {
lcd.print("Temp: " + String(fTemp) + " *C");
lcd.setCursor(0, 1);
lcd.print("Humi: " + String(fHumi) + " %");
  }
else //View pressur and altitude     {
lcd.print("Press: " + String(int(fPress)) + " hPa");
lcd.setCursor(0, 1);
lcd.print("Alt: " + String(fAlt) + " m");
  }
}

Code 1: Code für BME/BMP280 Monitor (Download)


With this, you have put door number 18 into operation. Would you like more project ideas? Then here are some examples:


More of my projects for AZ-Delivery can be found on github:

https://github.com/M3taKn1ght/Blog-Repo.

 

Specials

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN