Dear,
I made simple temperature and humidity sensor using xiao ESP32-C3 dev board, DHT-22 sensor and lipo 2000 mAh battery.
I am using home assistant and programing was done trough esp Home app.
Battery consumption is crazy high it drains out the battery in 5 days.
Can you please help me by marking the issues I made? Here is the code:
substitutions:
name: esphome-web-8982f0
friendly_name: Temperature Sensor
esphome:
name: ${name}
friendly_name: ${friendly_name}
min_version: 2024.6.0
name_add_mac_suffix: false
project:
name: esphome.web
version: dev
esp32:
board: esp32-c3-devkitm-1
framework:
type: arduino
…# Enable logging
logger:
level: NONE
…# Enable Home Assistant API
api:
…# Allow Over-The-Air updates (disabled for power consumption)
#ota:
#- platform: esphome
wifi:
ssid: xxxxx
password: xxxxx
fast_connect: true
power_save_mode: HIGH # Enable Wi-Fi power-saving mode
output_power: 8.5dBm # Reduce Wi-Fi output power (default is around 20dBm)
manual_ip:
static_ip: 192.168.XXX.XXX
gateway: 192.168.XXX.XXX
subnet: 255.255.255.0
dashboard_import:
package_import_url: github://esphome/firmware/esphome-web/esp32c3.yaml@main
import_full_config: true
deep_sleep:
sleep_duration: 60s
id: deep_sleep_1
sensor:
-
platform: dht
pin: GPIO6
model: AUTO_DETECT
temperature:
name: “Living Room Temperature”
filters:
- offset: -2
humidity:
name: “Living Room Humidity”
update_interval: 120s
-
platform: adc
pin: GPIO2
name: “Analog Sensor Value”
update_interval: 120s # Update interval in seconds
attenuation: 11db # Optional: Used for ESP32 to adjust the ADC range (0-3.6V)
filters:
- multiply: 2 # Convert to voltage
id: battery_voltage
…Battery SOC (State of Charge) Calculation
-
platform: template
name: “Battery SOC”
unit_of_measurement: “%”
update_interval: 120s
lambda: |-
float voltage = id(battery_voltage).state;
if (voltage > 4.2) {
return 100.0; // Fully charged
} else if (voltage > 4.15) {
return 90.0 + (voltage - 4.15) * 200.0; // 90% to 100%
} else if (voltage > 4.00) {
return 80.0 + (voltage - 4.00) * 66.67; // 80% to 90%
} else if (voltage > 3.85) {
return 50.0 + (voltage - 3.85) * 200.0; // 50% to 80%
} else if (voltage > 3.70) {
return 10.0 + (voltage - 3.70) * 266.67; // 10% to 50%
} else if (voltage > 3.50) {
return (voltage - 3.50) * 50.0; // 0% to 10%
} else {
return 0.0; // Deep discharge, 0%
}
Hi there,
SO can you edit your post and add the code to the Code Tags above Click </> and paste it in there, Improves readability and increase the chance you get quality help.
Is this Micropython?
Which BSP are you using to compile with?
C3 is good down to 44ua… Battery calculator HERE
For a baseline … while program is running what is the power draw.
We like to use the PowerProfiler II for real in circuit testing. Check out some of those threads on here.
HTH
GL PJ
1 Like
Sorry, never used this forum before. Didn’t realize trough phone code looked so bad.
So this is the code:
substitutions:
name: esphome-web-8982f0
friendly_name: Temperature Sensor
esphome:
name: ${name}
friendly_name: ${friendly_name}
min_version: 2024.6.0
name_add_mac_suffix: false
project:
name: esphome.web
version: dev
esp32:
board: esp32-c3-devkitm-1
framework:
type: arduino
# Enable logging
logger:
level: NONE
# Enable Home Assistant API
api:
# Allow Over-The-Air updates (disabled for power consumption)
#ota:
#- platform: esphome
wifi:
ssid: XXXXX
password: XXXX
fast_connect: true
power_save_mode: HIGH # Enable Wi-Fi power-saving mode
output_power: 8.5dBm # Reduce Wi-Fi output power (default is around 20dBm)
manual_ip:
static_ip: 192.168.XXX.XXX
gateway: 192.168.XXX.XXX
subnet: 255.255.255.0
deep_sleep:
sleep_duration: 60s
id: deep_sleep_1
sensor:
- platform: dht
pin: GPIO6
model: AUTO_DETECT
temperature:
name: "Living Room Temperature"
filters:
- offset: -2
humidity:
name: "Living Room Humidity"
update_interval: 120s
- platform: adc
pin: GPIO2
name: "Analog Sensor Value"
update_interval: 120s # Update interval in seconds
attenuation: 11db # Optional: Used for ESP32 to adjust the ADC range (0-3.6V)
filters:
- multiply: 2 # Convert to voltage
id: battery_voltage
# Battery SOC (State of Charge) Calculation
- platform: template
name: "Battery SOC"
unit_of_measurement: "%"
update_interval: 120s
lambda: |-
float voltage = id(battery_voltage).state;
if (voltage > 4.2) {
return 100.0; // Fully charged
} else if (voltage > 4.15) {
return 90.0 + (voltage - 4.15) * 200.0; // 90% to 100%
} else if (voltage > 4.00) {
return 80.0 + (voltage - 4.00) * 66.67; // 80% to 90%
} else if (voltage > 3.85) {
return 50.0 + (voltage - 3.85) * 200.0; // 50% to 80%
} else if (voltage > 3.70) {
return 10.0 + (voltage - 3.70) * 266.67; // 10% to 50%
} else if (voltage > 3.50) {
return (voltage - 3.50) * 50.0; // 0% to 10%
} else {
return 0.0; // Deep discharge, 0%
}
Programing was done in Esp Home App, trough yaml configuration. As far as I understood ESP home generates C++ code from yaml.
And this is explanation from chatgpt about compilation:
-
PlatformIO compiles the generated C++ code based on the specified framework (e.g., Arduino for ESP32).
-
It uses appropriate libraries for components like sensors (DHT, ADC) and deep sleep, as well as ESP-IDF or Arduino libraries for ESP32 devices.
-
The result is a binary firmware file (.bin
) that is compatible with the target microcontroller.
I don’t plan to buy current measuring hardware, I would appreciate if someone could help by checking code in details.
Also one detail I didn’t mention is that battery voltage measuring is done trough GPIO, since voltage is to high I made voltage divider with two 220 kOhm resistors.
According to datasheet power consumption in deep sleep should be cca 5uA. Biggest issue I can see is WIFI connectivity. Which could slow down software execution to 2 - 5 seconds.
Hi there,
That looks way better. So a couple things.
start with these:
Features
-
Powerful CPU: ESP32-C3, 32bit RISC-V singlecore processor that operates at up to 160 MHz
-
Complete WiFi subsystem: Complies with IEEE 802.11b/g/n protocol and supports Station mode, SoftAP mode, SoftAP + Station mode, and promiscuous mode
-
Bluetooth LE subsystem: Supports features of Bluetooth 5 and Bluetooth mesh
* Ultra-Low Power: Deep sleep power consumption is about 43μA
-
Better RF performance: External RF antenna included
-
Battery charging chip: Supports lithium battery charge and discharge management
-
Rich on-chip resources: 400KB of SRAM, and 4MB of on-board flash memory
-
Ultra small size: As small as a thumb(21x17.8mm) XIAO series classic form-factor for wearable devices and small projects
-
Reliable security features: Cryptographic hardware accelerators that support AES-128/256, Hash, RSA, HMAC, digital signature and secure boot
-
Rich interfaces: 1xI2C, 1xSPI, 2xUART, 11xGPIO(PWM), 4xADC, 1xJTAG bonding pad interface
- Single-sided components, surface mounting design
No way to get that low (5ua) , lots of folks use the voltage divider to do that.
I don’t know about chatGPT and the aforementioned but you will need to understand how it works for SLEEP and deep sleep. are you using Arduino IDE or Platformio, or ESP-IDF?
How do you measure the power usage? voltmeter in line with Battery pins (is the only true way) The BSP can also affect battery consumption. which BSP are you currently using?
I would suggest reading through the wiki for the Xiao ESP32C3 try some of the low power sleep examples and get a handle on what you are looking AT and FOR it is not rocket science but it takes a process of steps to pull it off correctly.
HTH
GL PJ
BTW the C3 is probably the highest of all the ESP’s in power consumption for sleep , so FYI!
The update_interval
for sensors is set to 120s
, but the device wakes every 60s
due to deep_sleep
. This causes unnecessary wake-ups, consuming more power.
Match the sleep_duration
to the required update rate (e.g., update_interval: 300s
and sleep_duration: 300s
for every 5-minute update).
The DHT22 sensor draws a small but continuous current.
Power the DHT22 sensor through a GPIO pin and turn it off during deep sleep.
You can also make a compact PCB for your setup like this one: ESP32-C3 Module-Based IoT PCB Mini for students - Share Project - PCBWay