Picture of the prototype board

Auto-water fountain

Picture of the prototype board


The prototype water fountain is based on an ultrasonic sensor, NPN transistor and ESP32

Watch the demo below

What is this all about then?

This auto start water fountain was designed for my cat, Huey.  I heard animals prefer to drink from a running stream so I was determined to give him one.

The idea is simple; once Huey approaches, the water fountain turns on for 15 seconds.  

Ultrasonic sensor

I chose an ultrasonic sensor to measure the distance to the nearest object.  It works by sending a high-frequency pulse and waiting for its return.  Distance can be calculated by:

D = 1/2 TC


D is the distance
T is the time in seconds
c is the speed of sound ~ 343 m/s

The HC-SR04 is inexpensive at $1.50 and works up to 13 feet

Other Parts

ESP32 Microcontroller

2N2222A Transistor

4.3k resistor

2-way Quick Connect

5V submersible water pump



Software, C++, Arduino


        Software for an Auto water fountain

        This software is designed to turn on a water pump when an animal approaches the distance sensor.
        It runs for 15 seconds (configurable), then shuts off.

          Arduino framework

#define GPIO_US1_Trig 12
#define GPIO_US1_Recv 13 
#define GPIO_Pump 25
#define GPIO_LevelMeter 36
#define MaxPumpRunTime 15000                    // 15 Seconds
#define CheckUltrasonicSensorEvery 10           // 10 mS
#define DetectionDistance 25                    // 25 cm


Ultrasonic MyUltrasonic1(GPIO_US1_Trig, GPIO_US1_Recv); // An ultrasonic sensor HC-04

void setup()

  pinMode(GPIO_LevelMeter, INPUT);
  digitalWrite(GPIO_Pump,false);          // false=off

long Timer_Status=0;          // When millis() exceeds this, print distance to the serial port
long Timer_PumpOff=0;         // When millis() exceeds this, turn the pump off
long Timer_LookForKitty=0;    // When millis() exceeds this, check the UltraSonic sensor

void loop()
  long Millis = millis();

  // Turn off the pump if it exceeds the timer
  if(Timer_PumpOff && Millis > Timer_PumpOff)
    digitalWrite(GPIO_Pump, false);
    Serial.printf("Turning off pump\n");

  if(Millis > Timer_LookForKitty)
    Timer_LookForKitty = Millis + CheckUltrasonicSensorEvery;

    unsigned int Distance = MyUltrasonic1.read();

    if(Distance <= DetectionDistance)
    {        // Found Kitty
      if(!Timer_PumpOff){                     // Is the pump off? (Haven't Already found kitty)
        Timer_PumpOff = Millis + MaxPumpRunTime;
        digitalWrite(GPIO_Pump, true);
        Serial.printf("Turning ON pump\n");


  // Status Timer controls how often we update the status
  if(Millis > Timer_Status)
    Timer_Status = Millis + 1000;
    //Serial.printf("Dist=%u Water=%d\n",MyUltrasonic1.read(), analogRead(GPIO_LevelMeter));

1 thought on “Auto-water fountain”

Leave a Comment