艺术
美容与健康
工艺
文化与历史
娱乐
环境
食品与饮料
逆向工程
科学
体育
技术
可穿戴设备
ESP32 Self-Watering Pot Controller
Ed

创建者

Ed

16. 十一月 2026FI
21
0
0
0
0

ESP32 Self-Watering Pot Controller

A tank unit that sits next to one houseplant pot. An ESP32 reads a capacitive soil-moisture sensor in the pot and an ultrasonic sensor looking down into a 1.7 L printed water tank. It shows soil moisture % and tank level % on a 0.96 in OLED and runs a small 5 V submersible pump through a logic-level MOSFET when the soil is dry and the tank is not empty. Powered from USB-C 5 V.

说明

1

Circuit board

加载 KiCanvas 查看器...

专业 PCB 设计查看器

2

Schematic

加载 KiCanvas 查看器...

专业 PCB 设计查看器

3

Print the enclosure base

设计文件STL71.6 KBCC0 - Free
4

Print the enclosure lid

设计文件STL79.1 KBCC0 - Free
5

Print the button plunger

设计文件STL16.3 KBCC0 - Free
6

Print the water tank

设计文件STL1.4 KBCC0 - Free
7

Print the tank lid with enclosure seat

设计文件STL46.5 KBCC0 - Free
8

Print the tube stake for the pot

设计文件STL32.2 KBCC0 - Free
9

Firmware

firmware.inoarduino
// Self-watering pot controller - ESP32 DevKit (38-pin) carrier board
// Soil: capacitive v1.2 on GPIO34 (ADC1). Tank: HC-SR04 TRIG=GPIO5, ECHO=GPIO18 (via 1k/2k divider).
// OLED SSD1306 128x64 I2C @0x3C on SDA=21 SCL=22. Pump MOSFET gate GPIO25. Button GPIO27 to GND.
// Button: short press = force a watering cycle, hold 3 s = calibrate soil sensor (dry / wet).
#include <Arduino.h>
#include <Wire.h>
#include <Preferences.h>

// ---------------- pins ----------------
const int PIN_SOIL = 34;
const int PIN_TRIG = 5;
const int PIN_ECHO = 18;
const int PIN_PUMP = 25;
const int PIN_BTN  = 27;
const int PIN_SDA  = 21;
const int PIN_SCL  = 22;

// ---------------- settings (adjust for your pot / tank) ----------------
const float TANK_EMPTY_CM   = 20.0;  // sensor-to-water distance when tank is empty
const float TANK_FULL_CM    = 4.0;   // sensor-to-water distance when tank is full
const int   SOIL_DRY_PCT    = 35;    // start watering below this moisture %
const int   TANK_MIN_PCT    = 10;    // never pump below this tank level %
const uint32_t PUMP_RUN_MS  = 5000;  // one watering shot
const uint32_t SOAK_MS      = 10UL * 60UL * 1000UL; // wait for water to soak in before next automatic shot
const uint32_t MEASURE_MS   = 1000;

Preferences prefs;
int calDry = 3000;   // raw ADC in air (dry)
int calWet = 1300;   // raw ADC in water (wet)

// ---------------- minimal SSD1306 driver ----------------
const uint8_t OLED_ADDR = 0x3C;
uint8_t fb[1024];
bool oledOk = false;

static const uint8_t FONT[] PROGMEM = {
0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x5F,0x00,0x00, 0x00,0x07,0x00,0x07,0x00, 0x14,0x7F,0x14,0x7F,0x14,
0x24,0x2A,0x7F,0x2A,0x12, 0x23,0x13,0x08,0x64,0x62, 0x36,0x49,0x56,0x20,0x50, 0x00,0x08,0x07,0x03,0x00,
0x00,0x1C,0x22,0x41,0x00, 0x00,0x41,0x22,0x1C,0x00, 0x2A,0x1C,0x7F,0x1C,0x2A, 0x08,0x08,0x3E,0x08,0x08,
0x00,0x80,0x70,0x30,0x00, 0x08,0x08,0x08,0x08,0x08, 0x00,0x00,0x60,0x60,0x00, 0x20,0x10,0x08,0x04,0x02,
0x3E,0x51,0x49,0x45,0x3E, 0x00,0x42,0x7F,0x40,0x00, 0x72,0x49,0x49,0x49,0x46, 0x21,0x41,0x49,0x4D,0x33,
0x18,0x14,0x12,0x7F,0x10, 0x27,0x45,0x45,0x45,0x39, 0x3C,0x4A,0x49,0x49,0x31, 0x41,0x21,0x11,0x09,0x07,
0x36,0x49,0x49,0x49,0x36, 0x46,0x49,0x49,0x29,0x1E, 0x00,0x00,0x14,0x00,0x00, 0x00,0x40,0x34,0x00,0x00,
0x00,0x08,0x14,0x22,0x41, 0x14,0x14,0x14,0x14,0x14, 0x00,0x41,0x22,0x14,0x08, 0x02,0x01,0x59,0x09,0x06,
0x3E,0x41,0x5D,0x59,0x4E, 0x7C,0x12,0x11,0x12,0x7C, 0x7F,0x49,0x49,0x49,0x36, 0x3E,0x41,0x41,0x41,0x22,
0x7F,0x41,0x41,0x41,0x3E, 0x7F,0x49,0x49,0x49,0x41, 0x7F,0x09,0x09,0x09,0x01, 0x3E,0x41,0x41,0x51,0x73,
0x7F,0x08,0x08,0x08,0x7F, 0x00,0x41,0x7F,0x41,0x00, 0x20,0x40,0x41,0x3F,0x01, 0x7F,0x08,0x14,0x22,0x41,
0x7F,0x40,0x40,0x40,0x40, 0x7F,0x02,0x1C,0x02,0x7F, 0x7F,0x04,0x08,0x10,0x7F, 0x3E,0x41,0x41,0x41,0x3E,
0x7F,0x09,0x09,0x09,0x06, 0x3E,0x41,0x51,0x21,0x5E, 0x7F,0x09,0x19,0x29,0x46, 0x26,0x49,0x49,0x49,0x32,
0x03,0x01,0x7F,0x01,0x03, 0x3F,0x40,0x40,0x40,0x3F, 0x1F,0x20,0x40,0x20,0x1F, 0x3F,0x40,0x38,0x40,0x3F,
0x63,0x14,0x08,0x14,0x63, 0x03,0x04,0x78,0x04,0x03, 0x61,0x59,0x49,0x4D,0x43, 0x00,0x7F,0x41,0x41,0x41,
0x02,0x04,0x08,0x10,0x20, 0x00,0x41,0x41,0x41,0x7F, 0x04,0x02,0x01,0x02,0x04, 0x40,0x40,0x40,0x40,0x40,
0x00,0x03,0x07,0x08,0x00, 0x20,0x54,0x54,0x78,0x40, 0x7F,0x28,0x44,0x44,0x38, 0x38,0x44,0x44,0x44,0x28,
0x38,0x44,0x44,0x28,0x7F, 0x38,0x54,0x54,0x54,0x18, 0x00,0x08,0x7E,0x09,0x02, 0x18,0xA4,0xA4,0x9C,0x78,
0x7F,0x08,0x04,0x04,0x78, 0x00,0x44,0x7D,0x40,0x00, 0x20,0x40,0x40,0x3D,0x00, 0x7F,0x10,0x28,0x44,0x00,
0x00,0x41,0x7F,0x40,0x00, 0x7C,0x04,0x78,0x04,0x78, 0x7C,0x08,0x04,0x04,0x78, 0x38,0x44,0x44,0x44,0x38,
0xFC,0x18,0x24,0x24,0x18, 0x18,0x24,0x24,0x18,0xFC, 0x7C,0x08,0x04,0x04,0x08, 0x48,0x54,0x54,0x54,0x24,
0x04,0x04,0x3F,0x44,0x24, 0x3C,0x40,0x40,0x20,0x7C, 0x1C,0x20,0x40,0x20,0x1C, 0x3C,0x40,0x30,0x40,0x3C,
0x44,0x28,0x10,0x28,0x44, 0x4C,0x90,0x90,0x90,0x7C, 0x44,0x64,0x54,0x4C,0x44, 0x00,0x08,0x36,0x41,0x00,
0x00,0x00,0x77,0x00,0x00, 0x00,0x41,0x36,0x08,0x00, 0x02,0x01,0x02,0x04,0x02
};

void oledCmd(uint8_t c) {
  Wire.beginTransmission(OLED_ADDR); Wire.write(0x00); Wire.write(c); Wire.endTransmission();
}

bool oledInit() {
  Wire.beginTransmission(OLED_ADDR);
  if (Wire.endTransmission() != 0) return false;
  const uint8_t init[] = {0xAE,0xD5,0x80,0xA8,0x3F,0xD3,0x00,0x40,0x8D,0x14,0x20,0x00,
                          0xA1,0xC8,0xDA,0x12,0x81,0xCF,0xD9,0xF1,0xDB,0x40,0xA4,0xA6,0x2E,0xAF};
  for (uint8_t c : init) oledCmd(c);
  return true;
}

void oledShow() {
  if (!oledOk) return;
  oledCmd(0x21); oledCmd(0); oledCmd(127);
  oledCmd(0x22); oledCmd(0); oledCmd(7);
  for (int i = 0; i < 1024; i += 16) {
    Wire.beginTransmission(OLED_ADDR);
    Wire.write(0x40);
    Wire.write(&fb[i], 16);
    Wire.endTransmission();
  }
}

void clearFb() { memset(fb, 0, sizeof(fb)); }

void setPixel(int x, int y) {
  if (x < 0 || x > 127 || y < 0 || y > 63) return;
  fb[x + (y / 8) * 128] |= (1 << (y & 7));
}

void drawChar(int x, int y, char c, int s) {
  if (c < 32 || c > 126) c = '?';
  for (int col = 0; col < 5; col++) {
    uint8_t bits = pgm_read_byte(&FONT[(c - 32) * 5 + col]);
    for (int row = 0; row < 8; row++)
      if (bits & (1 << row))
        for (int dx = 0; dx < s; dx++)
          for (int dy = 0; dy < s; dy++) setPixel(x + col * s + dx, y + row * s + dy);
  }
}

void drawText(int x, int y, const char *t, int s = 1) {
  while (*t) { drawChar(x, y, *t++, s); x += 6 * s; }
}

void drawBar(int x, int y, int w, int h, int pct) {
  for (int i = 0; i < w; i++) { setPixel(x + i, y); setPixel(x + i, y + h - 1); }
  for (int j = 0; j < h; j++) { setPixel(x, y + j); setPixel(x + w - 1, y + j); }
  int fill = (w - 4) * constrain(pct, 0, 100) / 100;
  for (int i = 0; i < fill; i++)
    for (int j = 2; j < h - 2; j++) setPixel(x + 2 + i, y + j);
}

// ---------------- sensors ----------------
int readSoilRaw() {
  long sum = 0;
  for (int i = 0; i < 16; i++) { sum += analogRead(PIN_SOIL); delay(2); }
  return sum / 16;
}

int soilPercent(int raw) {
  if (calDry == calWet) return 0;
  long p = (long)(calDry - raw) * 100 / (calDry - calWet);
  return constrain((int)p, 0, 100);
}

float readDistanceCm() {
  float best[5]; int n = 0;
  for (int i = 0; i < 5; i++) {
    digitalWrite(PIN_TRIG, LOW); delayMicroseconds(4);
    digitalWrite(PIN_TRIG, HIGH); delayMicroseconds(10);
    digitalWrite(PIN_TRIG, LOW);
    unsigned long us = pulseIn(PIN_ECHO, HIGH, 30000UL);
    if (us > 0) best[n++] = us / 58.0f;
    delay(30);
  }
  if (n == 0) return -1;
  for (int i = 0; i < n; i++)            // median
    for (int j = i + 1; j < n; j++)
      if (best[j] < best[i]) { float t = best[i]; best[i] = best[j]; best[j] = t; }
  return best[n / 2];
}

int tankPercent(float cm) {
  if (cm < 0) return -1;
  float p = (TANK_EMPTY_CM - cm) * 100.0f / (TANK_EMPTY_CM - TANK_FULL_CM);
  return constrain((int)p, 0, 100);
}

// ---------------- pump ----------------
bool pumpOn = false;
uint32_t pumpStart = 0, lastWater = 0;
bool everWatered = false;

void pump(bool on) {
  pumpOn = on;
  digitalWrite(PIN_PUMP, on ? HIGH : LOW);
  if (on) pumpStart = millis();
  else { lastWater = millis(); everWatered = true; }
}

// ---------------- button ----------------
// returns 0 = nothing, 1 = short press, 2 = long press (>3 s)
int readButton() {
  static bool was = false; static uint32_t t0 = 0; static bool longDone = false;
  bool down = digitalRead(PIN_BTN) == LOW;
  int ev = 0;
  if (down && !was) { t0 = millis(); longDone = false; }
  if (down && !longDone && millis() - t0 > 3000) { ev = 2; longDone = true; }
  if (!down && was && !longDone && millis() - t0 > 40) ev = 1;
  was = down;
  return ev;
}

void waitPress() {
  while (digitalRead(PIN_BTN) == LOW) delay(10);
  delay(50);
  while (digitalRead(PIN_BTN) == HIGH) delay(10);
  delay(50);
  while (digitalRead(PIN_BTN) == LOW) delay(10);
}

void calibrate() {
  pump(false);
  clearFb(); drawText(0, 0, "CALIBRATE SOIL", 1);
  drawText(0, 16, "1) Sensor in DRY air"); drawText(0, 28, "   then press button");
  oledShow(); waitPress();
  int dry = readSoilRaw();
  clearFb(); drawText(0, 0, "CALIBRATE SOIL", 1);
  drawText(0, 16, "2) Sensor in WATER"); drawText(0, 28, "   (up to the line)"); drawText(0, 40, "   then press button");
  oledShow(); waitPress();
  int wet = readSoilRaw();
  clearFb();
  if (dry - wet > 200) {
    calDry = dry; calWet = wet;
    prefs.putInt("dry", calDry); prefs.putInt("wet", calWet);
    drawText(0, 16, "Saved."); 
  } else {
    drawText(0, 16, "Bad readings,"); drawText(0, 28, "not saved.");
  }
  char b[32]; snprintf(b, sizeof(b), "dry=%d wet=%d", dry, wet); drawText(0, 44, b);
  oledShow(); delay(2500);
}

void setup() {
  pinMode(PIN_PUMP, OUTPUT); digitalWrite(PIN_PUMP, LOW);
  pinMode(PIN_TRIG, OUTPUT); digitalWrite(PIN_TRIG, LOW);
  pinMode(PIN_ECHO, INPUT);
  pinMode(PIN_BTN, INPUT_PULLUP);
  Serial.begin(115200);
  analogReadResolution(12);
  analogSetPinAttenuation(PIN_SOIL, ADC_11db);
  prefs.begin("plantpot", false);
  calDry = prefs.getInt("dry", calDry);
  calWet = prefs.getInt("wet", calWet);
  Wire.begin(PIN_SDA, PIN_SCL, 400000);
  oledOk = oledInit();
  clearFb(); drawText(10, 24, "PlantPot", 2); oledShow();
  delay(1000);
}

void loop() {
  static uint32_t lastMeas = 0;
  static int soil = 0, tank = -1, raw = 0;
  static float dist = -1;
  static bool forceRequest = false;

  int ev = readButton();
  if (ev == 2) { calibrate(); lastMeas = 0; }
  if (ev == 1) forceRequest = true;

  // pump safety timeout
  if (pumpOn && millis() - pumpStart >= PUMP_RUN_MS) pump(false);

  if (millis() - lastMeas >= MEASURE_MS || lastMeas == 0) {
    lastMeas = millis();
    raw = readSoilRaw();
    soil = soilPercent(raw);
    dist = readDistanceCm();
    tank = tankPercent(dist);
    bool tankOk = tank >= TANK_MIN_PCT;          // unknown (-1) counts as empty
    if (pumpOn && !tankOk) pump(false);

    if (!pumpOn && tankOk) {
      bool soaked = !everWatered || millis() - lastWater >= SOAK_MS;
      if (forceRequest || (soil < SOIL_DRY_PCT && soaked)) pump(true);
    }
    forceRequest = false;

    Serial.printf("soil raw=%d %d%%  dist=%.1fcm tank=%d%%  pump=%d\n", raw, soil, dist, tank, pumpOn);

    clearFb();
    char b[24];
    drawText(0, 0, "SOIL");
    snprintf(b, sizeof(b), "%3d%%", soil); drawText(70, 0, b, 2);
    drawBar(0, 18, 128, 8, soil);
    drawText(0, 32, "TANK");
    if (tank < 0) snprintf(b, sizeof(b), " ?? "); else snprintf(b, sizeof(b), "%3d%%", tank);
    drawText(70, 32, b, 2);
    drawBar(0, 48, 128, 8, tank < 0 ? 0 : tank);
    if (pumpOn) drawText(30, 0, "PUMP");
    else if (!tankOk) drawText(30, 0, "FILL!");
    oledShow();
  }
  delay(10);
}
10

Check board and parts

Check the bare PCB for shorts between +5V and GND with a multimeter. Measure the ESP32 dev board row spacing: the sockets are 25.4 mm apart.

此步骤所需材料:

印制电路板印制电路板1 pcs

所需工具:

万用表万用表
11

Solder SMD parts

Solder R1 and R2 (5.1k, CC pull-downs), R3 (100R gate), R4 (100k gate pull-down), R5 (1k), R6 (2k) and C2 (100 nF), all 0805. Then solder the USB-C receptacle J3.

此步骤所需材料:

贴片电阻贴片电阻6 pcs
贴片陶瓷电容贴片陶瓷电容1 pcs
USB-C 母座USB-C 母座1 pcs
焊锡丝焊锡丝5 克

所需工具:

电烙铁电烙铁
12

Solder through-hole parts

Solder D1 (1N5819, band to +5V), Q1 (IRLZ44N), C1 (470 uF 10 V, mind polarity), J4 pump terminal, J5 and J6 JST-XH headers, J7 OLED header, SW1 and the two 1x19 female sockets.

此步骤所需材料:

二极管二极管1 pcs
IRLZ44N 逻辑电平 MOSFETIRLZ44N 逻辑电平 MOSFET1 pcs
电解电容器电解电容器1 pcs
螺钉接线端子螺钉接线端子1 pcs
JST-XH 板端排针JST-XH 板端排针2 pcs
排针排针3 pcs
按钮按钮1 pcs
焊锡丝焊锡丝10 克

所需工具:

电烙铁电烙铁
13

Check rails and fit ESP32

Power the board through USB-C from a bench supply set to 5 V, 0.3 A limit, without the ESP32. Check 5 V on the DevKit 5V socket pin and on J4 pin 1. Then plug in the ESP32 dev board with the antenna toward the SOIL edge.

此步骤所需材料:

ESP32 开发板ESP32 开发板1 pcs

所需工具:

直流电源直流电源
万用表万用表
14

Fit board into enclosure base

Set the controller board on the base standoffs with USB-C and the pump terminal at their openings. Fix with four M3 x 6 self-tapping screws.

此步骤所需材料:

自攻螺钉自攻螺钉4 pcs

所需工具:

螺丝刀套装螺丝刀套装
15

Mount OLED in enclosure lid

Screw the OLED to the four lid posts with four M2 x 5 self-tapping screws (check the 23 x 23.5 mm hole pattern). Connect it to J7 with a 4-wire female jumper: GND, VCC, SCL, SDA. Put the plunger in the button hole.

此步骤所需材料:

OLED显示屏OLED显示屏1 pcs
自攻螺钉自攻螺钉4 pcs
跳线跳线4 pcs

所需工具:

螺丝刀套装螺丝刀套装
16

Mount pump and tank sensor

Set the pump on the tank floor. Pass its leads through the 6 mm lid hole and the tube through the 9.5 mm hole. Press the HC-SR04 transducers into the two 17 mm lid holes, facing down, and fix the sensor with hot glue.

此步骤所需材料:

潜水泵潜水泵1 pcs
硅胶管硅胶管1 m
超声波测距传感器超声波测距传感器1 pcs
热熔胶棒热熔胶棒1 pcs

所需工具:

热熔胶枪热熔胶枪
17

Wire pump and sensors

Extend the pump leads with hookup wire if needed, solder and cover with heat shrink. Pump + to J4 pin 1, pump - to J4 pin 2. Solder a 4-pin JST-XH cable to the HC-SR04 (VCC, TRIG, ECHO, GND) and a 3-pin cable to the soil sensor (GND, VCC, AOUT). Plug into J6 and J5.

此步骤所需材料:

连接导线连接导线1 m
热缩管热缩管10 厘米
电容式土壤湿度传感器电容式土壤湿度传感器1 pcs
JST-XH 线JST-XH 线2 pcs
焊锡丝焊锡丝3 克

所需工具:

电烙铁电烙铁
剥线钳剥线钳
螺丝刀套装螺丝刀套装
18

Flash firmware

With USB-C power unplugged, connect the DevKit micro-USB through the base slot to a computer. Set TANK_EMPTY_CM and TANK_FULL_CM for this tank, then upload firmware.ino for board esp32:esp32:esp32 with the Arduino IDE. Snap the enclosure lid on.

所需工具:

装有 Arduino IDE 的电脑装有 Arduino IDE 的电脑
USB 数据线USB 数据线
19

Leak-test the tank

Fill the printed tank with water and leave it on paper towel for 24 h. If it seeps, reprint with more walls or seal the inside before use.

所需工具:

量杯壶量杯壶
厨房纸巾厨房纸巾
水水
20

Set up, calibrate and test

Seat the lid on the tank and the enclosure in the lid frame. Push the soil sensor into the pot up to its line and clip the tube end into the stake. Power from the USB-C adapter. Hold the button 3 s for dry/wet calibration. Press briefly to water and check flow, readings and pump current (under 0.8 A).

此步骤所需材料:

USB-C 电源适配器USB-C 电源适配器1 pcs
USB-C 线USB-C 线1 pcs

所需工具:

万用表万用表
量杯壶量杯壶
花盆花盆
水水

材料

26

所需工具

12

设计文件

6
  • cus_ai_ed_electronics_01-pcb-prodjob_01M7JE41V7S9SQK1HV75AC433D-pcb-1-3cacf52a.stlSTLCC0
    71.6 KB
  • cus_ai_ed_electronics_01-pcb-prodjob_01M7JE41V7S9SQK1HV75AC433D-pcb-2-4c75c494.stlSTLCC0
    79.1 KB
  • cus_ai_ed_electronics_01-pcb-prodjob_01M7JE41V7S9SQK1HV75AC433D-pcb-3-47b31554.stlSTLCC0
    16.3 KB
  • cus_ai_ed_electronics_01-product-prodjob_01M7JE41V7S9SQK1HV75AC433D-1-8095aebb.stlSTLCC0
    1.4 KB
  • cus_ai_ed_electronics_01-product-prodjob_01M7JE41V7S9SQK1HV75AC433D-2-e010c2f2.stlSTLCC0
    46.5 KB
  • cus_ai_ed_electronics_01-product-prodjob_01M7JE41V7S9SQK1HV75AC433D-3-c5688cb5.stlSTLCC0
    32.2 KB

相关蓝图

这些蓝图共享知识——技术、材料或原理

CC0 公共领域

此蓝图以 CC0 协议发布。你可以自由复制、修改、分发和使用此作品,无需征得许可。

通过购买蓝图中的产品支持创客,他们将获得 创客佣金 (由供应商设定),或创建此蓝图的新版本并将其作为连接包含在你自己的蓝图中以分享收入。

讨论

(0)

登录 加入讨论

加载评论中...