Esp8266 D1 mini + TM1651 Battery

hello everyone,
The idea of ​​the project is to create a small box with an esp module and a tm1651 to read the percentage of the battery of my inverter via wifi. The esp will be powered by a usb power supply.

I have been doing some tests for a few weeks without success. I took the TM1651 Battery LED module following the guide . From the sensor of my inverter called “sensor.inverter_battery_percent” I have to represent the relative percentage of the battery through this module. The display continues to remain off, I have 3 esp8266 D1 mini modules and 3 TM1651 thinking they were defective but even replacing them they still do not work.
Home assistant is installed on raspberry pi, with esp I would like to read the percentage in wifi mode.
Where am I going wrong?

esphome:
  name: batterydisplay01
  friendly_name: Battery Display

esp8266:
  board: esp01_1m

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password


ota:
  - platform: esphome

logger:
  level: DEBUG

sensor:
  - platform: homeassistant
    id: battery_percent
    entity_id: sensor.inverter_battery_percent
    internal: true
    on_value:
      then:
        - lambda: |-
            int level = id(battery_percent).state;
            ESP_LOGD("battery_display", "Battery level: %d", level);
            id(tm1651_battery).set_level(level);

tm1651:
  id: tm1651_battery
  clk_pin: GPIO5
  dio_pin: GPIO4

api:

So what is it output of your sensor on your log?
If it’s percent, you should set the display in percent as well

id(tm1651_battery).set_level_percent(level);

1 Like

Hi,
thanks for your interest in my problem.
unfortunately I see very little from the log

[21:54:39][I][safe_mode:041]: Boot seems successful; resetting boot loop counter
nothing more.

I’m following the technical information of the module referring to the esphome link, therefore it makes me understand that this module is supported.
TM1651 Reference

I found various projects on the web with this module often combined with arduino but no reference yaml from which to take inspiration. I looked at the pinout documentation and it doesn’t seem to me that there are particular PINs dedicated to the CLOCK or DIO.

I’m maintaining
clk_pin: GPIO5
dio_pin: GPIO4

Do you think of any other tests I can do?

If you don’t see the sensor from Home Assistant updating in the log, then check the name and that it has a valid value in HA.

Once you see updates coming through, change the code as per @Karosm suggestion.

EDIT: I assume you have added the ESP to the ESPHome integration? Not the add-on - the actual integration.

thank you very much! It works, I don’t see anything from the logs but the display is lit and more or less it seems aligned with the battery.
I’ll leave the yaml working in case it can be useful to someone in my situation.

esphome:
  name: display1
  friendly_name: Display1
  includes:
    - custom_components/battery01/TM1651.h
    - custom_components/battery01/TM1651.cpp

esp8266:
  board: esp01_1m

# Enable logging
logger:
  level: DEBUG

# Enable Home Assistant API
api:
  encryption:
    key: "test"

ota:
  - platform: esphome
    password: "test"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

captive_portal:
##########################################
tm1651:
  id: tm1651_battery
  clk_pin: GPIO5
  dio_pin: GPIO4

sensor:
  - platform: homeassistant
    id: battery_percent
    entity_id: sensor.inverter_battery_percent #(My reference sensor)
    internal: true
    on_value:
      then:
        - lambda: |-
            int level = id(battery_percent).state;
            ESP_LOGD("battery_display", "Battery level: %d", level);
            id(tm1651_battery).set_level_percent(level);

I’ll try to update you tomorrow when the battery changes value

UPDATE:
This morning after leaving the esp on all night I can view the logs with the battery values.
currently the module works and is aligned with the real percentage of the battery (40% with 4 segments on) using id(tm1651_battery).set_level_percent(level);

[08:23:27][D][homeassistant.sensor:024]: 'sensor.inverter_battery_percent': Got state 39.00
[08:23:27][D][sensor:093]: 'battery_percent': Sending state 39.00000  with 1 decimals of accuracy
[08:23:27][D][battery_display:052]: Battery level: 39

The ideal for me is that when the battery is at 42% the green LED that corresponds to segment 6 lights up. With the battery at 40% the string used so far correctly lights up 2 red LEDs + 2 orange ones (the orange ones are 3 in total).

So to decide on a controlled ignition I was experimenting with this code that should light up segment 6 as soon as the battery reaches the value =>42 but it continues to light 7 segments. I played with the various thresholds but without success.

    id(tm1651_battery).set_level(10);
} else if (level >= 80) {
    id(tm1651_battery).set_level(9);
} else if (level >= 70) {
    id(tm1651_battery).set_level(8);
} else if (level >= 60) {
    id(tm1651_battery).set_level(7);
} else if (level >= 50) {
    id(tm1651_battery).set_level(6);
} else if (level >= 40) {
    id(tm1651_battery).set_level(5);
} else if (level >= 30) {
    id(tm1651_battery).set_level(4);
} else if (level >= 20) {
    id(tm1651_battery).set_level(3);
} else if (level >= 10) {
    id(tm1651_battery).set_level(2);
} else {
    id(tm1651_battery).set_level(1);
}```

The component documentation gives levels from 0 to 7.
Try to set corresponding percent instead…

2 Likes

With bitterness I realize only now that some segments are coupled and I am forced to settle for these settings. 2 red segments, 2 green segments and another 2 green segments coupled.
The single segments are only the 3 orange ones and the blue one.
If I had known before I would have built a synoptic with simple LEDs.
Thanks for the tips!

            if (level > 90) { 
                id(tm1651_battery).set_level(7); //blue led
            } else if (level > 69) { 
                id(tm1651_battery).set_level(6); //last 2 green led
            } else if (level > 50) { 
                id(tm1651_battery).set_level(5); // first 2 green led
            } else if (level > 41) { 
                id(tm1651_battery).set_level(4);  //3 Orange
            } else if (level > 35) { 
                id(tm1651_battery).set_level(3);  // 2 Orange
            } else if (level > 30) { 
                id(tm1651_battery).set_level(2);  //1 Orange
            } else if (level > 1) {  
                id(tm1651_battery).set_level(1); //2 red
            } else {
                id(tm1651_battery).set_level(0); //All OFF
            }

I didn’t read the datasheet either, TM1651 is a 7-segment driver…