Skip to main content

Water Dispenser on an ESP32-S3 with a Touch Screen

Loading...

This is the water dispenser with a flow sensor plugin again, on a different kind of hardware: an ESP32-S3 board with a touch screen instead of a TPP2(G2) with a keypad. As before, a Custom Plugin counts the pulses of the flow sensor and publishes the flow rate and the volume as two ordinary variables. The dispenser around it is now operated by touch: choose a quantity with - and +, touch START, and a pump runs until that quantity has gone through the sensor. The board has no relay of its own, so the project also shows how to switch a pump from a pin of the ESP32-S3 with a relay and a transistor.

In the video, the - button brings the target down from 1.0 L to 0.5 L and START starts the pump. The screen counts the water as it goes through the sensor, and the pump stops by itself at 0.50 L, with the water at the half-liter mark of the jug.

The project targets a LilyGO T-Display-S3 with a touch screen, running Zephyr. In AppBlocks it is set up on the TTGO T8-S3 board, with the 320 x 170 screen and its touch controller added as peripherals. The plugin's code is written for the Zephyr runtime.

Hardware​

You will need:

  • A LilyGO T-Display-S3, the version with the touch screen
  • A YF-S401 water flow sensor
  • A small 12V pump and a 12V supply for it
  • An SPDT relay with a coil that works from 3.3V
  • A 2N2222 transistor and a 1 kΩ resistor
  • Two diodes, for example 1N4007
  • A breadboard and jumper wires

A YF-S401 is a turbine with a magnet and a hall-effect sensor. Water spins the turbine, and the sensor gives one pulse for every fixed amount of water that passes: nominally 98 pulses per second for each liter per minute, which is 5880 pulses per liter. It has three wires: supply, ground and the pulse output.

The circuit on a breadboard, seen from above: the board with its touch screen showing the dispenser, the flow sensor connected with red, black and yellow wires, the transistor with its resistor, and the relay next to the terminal block of the pump.

Why a relay and a transistor​

On a Tibbo Project System the pump is switched by a relay Tibbit. This board only has bare pins, and a pin of the ESP32-S3 cannot switch a pump: it works at 3.3V and gives a few milliamperes, while the pump needs 12V and far more current. Two parts stand between the pin and the pump:

  • An SPDT relay switches the 12V of the pump. A single-pole double-throw relay has one common contact and two others, one closed at rest and one closed while its coil is powered. The pump goes through the common and the normally-open contacts, so it only runs while the coil is powered, and it stays off when the board is not powered. The relay also keeps the 12V circuit of the pump separate from the board.
  • A 2N2222 transistor switches the coil of the relay. Even the coil takes more current than a pin can give, so the pin only drives the base of the transistor, through a 1 kΩ resistor, and the transistor switches the coil between the 3.3V of the board and ground.

A coil pushes back with a voltage spike when it is switched off. A diode across the coil of the relay absorbs it and protects the transistor, and a second one across the pump does the same for the contacts of the relay. Each diode is fitted with its stripe on the positive side, so that it does not conduct in normal use.

Wiring​

The Wiring view of the Hardware tab holds the diagram of the circuit. It is part of the project, so it opens with it.

The wiring diagram: the sensor on the 3V pin, ground and pin 43 of the board; pin 44 going through a 1K resistor to the base of the transistor; the transistor between the coil of the relay and ground; the coil fed from the 3V pin with a diode across it; and the 12V supply reaching the pump through the contacts of the relay, with a diode across the pump.
FromTo
Sensor, supply3.3V pin of the board
Sensor, groundGround pin of the board
Sensor, pulse outputGPIO43
GPIO441 kΩ resistor, then the base of the transistor
TransistorBetween one side of the relay coil and a ground pin of the board
Other side of the relay coil3.3V pin of the board
Diode across the relay coilStripe on the 3.3V side
12V supply, positiveCommon contact of the relay
Normally-open contact of the relayPump, positive
Pump, negative12V supply, negative
Diode across the pumpStripe on the positive side

The sensor is powered from the 3.3V pin, so its pulses stay within what a pin of the ESP32-S3 accepts. The pulse line needs no resistor: the plugin turns on the pull-up of the microcontroller, so it does not float between pulses.

tip

A pump motor is an electrically noisy load, and switching it can put spikes on the pulse line that get counted as water. The two diodes, and sensor wiring kept away from the pump's, prevent this.

Pins​

In the Board view of the Hardware tab, the two pins of the circuit are GPIO Pin peripherals:

The board in the Hardware tab, with labels on its pins: interrupt on pin 43, relay_drive on pin 44, the SDA and SCL lines of i2c0 on pins 18 and 17, and the reset and interrupt lines of touch0 on pins 21 and 16.
PeripheralPinRole
interruptGPIO43Receives the pulses of the sensor. It is picked in the settings of the plugin, which takes care of the rest.
relay_driveGPIO44An output, active high. It comes with a variable of the same name: writing 1 to it starts the pump, 0 stops it.

The screen and its touch controller are built into the board, so there is nothing to wire for them. The project has them as the st77890, touch0 and i2c0 peripherals, along with two output pins that switch on the power and the backlight of the screen.

The Water Flow Sensor​

The sensor is handled by a Custom Plugin that comes with the project. There is nothing to install and no code to write: in the Features tab, Water Flow Sensor has its own page under Custom Services, and is set up like a built-in feature.

The settings of the Water Flow Sensor: Enabled, Pulse Input set to interrupt, Pulses per Liter holding the pulses_per_liter variable, Flow Rate Variable set to FLOW_RATE, Total Variable set to FLOW_TOTAL and Update Period 100 ms.
SettingWhat it isIn this project
Pulse InputThe GPIO Pin that receives the pulses.interrupt, on GPIO43.
Pulses per LiterWhat the sensor gives per liter, as a number or as a variable. It is also the calibration.The pulses_per_liter variable, which belongs to the setting described below.
Flow Rate VariableThe variable of the project that receives the flow rate.FLOW_RATE
Total VariableThe variable of the project that receives the volume.FLOW_TOTAL
Update Period (ms)How often the two variables are updated, from 100 to 60000 milliseconds.100, because the application compares the volume against a target.

What the sensor gives the application​

Everything the sensor measures arrives in two variables of the project. They are ordinary variables, created on the Variables page with the Value(number) data type and then picked in the sensor's settings:

VariableValue
FLOW_RATEThe flow rate, in liters per minute.
FLOW_TOTALThe volume counted since the device started, in liters. Writing a value to it restarts the count from that value.

Both move in steps of 0.01. The names are free: the sensor follows a variable that is renamed. The editor reports an error while one of the two settings is empty, and the project does not build if it holds a variable of another type.

Blocks use them like any other variable. An On Variable Changed block runs each time the value changes, and a Variable Set/Math block that writes 0 to FLOW_TOTAL starts a new count. The dispenser below is built from these two blocks.

To see how the plugin is made, open the Custom Plugins page of the Features tab, which holds its settings and its code.

The dispenser​

The application has two more variables: target_volume, the quantity to pour in liters, and is_pouring, which is 1 while the pump runs. The display is the st77890 peripheral of the Hardware tab. Its screen has the desired quantity with its - and + buttons, the quantity dispensed so far with a status line, and the START, PAUSE and MENU buttons.

The screen as the editor draws it: WATER DISPENSER with a MENU button, DESIRED (L) 1.0 above a minus and a plus button, DISPENSED (L) 0.00 above READY, a green START button and a red PAUSE button.

The flows are in the AppBlocks tab, and in the project embedded at the bottom of this page. Each button of the screen has an On LCD Button block, which runs when the button is touched.

TriggerWhat happens
On BootStops the pump, clears is_pouring and FLOW_TOTAL, then writes the target, 0.00 and READY to the screen.
- touchedWhen idle and above the minimum, takes 0.1 L off the target and redraws it.
+ touchedWhen idle, adds 0.1 L to the target and redraws it.
START touchedWhen idle, sets is_pouring, sets FLOW_TOTAL to 0, starts the pump, then shows 0.00 and POURING....
PAUSE touchedWhen pouring, stops the pump, clears is_pouring and shows PAUSED.
FLOW_TOTAL changedWhen pouring, writes the total to the screen with two decimals. Once it reaches target_volume, stops the pump, clears is_pouring and shows COMPLETED.
MENU touchedRuns Enter LCD Menu, which opens the menu where the pulses per liter are changed.

Starting and stopping the pump is a Variable Set/Math block that writes 1 or 0 to relay_drive. None of these blocks knows about pulses either. They read FLOW_TOTAL like any other variable, and setting it to 0 is enough to restart the count. FLOW_RATE is not used here, but it is updated all the same.

START always begins a new pour: after a PAUSE, the count starts again from zero.

The pump takes a moment to stop and the hose holds some water, so expect the glass to get slightly more than the target.

The Pulses per Liter setting​

The figure of a sensor is configured once per device, so the project keeps it in the Settings feature instead of on the plugin's page:

PropertyValue
Namepulses_per_liter
DescriptionPulses Per Liter
Setting TypeValue(number)
Default Value5880, the nominal figure of the YF-S401
The Settings feature with one setting: Name pulses_per_liter, Description Pulses Per Liter, Setting Type Value(number) and Default Value 5880.

A setting comes with a variable of the same name, and that variable is what the Pulses per Liter field of the plugin holds. The value is kept on the flash disk and loaded at startup, before the sensor starts counting with it.

Two more things put the setting in the user's hands:

  • In the Settings tab of the display, a group named General contains it, with the Text Box control. This is what puts it on the LCD menu.
  • In the AppBlocks tab, the On LCD Button block of the MENU button runs Enter LCD Menu.
The Settings tab of the display: a group named General with one row, whose Connected Setting and Setting Display Name are Pulses Per Liter, UI Control Type Text Box and Edit Mode Read/Write.

On the device, touching MENU opens the menu over the dispenser. The menu has a single group, so its setting is directly on the root page, which has two rows: Pulses Per Liter with its current value, and Reboot device. On a touch screen a row is opened by touching it. The four buttons of the bar at the bottom do the same job as the four keys of a keypad: up, down, select and close.

The root page of the LCD menu, titled Settings: Pulses Per Liter selected with the value 5880, then Reboot device. The bar at the bottom has four buttons: up, down, select and close.

Touching Pulses Per Liter opens a number pad. Type the new value, then touch the check mark to store it. The key under it deletes the last character, and the cross in the corner cancels.

The editor for Pulses Per Liter: the value 5880 in a text box above a number pad with the digits, a decimal point, a check mark and a delete key. A cross in the top left corner closes it.

The new value is saved at once, but the sensor took its figure at startup. Touch Reboot device to restart with the new one.

The Reboot page of the LCD menu: Yes, reboot now, then No, go back selected, and the note Saved settings are applied during startup.
note

While the menu is open it covers the screen, so the buttons of the dispenser cannot be touched, PAUSE included. A pour that is under way still ends at its target, but open the menu when nothing is pouring.

Calibration​

5880 is the nominal figure for a YF-S401, and sensors differ from one unit to the next. To calibrate yours, pour a known volume and compare it with what the screen reports:

new Pulses per Liter = current value × liters reported ÷ liters really poured

Enter the result in Pulses Per Liter on the LCD menu, then reboot the device from the same menu. Nothing is rebuilt or uploaded, and each device keeps its own figure.

The plugin is not tied to this model. Any sensor with a pulse output works with its own figure, for example about 450 pulses per liter for a YF-S201.

Using the plugin in another project​

The plugin is not tied to this board either: its pulse input is a GPIO Pin, which any board has. The Import/Export tab of the Custom Plugins page gives the plugins of this project as JSON, and imports them into another project, where they replace the plugins already there. The Water Flow Sensor then appears under Custom Services. To set it up:

  1. On the Variables page, add two variables of type Value(number), one for the flow rate and one for the volume.
  2. In the Hardware tab, add a GPIO Pin on the pin that receives the pulses. Its other settings do not matter: the sensor takes the pin over, as an input with its pull-up on.
  3. On the Water Flow Sensor page, enable the sensor, then pick that pin and the two variables.
  4. Enter the pulses per liter of your sensor, as a number or, as in this project, as the variable of a setting.

The two variables are then ready to be used in blocks.