Hydroponics Automation – Ebb & Flood Basic

This article is a supplement to my video: https://youtu.be/ldvtDrwesoo

This is the schematic for this project. I am no expert at putting together electronic schematics, but hopefully this gets the point across!

Schematic - Ebb and Flood Automation- Basic

The code from the video is available below:

Author: Shaun AKA Chillichump
const String swName = "ChillichumpIO Ebb&Flood Basic";
const String swVer = "1.1";
const String swRel = "09/03/19";

/* Common Millis equivalents
   4 hours = 14400000; 2 hours = 7200000; 1 hour = 3600000
   10 minutes = 600000; 5 minutes = 300000; 2 minutes = 120000; 1 minute = 60000

const long floodIntMillis = 60000; //millis between flooding
const long floodDurMillis = 5000; //millis for flood duration
const long longestDelayBetweenFlooding = 43200000; //longest acceptable delay between flooding. This ensures flooding occurs even if lights fail etc. 43,200,000 is 12 hours
const int lightLevel = 600; //this value indicates whether the sun is up. Value out of 1023. higher = lighter (indirect sun is around 850; normal indoor lighting is around 650)
const long lightLevelDelay = 20000; //delay before deciding it is daylight. To avoid lights, torches etc. causing issues
const byte pumpPin = 6; //pin number for activating relay for pump
const byte pinLightSensor = A2; //pin number for light sensor

unsigned int lightSensorNow = analogRead(A2);
unsigned long sunUpMillis = 0;
unsigned long sunDownMillis = 0;
unsigned long pumpLastOn = floodIntMillis + lightLevelDelay; //ensuring pump comes on when device is turned on and it is light
unsigned long pumpOffAt = 0;
boolean sunUpCheck = false;
boolean sunDownCheck = false;
boolean sunUp;
boolean lastState = false;

void setup() {
  pinMode(pumpPin, OUTPUT);
  digitalWrite(pumpPin, LOW); //pump off

void loop() {
  lightSensorNow = analogRead(pinLightSensor);
  if (lightSensorNow >= lightLevel && sunUp == false) {
    sunUp = true;
  if (lightSensorNow < lightLevel && sunUp == true) {
    sunUp = false;

  if (sunUp == true && sunUpCheck == false) { //checking if sun is up or lights on
    sunUpMillis = millis();
    sunUpCheck = true;
  if (sunUp == false && sunDownCheck == false) { //checking if sun is down or lights are off
    sunDownMillis = millis();
    sunDownCheck = true;
  if (sunUp == true && millis() - sunUpMillis > lightLevelDelay && sunDownCheck == true) { //checking if sun has been up or lights have been on longer than lightLevelDelay
    sunDownCheck = false;
  if (sunUp == false && millis() - sunDownMillis > lightLevelDelay && sunUpCheck == true) { //checking if sun has been down or lights have been off longer than lightLevelDelay
    sunUpCheck = false;

  if (millis() - sunUpMillis > lightLevelDelay && sunUpCheck == true ) { //checking that there has been light longer than the lightLevelDelay
    if (millis() - pumpLastOn > floodIntMillis || millis() < pumpLastOn) { //checking when the pump was last on. also checking if the sun has just come up or lights are on to ensure a flooding at first light.
      pumpLastOn = millis();
      pumpOffAt = millis() + floodDurMillis; //pumpOffAt has been added so that the pump doesn't switch off during a cycle if the lights go our or sun goes down

  if (millis() - pumpLastOn > longestDelayBetweenFlooding && millis() > longestDelayBetweenFlooding) { //ensuring there is water at least every x hours
    pumpLastOn = millis();
    pumpOffAt = millis() + floodDurMillis;

  if (millis() < pumpOffAt) { //turning the pump on
    digitalWrite(pumpPin, HIGH); //pump on
  } else {
    digitalWrite(pumpPin, LOW); //pump off


2 thoughts on “Hydroponics Automation – Ebb & Flood Basic”

  1. Hey Champ/Chump.
    First off, you are amazing and the whole reason I now have built a greenhouse.
    I love fiddling around with small electronics, but can’t figure out the coding…
    In the video you mention and show the more advanced edition of the ebb and flow system. Can you perhaps please provide a link for that and maybe let me know the hardware and the code used for that.
    Kind Regards from a fan from Denmark

    1. Hello Mads, if you sign up on Patreon (patreon.com/Chillichump), you will get access to my private Discord where I have a channel just for discussing tech related things. It might be a good place for you to join up and I could give you a hand. With regards to the code for my advanced hydro system, I will be releasing that at some point I need to just figure out how and when I will be doing that!

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart