Archive for the ‘arduino’ Category

This post gives coding for temperature and dewpoint displays in both Fahrenheit and Celsius.

If you enjoy programming microcontrollers in Arduino IDE and building prototypes, this is a cool project to consider. It uses a battery powered ESP32-WROOM module with Bluetooth capability and a DHT11 or DHT22 temperature humidity sensor to transmit a set of four parameters that measure the comfort of your environment to an app on your Android smartphone. The prototype is portable (in principle wearable) and because it communicates via Bluetooth it doesn’t interrupt your phone’s internet connectivity.

Prototype construction

I used a 30-pin ESP32-WROOM microcontroller equipped with a DHT22 sensor on a standard 400 tie-point breadboard with a Breadboard Power Supply Module delivering 5V to the ESP32 module (VIN, GND) from a 9V battery. The sensor, mounted on a breakout board with a built-in pull-up resistor, is powered from the ESP32 (3V3, GND) with the signal routed to GPIO4. The Bluetooth app installed on my Android phone in the header picture is Serial Bluetooth Terminal by Kai Morich.

Comfort indicators

We human beings are equipped with sensory nerves that enable us to feel the Temperature of our surroundings, but it is the relation between temperature and humidity which really determines how comfortable our environment feels. Relative humidity is the percentage uptake of air’s water vapor carrying capacity,  but because this capacity rises and falls as air warms and cools it is not always the best guide to comfort. Absolute humidity measures the actual amount of water vapor in the air. In more temperate climates this is useful especially for indoor environments: 5 g/m3 or below feels dry, 8 – 11 g/m3 is comfortable, above 13 g/m3 starts getting clammy. For both indoor and outdoor environments, the Dewpoint temperature is a good measure for assessing comfort particularly during summer: a dewpoint of 13C/55F is comfortable, a dewpoint of 15C/60F is beginning to get clammy, while a dewpoint of 18C/65F is oppressive.

Coding and Uploading

The coding below was compiled in the Arduino IDE programming environment.
NOTE 1: When ESP32 boards are installed, the BluetoothSerial.h library comes with it so you don’t need to install it separately.
NOTE 2: The declarations for ah, td and tdf must be on one line.
NOTE 3: The display is set to refresh every 30 seconds = 30000 milliseconds; this can be configured in the millisecond delay(30000) at the end of the code.
BEFORE UPLOAD: With the board connected to the USB, check under Tools menu that the correct board is chosen and that the relevant COM port is selected. Click Upload then press and hold down the BOOT button on the ESP32 and release it when the status message at the bottom of the IDE window shows “Connecting …….” When the sketch has loaded, open the Arduino IDE Serial Monitor, set the baud rate to 115200 and press the enable EN button on the ESP32 to start the device for Bluetooth pairing and check processing of data from the sensor.
Now you’re good to go with your smartphone Bluetooth app.

– – – – – – – – – – – – – – –
Display in degrees Celsius
– – – – – – – – – – – – – – –

/*
Bluetooth Temperature Humidity Gauge for Android Smartphones using an ESP32-WROOM
microprocessor equipped with a DHT22 or DHT11 RH&T sensor to pair with a Smartphone app
such as Serial Bluetooth Terminal. Variables displayed are relative humidity in %, absolute humidity in g/m3, temperature and dewpoint temperature in degrees Celsius. Program by Peter Mander, published August 2022 by CarnotCycle blog.
*/

#include “BluetoothSerial.h”
#include <DHT.h>
#include <DHT_U.h>
#include <math.h>

BluetoothSerial SerialBT;

#define DHTPIN 4
//#define DHTTYPE DHT22 //uncomment as necessary
//#define DHTTYPE DHT11 //uncomment as necessary
DHT dht(DHTPIN, DHTTYPE);
float rh, t, tp, ah, td;

void setup() {
Serial.begin(115200);
dht.begin();
SerialBT.begin(“ESP32”); // Bluetooth device name
Serial.println(“ESP32 ready to pair with Bluetooth”);
}

void loop() {
rh = dht.readHumidity(); //relative humidity in %
t = dht.readTemperature(); //temperature in Celsius
tp = 1-(373.15/(273.15+t));
ah = (1013.25*pow(2.71828,((13.3185*tp)-(1.9760*pow(tp,2))-(0.6445*pow(tp,3))-(0.1299*pow(tp,4))))*rh*2.1667)/(273.15+t); // absolute humidity = water vapor density in g/m^3, formula by P.Mander 2020
td = 243.5*(log(rh/100)+((17.67*t)/(243.5+t)))/(17.67-log(rh/100)-((17.67*t)/(243.5+t))); // dew point temperature in Celsius, formula by P.Mander 2017
Serial.print(“Rel Humidity “); Serial.print(rh); Serial.println(” %”);
Serial.print(“Abs Humidity “); Serial.print(ah); Serial.println(” g/m3″);
Serial.print(“Temperature “); Serial.print(t); Serial.println(” deg C”);
Serial.print(“Dewpoint “); Serial.print(td); Serial.println(” deg C”);
Serial.println(” “);
SerialBT.print(“Rel Humidity “); SerialBT.print(rh); SerialBT.println(” %”);
SerialBT.print(“Abs Humidity “); SerialBT.print(ah); SerialBT.println(” g/m3″);
SerialBT.print(“Temperature “); SerialBT.print(t); SerialBT.println(” deg C”);
SerialBT.print(“Dewpoint “); SerialBT.print(td); SerialBT.println(” deg C”);
SerialBT.println(” “);
delay(30000);
}

– – – – – – – – – – – – – – – – –
Display in degrees Fahrenheit
– – – – – – – – – – – – – – – – –

/*
Bluetooth Temperature Humidity Gauge for Android Smartphones using an ESP32-WROOM
microprocessor equipped with a DHT22 or DHT11 RH&T sensor to pair with a Smartphone app
such as Serial Bluetooth Terminal. Variables displayed are relative humidity in %, absolute humidity in g/m3, temperature and dewpoint temperature in degrees Fahrenheit. Program by Peter Mander, published August 2022 by CarnotCycle blog.
*/

#include “BluetoothSerial.h”
#include <DHT.h>
#include <DHT_U.h>
#include <math.h>

BluetoothSerial SerialBT;

#define DHTPIN 4
//#define DHTTYPE DHT22 //uncomment as necessary
//#define DHTTYPE DHT11 //uncomment as necessary
DHT dht(DHTPIN, DHTTYPE);
float rh, t, tf, tp, ah, tdf;

void setup() {
Serial.begin(115200);
dht.begin();
SerialBT.begin(“ESP32”); // Bluetooth device name
Serial.println(“ESP32 ready to pair with Bluetooth”);
}

void loop() {
rh = dht.readHumidity(); //relative humidity in %
t = dht.readTemperature(); //temperature in Celsius
tf = t*9.0/5.0+32.0; //temperature in Fahrenheit
tp = 1-(373.15/(273.15+t));
ah = (1013.25*pow(2.71828,((13.3185*tp)-(1.9760*pow(tp,2))-(0.6445*pow(tp,3))-(0.1299*pow(tp,4))))*rh*2.1667)/(273.15+t); // absolute humidity = water vapor density in g/m^3, formula by P.Mander 2020
tdf = 243.5*(log(rh/100)+((17.67*t)/(243.5+t)))/(17.67-log(rh/100)-((17.67*t)/(243.5+t)))*9.0/5.0+32.0; // dew point temperature in Fahrenheit, formula by P.Mander 2017
Serial.print(“Rel Humidity “); Serial.print(rh); Serial.println(” %”);
Serial.print(“Abs Humidity “); Serial.print(ah); Serial.println(” g/m3″);
Serial.print(“Temperature “); Serial.print(tf); Serial.println(” deg F”);
Serial.print(“Dewpoint “); Serial.print(tdf); Serial.println(” deg F”);
Serial.println(” “);
SerialBT.print(“Rel Humidity “); SerialBT.print(rh); SerialBT.println(” %”);
SerialBT.print(“Abs Humidity “); SerialBT.print(ah); SerialBT.println(” g/m3″);
SerialBT.print(“Temperature “); SerialBT.print(tf); SerialBT.println(” deg F”);
SerialBT.print(“Dewpoint “); SerialBT.print(tdf); SerialBT.println(” deg F”);
SerialBT.println(” “);
delay(30000);
}

– – – –

P. Mander August 2022

This prototype displays temperature, relative humidity, dew point temperature and absolute humidity

UPDATE: See the new wireless version < here >

As shown in previous posts on the CarnotCycle blog, it is possible to compute dew point temperature and absolute humidity (defined as water vapor density in g/m^3) from ambient temperature and relative humidity. This adds value to the output of RH&T sensors like the DHT22 pictured above, and extends the range of useful parameters that can be displayed or toggled on temperature-humidity gauges employing these sensors.

Meteorological opinion* suggests that dew point temperature is a more dependable parameter than relative humidity for assessing climate comfort especially during summer, while absolute humidity quantifies water vapor in terms of mass per unit volume. In effect this added parameter turns an ordinary temperature-humidity gauge into a gas analyzer. (more…)