Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Humidity and temperature monitor
// for Arduino UNO R3 (and probably most others)
// Main pieces:
// DHT11 humidity/temp sensor
// - mandatory 10k pull-up resistor
// -> using DHT.f by adafruit at https://github.com/adafruit/DHT-sensor-library
// 16x2 LCD
// - 10K potentiometer for contrast control
// - Push button and transistor for backlight control
// - 220ohm resistor to make the backlight a bit darker
// -> using LiquidCrystal builtin plugin for LCD
// code is hacky, horribly built, unprofessinally organized, and works!
#include <LiquidCrystal.h>
#include "DHT.h"
// pins
#define DHTPIN 14
#define LEDPIN 13
#define BTNPIN 8
#define TRANSPIN 9
// init DHT
#define DHTTYPE DHT11 // DHT 11
#define FAHRENHEIT true
DHT dht(DHTPIN, DHTTYPE);
// init LCD
LiquidCrystal lcd(6, 7, 2, 3, 4, 5);
// button state
long last_button_press = 0;
boolean is_active = false;
// graphics ;)
byte marker1[8] = {
0b10000,
0b01000,
0b01000,
0b00100,
0b00100,
0b00010,
0b00010,
0b00000
};
byte marker2[8] = {
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b00000
};
byte marker3[8] = {
0b00001,
0b00010,
0b00010,
0b00100,
0b00100,
0b01000,
0b01000,
0b00000
};
byte marker4[8] = {
0b00000,
0b00000,
0b00000,
0b11111,
0b00000,
0b00000,
0b00000,
0b00000
};
byte next_marker = 0;
void setup() {
//pushbutton with internal pull-up resistor
pinMode(BTNPIN, INPUT);
digitalWrite(BTNPIN, HIGH);
// transistor
pinMode(TRANSPIN, OUTPUT);
// forget about the internal led
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, LOW);
dht.begin();
lcd.createChar(0, marker1);
lcd.createChar(1, marker2);
lcd.createChar(2, marker3);
lcd.createChar(3, marker4);
lcd.begin(16, 2);
// the LCD template. Values will be written later
lcd.print("Humidity: %");
lcd.setCursor(0, 1);
lcd.print("Temp : F");
// let everything settle
delay(1000);
// and go to sleep
displayOff();
}
// booleanize it.
// also - true if digitalRead is false (!)
// because it is counter intuitive, we keep it
// in a dedicated function.
boolean isButtonPressed() {
int button_pressed = !digitalRead(BTNPIN); // pin low -> pressed
return button_pressed;
}
void displayOn() {
digitalWrite(TRANSPIN, HIGH);
lcd.display();
}
void displayOff() {
digitalWrite(TRANSPIN, LOW);
lcd.noDisplay();
}
void loop() {
boolean button_pressed = isButtonPressed();
if (!button_pressed) {
if (is_active) {
if ((millis() - last_button_press > 10000)) {
is_active = false;
displayOff();
}
}
} else {
last_button_press = millis();
is_active = true;
displayOn();
}
if (is_active) {
digitalWrite(LEDPIN, HIGH);
// this is not optimal. one should be able to read both
// value with a single actuall read from the sensor.
// I think that the DHT11 library is reading twice.
float h = dht.readHumidity();
float t = dht.readTemperature(FAHRENHEIT);
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (!isnan(t) && !isnan(h)) {
lcd.setCursor(10, 0);
lcd.print(h,0);
lcd.setCursor(10, 1);
lcd.print(t, 1);
lcd.setCursor(15, 0);
lcd.write(next_marker);
next_marker = ((next_marker+1) % 4);
}
digitalWrite(LEDPIN, LOW);
delay(2000);
} else {
delay(50);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.