Skip to content

Instantly share code, notes, and snippets.

@rahji
Created January 29, 2023 18:46
Show Gist options
  • Save rahji/88201dfcaa55db9b0658ee7b9b009b25 to your computer and use it in GitHub Desktop.
Save rahji/88201dfcaa55db9b0658ee7b9b009b25 to your computer and use it in GitHub Desktop.
DHT11 sensor lab
#include <Arduino.h>
#include <Wire.h>
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// This function polls for a measurement and returns a boolean
// value of true if a measurement is available.
static bool measure_environment( float *temperature, float *humidity ) {
static unsigned long measurement_timestamp = millis( );
/* check once every four seconds. */
if (millis( ) - measurement_timestamp > 3000ul) {
if( dht_sensor.measure(temperature, humidity) == true ) {
measurement_timestamp = millis();
return true;
}
}
return false;
}
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Temp C ");
lcd.setCursor(0, 1);
lcd.print("Humid % ");
}
void loop() {
float temperature;
float humidity;
/* Measure temperature and humidity. If the functions returns
true, then a measurement is available. */
if( measure_environment(&temperature, &humidity) == true) {
lcd.setCursor(6, 0);
lcd.print(temperature, 0);
lcd.setCursor(6, 1);
lcd.print(humidity, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment