Wemos D1 Mini Smart Aquarium Controller with Heater, Light & Fish Feeder

Build a Wemos D1 Mini smart aquarium controller with a DS18B20 thermostat for the heater, a scheduled LED light, an SG90 servo fish feeder and a Wi-Fi web dashboard. Wiring table, diagram and full code.

  • DifficultyAdvanced
  • Build time5–8 hours
  • Board / MCUWemos D1 Mini (ESP8266)
  • Parts9 items
  • CodeArduino C++

Need it ready-made? Buy Ready Project

This Wemos D1 Mini smart aquarium controller looks after a fish tank on its own. A waterproof DS18B20 probe measures the water temperature and switches the heater through a relay, a second relay turns the aquarium light on and off on a daily schedule, and a small SG90 servo drops food into the tank twice a day. Everything can be checked and controlled from a web dashboard on your phone over home Wi-Fi.

It is a big combination project that brings together a 1-Wire sensor, relays, a servo, NTP internet time and a web server on one cheap ESP8266 board. The heater thermostat keeps working even if Wi-Fi goes down.

Wemos D1 Mini smart aquarium controller with DS18B20 probe, 2-channel relay and SG90 servo fish feeder

How the Smart Aquarium Controller Works

  • Temperature control: every 2 seconds the D1 Mini reads the DS18B20 probe. If the water is more than 0.5 °C below the target (26 °C by default) the heater relay turns ON, and when it is 0.5 °C above the target the relay turns OFF. This gap (hysteresis) stops the relay clicking on and off.
  • Fail-safe: if the probe is unplugged or the water goes above 31 °C, the heater is forced OFF.
  • Light schedule: after connecting to Wi-Fi the board gets the time from an NTP server (set to India time, UTC+5:30). The light relay is ON from 08:00 to 20:00. You can override it from the dashboard and press Auto to go back to the schedule.
  • Automatic feeder: at 09:00 and 18:00 the servo tips a small food container to 120° and back, dropping a pinch of food. A Feed now button does the same on demand. The servo is detached after each feed so it does not buzz or jitter.
  • Web dashboard: open the IP address shown in the Serial Monitor to see the water temperature, heater and light state and the last feeding time, and to change the target temperature in 0.5 °C steps.

Components Required

  • Wemos D1 Mini (ESP8266) – 1
  • DS18B20 waterproof temperature probe (stainless steel, 1 m cable) – 1
  • 4.7 kΩ resistor (pull-up for the DS18B20) – 1
  • 2-channel 5 V relay module (opto-isolated, contacts rated 10 A 250 V) – 1
  • SG90 micro servo – 1, with a small plastic pill box or bottle cap as the food container
  • 5 V 2 A USB adapter and USB cable (powers the D1 Mini, relay and servo)
  • 470 µF / 10 V electrolytic capacitor across 5 V and GND near the servo – 1
  • Aquarium heater and aquarium LED light (your existing ones)
  • Breadboard or perfboard, jumper wires, screw terminals and a closed plastic enclosure

Wiring Table

Module pinWemos D1 Mini pinNotes
DS18B20 red (VDD)3V3–
DS18B20 black (GND)GND–
DS18B20 yellow (DATA)D2 (GPIO4)4.7 kΩ pull-up from DATA to 3V3
Relay module VCC5VPowered from the USB 5 V
Relay module GNDGNDCommon ground
Relay IN1D1 (GPIO5)Heater relay
Relay IN2D5 (GPIO14)Light relay
Servo red5VAdd 470 µF across 5 V and GND
Servo brownGND–
Servo orange (signal)D6 (GPIO12)Feeder servo
Relay 1 COM / NOHeater live wireHeater ON when relay is ON
Relay 2 COM / NOLight live wireLight ON when relay is ON

Most cheap relay modules switch ON when the input is LOW, and the sketch is set for that (RELAY_ACTIVE_LOW = true). If your module switches ON with a HIGH input, change it to false. If the relays do not click reliably with the 3.3 V signal, use a module marked as 3.3 V compatible.

Connection Diagram

The illustration at the top shows the general layout. The pin-accurate drawing below matches the wiring table exactly.

Wemos D1 Mini Smart Aquarium Controller – Pin-Accurate Wiring Wemos D1 Mini D2 (GPIO4) DS18B20 DATA (yellow); 4.7 kΩ from DATA to 3V3 D1 (GPIO5) Relay module IN1 → aquarium heater D5 (GPIO14) Relay module IN2 → aquarium LED light D6 (GPIO12) SG90 servo signal (orange) → fish feeder 3V3 DS18B20 VDD (red) 5V Relay VCC + servo red wire (from 5 V 2 A adapter) GND DS18B20 black, relay GND, servo brown: common GND Relay contacts (COM / NO) switch the heater and light. Mains wiring must be enclosed and done by a qualified person. Avoid D3, D4 and D8 for these loads: they are boot-strapping pins on the ESP8266.
Pin-accurate connection diagram for the Wemos D1 Mini smart aquarium controller.

Arduino Code

In the Arduino IDE install the ESP8266 board package, select LOLIN(WEMOS) D1 R2 & mini, and install the OneWire and DallasTemperature libraries from the Library Manager. The Servo library comes with the ESP8266 core. Put your own Wi-Fi name and password in place of the placeholders before uploading.

/*
  Wemos D1 Mini Smart Aquarium Controller
  - DS18B20 water temperature probe
  - Relay 1: aquarium heater (thermostat with hysteresis)
  - Relay 2: aquarium LED light (on/off schedule from NTP time)
  - SG90 servo: automatic fish feeder (twice a day + button on web page)
  - Web dashboard on the local Wi-Fi network
  Board: LOLIN(WEMOS) D1 R2 & mini  (ESP8266 core 3.x)
  Libraries: OneWire, DallasTemperature (Servo is included in the ESP8266 core)
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Servo.h>
#include <time.h>

// ---------- Wi-Fi (replace with your own) ----------
const char* WIFI_SSID     = "YOUR_WIFI_SSID";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";

// ---------- Pins ----------
const uint8_t PIN_TEMP   = D2;  // GPIO4  - DS18B20 data (4.7k pull-up to 3V3)
const uint8_t PIN_HEATER = D1;  // GPIO5  - Relay IN1 (heater)
const uint8_t PIN_LIGHT  = D5;  // GPIO14 - Relay IN2 (LED light)
const uint8_t PIN_SERVO  = D6;  // GPIO12 - Servo signal

// Most low-cost relay modules switch ON when IN is LOW. Set false if yours is active-HIGH.
const bool RELAY_ACTIVE_LOW = true;

// ---------- Settings ----------
float targetTemp = 26.0;          // degrees C
const float HYSTERESIS = 0.5;     // heater ON below target-0.5, OFF above target+0.5
const float MAX_SAFE_TEMP = 31.0; // heater forced OFF above this
int lightOnHour  = 8;             // light ON at 08:00
int lightOffHour = 20;            // light OFF at 20:00
const int FEED_HOURS[] = {9, 18}; // feed at 09:00 and 18:00
const int FEED_COUNT = sizeof(FEED_HOURS) / sizeof(FEED_HOURS[0]);
const long GMT_OFFSET_SEC = 19800; // India (IST, UTC+5:30)

OneWire oneWire(PIN_TEMP);
DallasTemperature sensors(&oneWire);
Servo feeder;
ESP8266WebServer server(80);

float waterTemp = NAN;
bool heaterOn = false;
bool lightOn = false;
bool lightManual = false;         // true = user overrode the schedule
int lastFeedDay = -1, lastFeedHour = -1;
String lastFeedTime = "never";
unsigned long lastRead = 0;

void setRelay(uint8_t pin, bool on) {
  digitalWrite(pin, (on ^ RELAY_ACTIVE_LOW) ? HIGH : LOW);
}

String nowString() {
  time_t now = time(nullptr);
  if (now < 100000) return "time not synced";
  struct tm* t = localtime(&now);
  char buf[20];
  snprintf(buf, sizeof(buf), "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec);
  return String(buf);
}

void feedFish() {
  feeder.attach(PIN_SERVO, 500, 2400);
  feeder.write(0);   delay(400);
  feeder.write(120); delay(700);   // tip the food container
  feeder.write(0);   delay(600);
  feeder.detach();                 // stop jitter and save power
  lastFeedTime = nowString();
  Serial.println("Fed the fish at " + lastFeedTime);
}

void readTemperature() {
  sensors.requestTemperatures();
  float t = sensors.getTempCByIndex(0);
  waterTemp = (t == DEVICE_DISCONNECTED_C) ? NAN : t;
}

void controlHeater() {
  if (isnan(waterTemp) || waterTemp >= MAX_SAFE_TEMP) {
    heaterOn = false;                    // fail safe: sensor missing or too hot
  } else if (waterTemp < targetTemp - HYSTERESIS) {
    heaterOn = true;
  } else if (waterTemp > targetTemp + HYSTERESIS) {
    heaterOn = false;
  }
  setRelay(PIN_HEATER, heaterOn);
}

void runSchedule() {
  time_t now = time(nullptr);
  if (now < 100000) return;              // wait for NTP
  struct tm* t = localtime(&now);
  if (!lightManual) {
    lightOn = (t->tm_hour >= lightOnHour && t->tm_hour < lightOffHour);
  }
  // Clear manual override at the next scheduled switch time
  if (t->tm_min == 0 && t->tm_sec < 2 && (t->tm_hour == lightOnHour || t->tm_hour == lightOffHour)) {
    lightManual = false;
  }
  setRelay(PIN_LIGHT, lightOn);
  for (int i = 0; i < FEED_COUNT; i++) {
    if (t->tm_hour == FEED_HOURS[i] && t->tm_min == 0 &&
        !(lastFeedDay == t->tm_yday && lastFeedHour == t->tm_hour)) {
      lastFeedDay = t->tm_yday;
      lastFeedHour = t->tm_hour;
      feedFish();
    }
  }
}

void handleRoot() {
  String html = F("<!DOCTYPE html><html><head><meta name='viewport' content='width=device-width,initial-scale=1'>"
                  "<meta http-equiv='refresh' content='10'><title>Aquarium</title>"
                  "<style>body{font-family:Arial;text-align:center;background:#e3f2fd}"
                  ".c{background:#fff;margin:10px auto;padding:12px;max-width:340px;border-radius:10px}"
                  "a{display:inline-block;margin:4px;padding:10px 14px;background:#1976d2;color:#fff;border-radius:6px;text-decoration:none}</style></head><body>"
                  "<h2>Smart Aquarium</h2>");
  html += "<div class='c'><h3>Water: " + (isnan(waterTemp) ? String("sensor error") : String(waterTemp, 1) + " &deg;C") + "</h3>";
  html += "Target: " + String(targetTemp, 1) + " &deg;C<br>Heater: " + String(heaterOn ? "ON" : "OFF");
  html += "<br><a href='/temp?d=-0.5'>-0.5</a><a href='/temp?d=0.5'>+0.5</a></div>";
  html += "<div class='c'>Light: " + String(lightOn ? "ON" : "OFF") + (lightManual ? " (manual)" : " (schedule)");
  html += "<br><a href='/light?s=1'>Light ON</a><a href='/light?s=0'>Light OFF</a><a href='/light?s=a'>Auto</a></div>";
  html += "<div class='c'>Last feed: " + lastFeedTime + "<br><a href='/feed'>Feed now</a></div>";
  html += "<p>Time: " + nowString() + "</p></body></html>";
  server.send(200, "text/html", html);
}

void redirectHome() { server.sendHeader("Location", "/"); server.send(303); }

void setup() {
  Serial.begin(115200);
  pinMode(PIN_HEATER, OUTPUT);
  pinMode(PIN_LIGHT, OUTPUT);
  setRelay(PIN_HEATER, false);           // everything OFF at boot
  setRelay(PIN_LIGHT, false);
  sensors.begin();

  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to Wi-Fi");
  unsigned long start = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - start < 20000) { delay(500); Serial.print('.'); }
  Serial.println();
  if (WiFi.status() == WL_CONNECTED) Serial.println("Dashboard: http://" + WiFi.localIP().toString());
  else Serial.println("Wi-Fi failed - heater thermostat still works offline");

  configTime(GMT_OFFSET_SEC, 0, "pool.ntp.org", "time.nist.gov");

  server.on("/", handleRoot);
  server.on("/feed", []() { feedFish(); redirectHome(); });
  server.on("/temp", []() {
    targetTemp = constrain(targetTemp + server.arg("d").toFloat(), 20.0, 30.0);
    redirectHome();
  });
  server.on("/light", []() {
    String s = server.arg("s");
    if (s == "a") lightManual = false;
    else { lightManual = true; lightOn = (s == "1"); setRelay(PIN_LIGHT, lightOn); }
    redirectHome();
  });
  server.begin();
}

void loop() {
  server.handleClient();
  if (millis() - lastRead > 2000) {      // read and control every 2 s
    lastRead = millis();
    readTemperature();
    controlHeater();
    runSchedule();
    Serial.printf("Temp %.2f C, heater %s, light %s\n", waterTemp, heaterOn ? "ON" : "OFF", lightOn ? "ON" : "OFF");
  }
}

Setup and Testing

  • Upload the sketch, open the Serial Monitor at 115200 baud and note the dashboard IP address.
  • Test the probe first: hold it in your hand and watch the temperature rise in the Serial Monitor.
  • Test the relays with a small lamp before connecting the real heater.
  • Adjust the servo angle (120°) and the container opening so one feed drops only a small pinch of food.
  • Change lightOnHour, lightOffHour and FEED_HOURS to suit your fish.

Troubleshooting

ProblemFix
Temperature shows sensor error or -127 °CCheck the 4.7 kΩ pull-up between DATA and 3V3 and the DS18B20 wire colours.
Relays work the opposite wayChange RELAY_ACTIVE_LOW in the sketch.
Board resets when the servo movesUse a 2 A adapter and add the 470 µF capacitor across the servo supply.
Light and feeder do not follow the scheduleThe board has no internet time yet. Check Wi-Fi and that the dashboard shows the correct time.
Dashboard does not openMake sure your phone is on the same Wi-Fi network and use the IP from the Serial Monitor.

Safety Note

Aquarium heaters and lights run on 230 V mains. Keep mains wiring inside a closed box, away from water, use relays rated for the load and get help from a qualified electrician. Never put the electronics where water can splash on them.

Ideas to Extend

  • Add a water level float switch to warn when the tank needs topping up.
  • Send a Telegram or email alert when the temperature goes out of range.
  • Add a 0.96 inch OLED to show the temperature on the tank itself.
  • Use a third relay for the air pump or filter with its own schedule.

Leave a Comment