Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
25
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define relay_pin 2
  2. #define humidity_sensor_vcc 3
  3. #define humidity_sensor_pin A4
  4.  
  5. void setup() {
  6.   // Usual setup
  7.   pinMode(LED_BUILTIN, OUTPUT);
  8.   pinMode(relay_pin, OUTPUT);
  9.   pinMode(humidity_sensor_vcc, OUTPUT);
  10.   digitalWrite(relay_pin, HIGH);
  11.   delay(2000); // Safety delay
  12. }
  13.  
  14. int read_humidity_sensor() {
  15.   int value;
  16.   digitalWrite(humidity_sensor_vcc, HIGH); // Power up sensor
  17.   digitalWrite(LED_BUILTIN, HIGH); // Builtin led on to show activity
  18.   delay(500); // Wait a little bit until sensor is ready
  19.  
  20.   value = analogRead(humidity_sensor_pin);
  21.  
  22.   digitalWrite(humidity_sensor_vcc, LOW); // Sensor off
  23.   digitalWrite(LED_BUILTIN, LOW); // Activity led off
  24.   return 1023 - value;// Capture range is 0..1023
  25. }
  26.  
  27. void loop() {
  28.   int soil_humidity = read_humidity_sensor();
  29.   if(soil_humidity > 300) {
  30.     digitalWrite(relay_pin, LOW);// Relay low = pump on  
  31.   } else {
  32.     digitalWrite(relay_pin, HIGH);
  33.   }
  34.   delay(10000);// Next loop in 10 secs
  35. }
Advertisement
RAW Paste Data Copied
Advertisement