Fathead
Published © GPL3+

Fish Tank Controller UPDATED

A basic controller to turn on/off two sets of lights and display temperature on LCD and RGB LED which shows if its in the correct range.

BeginnerShowcase (no instructions)2 hours1,570
Fish Tank Controller UPDATED

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
Adafruit Waterproof DS18B20 Digital temperature sensor
Adafruit Waterproof DS18B20 Digital temperature sensor
×1
DS1307 64 x 8, Serial, I²C Real-Time Clock
Maxim Integrated DS1307 64 x 8, Serial, I²C Real-Time Clock
×1
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1
Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
×1
Dual Relay Shield
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 221 ohm
Resistor 221 ohm
×3
Resistor 4.75k ohm
Resistor 4.75k ohm
×1
LDR
Basic Light Dependant Resistor (LDR)
×1
Box
Purchased from Ebay, not essential but hides everthing
×1
Toggle Switch
Simple SPST toggle switch
×1
5V power Supply
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Diagram

Diagram

Code

Code Version 2.2

C/C++
Used to control fish tank lighting and monitor temperature
// Code by Corners 11/01/19
// Set the RTU in my case DS1307 by using DS1307RTC SetTime example
// Main light (white T6) goes on at 0800 and off at 1900
// LED light turns on at 1900 and off at 2100
// RGB LED status light - RED too hot above 27 degees C
//                      - Green within temperature range 24-27 degrees C
//                      - Blue too cold below 24 degrees C  
// Switch added to turn main light (white) on when outside of normal on hours


#include <OneWire.h>
#include <DallasTemperature.h>
#include <DS1307RTC.h>
#include <RTClib.h>
#include <TimeLib.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define ONE_WIRE_BUS D4
#define Relay1 D7
#define Relay2 D8

LiquidCrystal_I2C lcd(0x3F, 16, 2);

int val;
int redPin = D0;
int greenPin = D6;
int bluePin = D5;
int LDR = A0;
int Light = 0;
int Switch = D3;

float V = 2.2;    //version
float WaterTemp;

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup()
{
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  Wire.begin();
  sensors.begin();

  lcd.setCursor(0, 0);
  lcd.print(F(" Juwel RIO 180"));
  delay(1000);
  lcd.setCursor(0, 1);
  lcd.print(F("Tank Controller"));
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 1);
  lcd.print(F(" Version : "));
  lcd.print(V, 1);
  delay(2000);
  lcd.clear();

  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(LDR, INPUT);
  pinMode(Switch, INPUT_PULLUP);

  digitalWrite(Relay1, HIGH);
  digitalWrite(Relay2, HIGH);

  tmElements_t  tm;

  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if (timeStatus() != timeSet) {
    Serial.println("Unable to sync with the RTC");
  } else {
    Serial.println("RTC has set the system time");
  }
  if (RTC.read(tm)) {
    Serial.print("Ok, Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS3231 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    } else {
      Serial.println("DS3231 read error!  Please check the circuitry.");
      Serial.println();
    }
    delay(9000);
  }
  delay(1000);

}

  void loop()
  {
    Light = analogRead(LDR);
    Serial.print("Light Level: ");
    Serial.println(Light);
    if (Light < 25) {
      lcd.noBacklight();
    }  else   {
      lcd.backlight();
    }

tmElements_t tm;
    
if (RTC.read(tm)) {
    lcd.setCursor(0,0);
    print2digits1(tm.Hour);
    lcd.print(F(":"));
    print2digits1(tm.Minute);
    
  }
   
    Serial.print("Relay Hour: ");
    Serial.println(tm.Hour);

    if ((tm.Hour >= 8 && tm.Hour <= 18) || (digitalRead(Switch) == HIGH)) 
    {
      digitalWrite(Relay1, LOW);
      lcd.setCursor(4, 1);
      lcd.print("ON ");
    } else {
      digitalWrite(Relay1, HIGH);  
      lcd.setCursor(4, 1);
      lcd.print("OFF");   
    }
    
    if (tm.Hour >= 19 && tm.Hour <= 20)
    {
      digitalWrite(Relay2, LOW);
      lcd.setCursor(13, 1);
      lcd.print("ON ");
    } else {
      digitalWrite(Relay2,HIGH);
      lcd.setCursor(13, 1);
      lcd.print("OFF");
    }
        
    lcd.setCursor(0, 1);
    lcd.print(F("T6: "));
    lcd.setCursor(8, 1);
    lcd.print(F("LED: "));

    WaterTemp = (sensors.getTempCByIndex(0));

    Serial.print(F(" Requesting temperatures..."));
    sensors.requestTemperatures(); // Send the command to get temperature readings
    Serial.println("DONE");

    Serial.print(F("Water Temperature is: "));
    Serial.println(WaterTemp);
    lcd.setCursor(9, 0);
    lcd.print(WaterTemp, 1);
    lcd.print(F(" "));
    lcd.print((char)223);
    lcd.print(F("C"));
    delay(1000);

    delay(1000);

    if (WaterTemp < 24)
    {
      setColor(0, 0, 255); // Blue Color, Water Temp too COLD
    }
    if (WaterTemp >= 24 && WaterTemp <= 27)
    {
      setColor(0, 255, 0); // Green Color, Water Temp OK
    }
    if (WaterTemp > 27)
    {
      setColor(255, 0, 0); // Red Color, Water Temp too HOT
    }
  }

  void setColor(int redValue, int greenValue, int blueValue) {
    analogWrite(redPin, redValue);
    analogWrite(greenPin, greenValue);
    analogWrite(bluePin, blueValue);
  }

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

void print2digits1(int numbers){
  if (numbers >= 0 && numbers < 10){
    lcd.print("0");
  }
  lcd.print(numbers);  
}

Credits

Fathead

Fathead

2 projects • 11 followers

Comments