Skip to content

Instantly share code, notes, and snippets.

@excalq
Last active September 8, 2019 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save excalq/125b2ebf737e3e60581756c766a4af2f to your computer and use it in GitHub Desktop.
Save excalq/125b2ebf737e3e60581756c766a4af2f to your computer and use it in GitHub Desktop.
ESP32 Sensors: DHT11 (Temp+Humidity), 150PSI Transducer [NodeMCU ESP32 Dev Board]
// ESP32 Sensors: DHT11 (Temp+Humidity), 150PSI Transducer [NodeMCU ESP32 Dev Board]
// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
#include "DHT.h"
#define DHTPIN 23 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
// Pressure Sensor Setup
// Analog data pin for pressure sensor
#define PRESSURE_ADC_PIN 36
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
int measure_frequency = 2000; // Millsecs
delay(measure_frequency);
print_temp_and_humidity();
print_transducer_pressure();
}
// Print Functions
void print_temp_and_humidity() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}
void print_transducer_pressure() {
int reading;
int read_qty;
float pressure;
reading = 0;
read_qty = 20;
// take 10 samples at 20ms intervals
for (int i = 0; i < read_qty; i++) {
reading += analogRead(PRESSURE_ADC_PIN);
delay(20);
}
// simple linear model from sensor reading to PSI
// Abdul: rough guess of 0psi (416) and slope (30.7 / psi)
// Arthur: baseline is ~425 (0psi) (with 100k ohm to GND)
// Arthur: calibrated 2019.09.08 in Kepler Garage on CO2 tank w/ 150psi tranducer
// Baseline at 0psi was ~425 (on ADC13/GPIO23 on NodeMCU ESP32 Dev Board, with 100kΩ resistor to GND)
// Using the following values, calculated slope was 23.1:
// {0: 425, 10: 625, 15: 750, 20: 865, 30: 1115}
// https://docs.google.com/spreadsheets/d/1o72WBAO7lgtv2Z4gCs53Q8aE0iwgIj8KWk1Tl2XpHkI/edit?usp=sharing
int baseline = 0; // With no input connected
int td_zero = 425; // Tranducer at 0psi
int td_thirty = 1100; // Tranducer
float slope = 23.1; // Tranducer
pressure = ((reading / read_qty) - (baseline + td_zero)) / slope;
Serial.print(F("CO2 Keg Line pressure: "));
Serial.print(pressure);
Serial.print(F("PSI Raw data reading was: "));
Serial.println(reading / read_qty);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment