A custom ESPHome sensor for Home Assistant.
Description • Hardware Components • How To Use • Download • Credits • Related •
This sensor uses an ESP32 board that supports battery power/charging. The ESP32 also measures the battery voltage via an analog pin, through a voltage divider. The voltage divired is needed because the maximum voltage of the battery is 4.2V while the ADC of the ESP32 can measure only up to 1.1V. The oil level measurement is done with a waterproof ultrasonic sensor and its module pcb. However, you can probably use a regular ultrasonic sensor. The electronics were mounted inside a custom enclosure that was designed to screw on the lid of my oil tank.
This sensor works using an Ultrasonic sensor, to measure the level of a liquid in a tank.
In this particular case, the sensor is placed at the top of the tank, pointing down on the surface of the liquid.
This way, we can measure the distance of the liquid from the top of the tank, and, knowing the dimensions of the tank, we can calculate its volume.
Specifically, we measure the volume of the remaining liquid using the following formula:
where maxVolume is calculated from the dimensions of the tank (Length, Width, Height), and measuredVolume is calculated using the following formula:
Similarly, the consumed liquid can be calculated using the following formula:
- ESP wakes up for 60 seconds
- Connects to Home Assistant
- Samples 17 times the oil surface distance and calculates an average
- Samples the battery voltage and converts it to percentage
- Calculates and updates the consumed oil volume and the remaining oil volume
- Falls to deep sleep for 29 minutes
- ESP32 board with battery charging capability
- Li-Ion battery
- Waterproof Ultrasonic sensor
- 2 resistors for measuring the battery voltage
You need to have a Home Assistant server with ESPHome plugin working. If so, follow these steps:
- Connect the hardware components according to the schematic
- Download the YAML code for this sensor and make the required changes in the parameters according to your use-case
- Create a new sensor in your ESPHome dashboard
- Copy your customized YAML code to the sensor's YAML code
- Download and 3D print the electronics enclosure, or design your own according to your needs.
This software uses the following platforms:
You can find similar code in the official Ultrasonic sensor implementation page of ESPHome Ultrasonic sensor
GitHub @frapais · Instagram @worked_first_time