Arduino

Irrigation System

Enhanced code to control a waterpump, a display and an “Capacitive Soil Moisture Sensor”.

  • Display shows a diagramm in %
  • Sensor values
  • dry or wet
  • % value
Irrigation.ino
//Irrigation 
//version 1.2
//by tmade
 
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
 
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//Adafruit_SSD1306 display(-1);
 
//variables
int SLEEP = 1;
const int DRY = 694;
const int WET = 344;
int sensorValue = 0;
int rectangle = 0;
const int X = 10;
const int Y = 1;
const int WIDTH = 35;
const int HEIGHT = 63;
 
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  //to debug set to 115200 !!
  //Serial.begin(115200);
 
  digitalWrite(0, 1);
  pinMode (2, OUTPUT);
  digitalWrite(2, 1);
 
  // initialize with the I2C addr 0x3C
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
 
  // Clear the buffer.
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  //Define the position where the text starts using the setCursor(x,y) method
  display.setCursor(0,20);
  display.println("Startup..");
  display.display();
  delay(4000);
}
 
void loop() {
  // put your main code here, to run repeatedly:
  display.clearDisplay();
 
  int sensorValue=analogRead(A0);
  int val = analogRead(0);
  val = map(val, DRY, WET, 0, 100);
  delay(200);
  int rectangle = analogRead(0);
  rectangle = map(rectangle, DRY, WET, 1, 63);
  delay(200);
 
// debugging & examples
/*
  Serial.print("rectanglevalue: ");
  Serial.println(rectangle);
 
  analogWrite(9, val);
 
  Serial.print("Dry: ");
  Serial.println(WET);
  Serial.print("Wet: ");
  Serial.println(WET);
  delay(200);
*/  
 
  SLEEP = SLEEP-1;
  if ((sensorValue > 570) && (SLEEP < 2)) 
  {
    display.clearDisplay();
    // drawRect(x, y, width, height, color)
    display.drawRoundRect(X, Y, WIDTH, HEIGHT, 2, WHITE);
    display.fillRoundRect(X, Y, WIDTH, rectangle, 2, WHITE);
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(60,5);
    display.println("dry!");
    display.setCursor(60,25);
    display.setTextSize(2);
    display.println(sensorValue);
    display.setCursor(60,45);
    display.setTextSize(2);
    display.print(val);
    display.println(" %");
    // write the buffer to the display
    display.display();
    delay(200);
 
    Serial.print("sensorValue: ");
    Serial.println(sensorValue);
 
    Serial.println("Pump On");
    // 0 = LOW = ON
    digitalWrite(2,0); 
    //set pump duration per loop in milliseconds
    delay(5000);
    // 1 = HIGH = OFF
    digitalWrite(2,1);  
    Serial.println("Pump Off");
    delay(200);
    //set wait until next check
    SLEEP = 60;
  }
 
   else if (sensorValue < 571)
  {
    Serial.print("sensorValue: ");
    Serial.println(sensorValue);
    Serial.print("humidity: ");
    Serial.print(val);
    Serial.println(" %");
    Serial.println("Sensor is wet");
    delay(1000);
    // Display
    display.clearDisplay();
    // drawRect(x, y, width, height, color)
    display.drawRoundRect(X, Y, WIDTH, HEIGHT, 2, WHITE);
    display.fillRoundRect(X, Y, WIDTH, rectangle, 2, WHITE);
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(60,5);
    display.println("wet!");
    display.setCursor(60,25);
    display.setTextSize(2);
    display.println(sensorValue);
    display.setCursor(60,45);
    display.setTextSize(2);
    display.print(val);
    display.println(" %");
    // write the buffer to the display
    display.display();
    delay(1000);
  }
 
  else if (SLEEP > 2) 
  {
    display.clearDisplay();
    // drawRect(x, y, width, height, color)
    display.drawRoundRect(X, Y, WIDTH, HEIGHT, 2, WHITE);
    display.fillRoundRect(X, Y, WIDTH, rectangle, 2, WHITE);
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(60,5);
    display.println("dry!");
    display.setCursor(60,25);
    display.setTextSize(2);
    display.println(sensorValue);
    display.setCursor(60,45);
    display.setTextSize(2);
    display.print(val);
    display.println(" %");
    // write the buffer to the display
    display.display();
    delay(200);
 
    Serial.print("sensorValue: ");
    Serial.println(sensorValue);
    Serial.print("humidity: ");
    Serial.print(val);
    Serial.println(" %");
    Serial.println("Sensor is dry");
    Serial.print("wait another: ");
    Serial.print(SLEEP); 
    Serial.println(" seconds");  
    delay(200);
  }
}

Alles & nichts

Code to control a waterpump, a display and an “Capacitive Soil Moisture Sensor”.

Many thanks to “Alles & nichts” for sharing this code! :-)

Have also a look on:

https://www.youtube.com/watch?v=sRStuXpqiQ4

/*
VCC = 5 Volt cap sens
VCC = 3,3 Volt Relais

-------------------------------------------------------------*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 display(-1);

/*
const unsigned char Smiley_lachen [] PROGMEM = {
 
};

const unsigned char Smiley_middle [] PROGMEM = {

};

const unsigned char Smiley_traurig [] PROGMEM = {

};*/

/*const int waterValue= 400;
const int moistValue= 600;
const int dryValue = 850;
const int pumpON = 660;*/

int val = 1;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  digitalWrite(0, 1);
  pinMode (2, OUTPUT);
  digitalWrite(2, 1);

  // initialize with the I2C addr 0x3C
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  

  // Clear the buffer.
  display.clearDisplay();
  //display.invertDisplay(1);

  // symbol_leer

  display.drawRoundRect(0, 14, 30, 50, 5, WHITE);
  display.display();
  delay(2000);

  // symbol_viertelvoll

  display.fillRoundRect(0, 14, 30, 12.5, 2, WHITE);
  display.display();
  delay(2000);

  // symbol_halbvoll

  display.fillRoundRect(0, 14, 30, 25, 2, WHITE);
  display.display();
  delay(2000);

  // symbol_dreiviertelvoll

  display.fillRoundRect(0, 14, 30, 38, 2, WHITE);
  display.display();
  delay(2000);

  // symbol_voll

  display.fillRoundRect(0, 14, 30, 50, 2, WHITE);
  display.display();
  delay(2000);

}

void loop() {
  // put your main code here, to run repeatedly:
  display.clearDisplay();
  int sensorValue=analogRead(A0);
 
  Serial.println(sensorValue);
  delay(200); 

  //display.println(sensorValue);
 
  if (sensorValue > 570)
  {
  //val=!val;
  display.drawRoundRect(0, 14, 30, 50, 5, WHITE);
  display.display();
  digitalWrite(2,0); // 0 bzw. LOW = AN
  }

  else if ((sensorValue < 550) && (sensorValue > 500))
  {
  display.fillRoundRect(0, 14, 30, 12.5, 2, WHITE);
  display.display();
  }

  else if ((sensorValue < 500) && (sensorValue > 450))
  {
  display.fillRoundRect(0, 14, 30, 38, 2, WHITE);
  display.display();
  }

  else if (sensorValue < 450) 
  {
  display.fillRoundRect(0, 14, 30, 50, 2, WHITE);
  display.display();
  digitalWrite(2,1);  // 1 bzw. HIGH = AUS
  }
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(50,20);
    display.println(sensorValue);
    
    display.display();
    delay(200);
}

Another example

http://hyperdramatik.net/mediawiki/index.php?title=Bew%C3%A4sserungsanlage_mit_Arduino_Uno_und_DIY-Feuchtigkeitssensor

void setup() {
     Serial.begin(9600);
     pinMode(A0, OUTPUT); //Default pinMode einstellung auf INPUT/OUTPUT
 digitalWrite(A0, HIGH);
}

void loop() {

     int sensor = analogRead(A1);
     Serial.println(sensor);

     //SENSOR FEUCHT - 500

     //SENSOR TROCKEN - 0

     delay(1000);

     if (sensor < 430 ) {

          //Relais einschalten

          digitalWrite(A0, LOW);
          Serial.println("Pumpe AN");

          //2 Sekunden anlassen

          delay(2000);

          //Relais ausschalten

          digitalWrite(A0, HIGH);

           Serial.println("Pumpe Aus");


          //10 Sekunden warten

          delay(10000);

     }
}

Display

https://randomnerdtutorials.com/guide-for-oled-display-with-arduino/

To check HEX-adress of your display - which has to be referenced - please run this sketch:

oled-test.ino
/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/
 
#include <Wire.h>
 
void setup() {
  Wire.begin();
  Serial.begin(115200);
  Serial.println("\nI2C Scanner");
}
 
void loop() {
  byte error, address;
  int nDevices;
  Serial.println("Scanning...");
  nDevices = 0;
  for(address = 1; address < 127; address++ ) {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address<16) {
        Serial.print("0");
      }
      Serial.println(address,HEX);
      nDevices++;
    }
    else if (error==4) {
      Serial.print("Unknow error at address 0x");
      if (address<16) {
        Serial.print("0");
      }
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0) {
    Serial.println("No I2C devices found\n");
  }
  else {
    Serial.println("done\n");
  }
  delay(200);          
}

Note: You have to change baud rate in serial monitor to “115200” to get the result!!

MQTT

arduino/arduino.txt · Last modified: 2020/07/27 21:44 by tmade
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki