Automatic Water Dispenser using Arduino

Today we are going to make an Automatic water dispenser which will save some water from dripping out from the water dispenser. As we know, About 71% of the earth is cover with water, but sadly only 2.5% of it is drinking water. With the rise in population, pollution, and climate change, it is expecting that by as soon as 2025 we will experience perennial water shortages. On one hand, there are already minor disputes among nations and states for sharing river water on the other hand we as humans waste a lot of drinking water due to our negligence. So in this project, we will learn to make our own Automatic water dispenser System.

In this project, we will build an Automatic Water Dispenser using Arduino and a Solenoid valve that can automatically give you water when a glass is placed near it. Sounds cool right! So let’s build one…

Materials Required

Solenoid Valve

Arduino Uno (any version)

HCSR04 – Ultrasonic Sensor


1k and 10k Resistor


Connecting Wires

Working Concept

The Concept behind the Automatic Water Dispenser is very simple. We will use an HCSR04 Ultrasonic Sensor to check if any object such that the glass is placed before the dispenser. A solenoid valve will be used to control the flow of water, which is when energized the water will flow out, and when re-energized the water will be stopped. So we will write an Arduino program which always checks if any object is placed near the tap, if yes then the solenoid will be turned on and wait till the object is removed, once the object is removed the solenoid will turn off automatically, thus closing the supply of water. Learn more about using the Ultrasonic sensor with Arduino here.

Automatic Water Dispenser using Arduino                                                                    Dispenser using Arduino

Circuit Diagram for Automatic water Dispenser Project

The complete circuit diagram for Arduino based water Dispenser is shown below

Automatic Water Dispenser using Arduino

The solenoid valve used in this project is a 12V valve with a maximum current rating of 1.2A and a continuous current rating of 700mA. That is when the Valve is turned on it will consume about 700mA to keep the valve turned on. As we know an Arduino is a Development board that operates with 5V and hence we need a switching driver circuit for the Solenoid to turn it on and off.

The switching device used in this project is the IRF540N N-Channel MOSFET. It has the 3 pins Gate, Source and Drain from pin 1 respectively. As shown in the circuit diagram the positive terminal of the solenoid is powered with the Vin pin of the Arduino. Because we will use a 12V adapter to power the Arduino and thus the Vin pin will output 12V which can be used to control the Solenoid.

The negative terminal of the solenoid is connected to the ground through the MOSFET’s Source and Drain pins. So the solenoid will be powered only if the MOSFET is turned on.

The gate pin of the MOSFET is used to turn it on or off. It will remain off if the gate pin is grounded and will turn on if a gate voltage is applied. To keep the MOSFET turned off when no voltage is applied to the gate pin, the gate pin is pulled to the ground through a 10k resistor. The Arduino pin 12 is used to turn on or off the MOSFET, so the D12 pin is connected to the gate pin through a 1K resistor. This 1K resistor is used for the current limiting purpose.

The Ultrasonic Sensor is powered by the +5V and ground pins of the Arduino. The Echo and Trigger pin is connected to pin 8 and pin 9 respectively. We can then program the Arduino to use the Ultrasonic sensor to measure the distance and turn on the MOSFET when an object is detected. The whole circuit is simple and hence can be easily built on top of a breadboard. Mine looked something like this below after making the connections.

Automatic Water Dispenser using Arduino

Programming the Arduino Board

For this project, we have to write a program that uses the HCSR-04 Ultrasonic sensor to measure the distance of the object in front of it. When the distance is less than 10cm we have to turn on the MOSFET and else we have to turn off the MOSFET. We will also use the onboard LED connected to pin 13 and toggle it along with the MOSFET so that we can ensure if the MOSFET is turned on or off state.

The complete program to do the same is given at the end of this page. Just below I have explained the program by breaking it into small meaningful snippets.

The program starts with macros definition. We have the trigger and echo pin for the Ultrasonic sensor and the MOSFET gate pin and LED as the I/O for our Arduino. So, we have to define which pin of these will be connected to in our hardware, also we have connected the Echo and Trigger pin to 8 and 9th digital pin respectively. Then, the MOSFET pin is connected to pin 12 and the on-board LED by default is connected to pin 13. We define the same using the following lines

#define trigger 9

#define echo 8

#define LED 13

#define MOSFET 12

Inside the setup function, we declare which pins are input and which are output. In our hardware, only the Echo pin of Ultrasonic(US) sensor is the input pin, and rest all are output pins. So we use the pinMode function of Arduino to specify the same as shown below





Inside the main loop function, we call for the function called measure_distance(). This function uses the US sensor to measure the distance of the object in front of it and updates the value to the variable ‘distance’. To measure distance using the US sensor the trigger pin must firstly be held low for 2 microseconds and then secondly, held high for 10 microseconds and finally held low for 2 microseconds. This will send a sonic blast of Ultrasonic signals into the air which will get reflected by the object in front of it and the echo pin will pick up the signals reflected by it. Then we use the time taken value to calculate the distance of the object ahead of the sensor. If you want to know more about how to interface the HC-SR04 Ultrasonic sensor with Arduino, read through the link. Lets, Calculate the distance by using this program:









Now, Let’s compare the value of distance using a simple if statement and if the value is less than 10cm we make the MOSFET and LED to go high, in the following else statement we make the MOSFET and LED to go low. The program to do the same is shown below.











Arduino Code for Automatic Water Dispenser

HEY… you just made automatic water disperse. now your this new creation will save some water.

(Visited 3,112 times, 1 visits today)

Leave a Reply

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