Skip to content

Instantly share code, notes, and snippets.

@wdog
Created October 20, 2018 12:28
Show Gist options
  • Save wdog/9531c65bfdd56fe1f2ce769848bf21b1 to your computer and use it in GitHub Desktop.
Save wdog/9531c65bfdd56fe1f2ce769848bf21b1 to your computer and use it in GitHub Desktop.
arduino nano moist sensor temperature humidity waper pump
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
#include "DHT.h"
#define DHTPIN 11
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
#define MOISTPIN 6
int sensorvalue = 0;
int percentagevalue = 0;
#define PUMPPIN 4
LiquidCrystal_PCF8574 lcd(0x27); // set the LCD address to 0x27 for a 16 chars and 2 line display
int show;
void setup()
{
pinMode(PUMPPIN, OUTPUT);
pinMode(MOISTPIN, INPUT);
dht.begin();
int error;
Serial.begin(9600);
Serial.println("LCD...");
while (! Serial);
Serial.println("Dose: check for LCD");
Wire.begin();
Wire.beginTransmission(0x27);
error = Wire.endTransmission();
Serial.print("Error: ");
Serial.print(error);
if (error == 0) {
Serial.println(": LCD OK found.");
}
lcd.begin(16, 2); // initialize the lcd
digitalWrite(PUMPPIN, LOW);
delay(5000);
Serial.println("START...");
}
void loop()
{
lcd.setBacklight(255);
lcd.home();
//lcd.clear();
sensorvalue = analogRead(MOISTPIN);
if (sensorvalue > 1023) {
sensorvalue = 1023;
}
if (sensorvalue < 300) {
sensorvalue = 300;
}
percentagevalue = map(sensorvalue, 1023 , 300, 0 , 100 );
Serial.println(sensorvalue);
lcd.setCursor(0, 0);
lcd.print("Temp: " + padd(String(dht.readTemperature()), " ") + "C");
lcd.setCursor(0, 1);
lcd.print("H " + padd(String(dht.readHumidity()), " ") + "%");
lcd.print(" M " + padd(String(percentagevalue), " ") + "%" );
if (percentagevalue < 50) {
digitalWrite(PUMPPIN, HIGH);
delay(5000);
}
digitalWrite(PUMPPIN, LOW);
delay(500);
}
String padd(String text, String add) {
int len ;
text = add + text;
len = text.length();
return (&text[len - add.length()]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment