Elektroda.com
Elektroda.com
X

[OpenBeken] Battery measurement driver based ADC with voltage divider

dheenhasty 1275 10
  • Some IoT devices are powered by electric battery and rely on ADC pin to do the battery measurement.

    The issue is that the voltage value generally exceed voltage reference of Beken ADC pin who is by default 2400mv.

    So to bypass that issue Tuya implement some voltage divider activated by a relay pin. here's an example on CBU:

    [OpenBeken] Battery measurement driver based ADC with voltage divider
    thanks @nelliug54 for the schema :)

    in this example BAT_ADC is on pin 5 and BAT_Relay on pin 20. when relay is activated it will divide the voltage on pin 5 by ~2.3 (it's mostly the case 2 or 3 AAA power sensor) on schema. but in real the divider was 2.29 so the vdivider really need to be adapt on each sensor (depending on the quality of resistor)

    OpenBeken has a driver to capture that and publish the value in voltage and battery mqtt topic of the device.
    If BAT_ADC pin is assign Battery driver will launched automatically and take measurement every 10sec.

    it come with two command :
    "Battery_Setup" : it permit to change the default parameter to calculate battery value.
    - vmin(required) : minimal value accepted by the device in mv (default 2000)
    - vmax(required) : maximal value accepted by the device in mv (default 3000)
    - vdivider(optional) : divider (default 2.29 but to adapt on your sensor if value seems not good)
    - vref(optional) : vref of the ADC pin (default 2400 and static on BK7231)
    - adcbits(optional) : number of bits available in ADC (default 4096 in 99% of case shoud be that)


    "Battery_cycle" <int> : it permit to adjust the number of frame to skip to take measurement (1 frames = 1seconds) (default = 10 )

    On most device the default value should be ok, so just starting the driver and launch a scheduleHADiscovery will do the trick.

    Edit : Change the order of args for battery setup to simplify script implementation (https://github.com/openshwprojects/OpenBK7231T_App/pull/692)

    Edit 2 : Adding BAT pin reference and Autostart

    Cool? Ranking DIY
    About Author
    dheenhasty
    Level 12  
    Offline 
    dheenhasty wrote 102 posts with rating 13, helped 2 times. Been with us since 2023 year.
  • #2
    p.kaczmarek2
    Moderator Smart Home
    @dheenhasty do you think this solution is also used in those door sensors:
    https://www.elektroda.com/rtvforum/topic3959677.html
    https://www.elektroda.pl/rtvforum/viewtopic.php?p=20448552#20448552
    The second one linked seems to have schematic drawn:
    Spoiler:

    [OpenBeken] Battery measurement driver based ADC with voltage divider

    [url=https://obrazki.elektroda.pl/6586709700_1676894176.jpg][OpenBeken] Battery measurement driver based ADC with voltage divider
  • #3
    dheenhasty
    Level 12  
    Certainly on schema we see a transistor on gpio16 so it has a divider.

    A good way to test is to assign ADC to a channel and a rel to gpio16. Then you can try to switch the relay to check the value.

    Then need to check the vref should be 2400 (based on beken documentation) but on cbu seems like it's more 2900....

    For the door detection, i will got with the pindeepsleep, but they will have a 30sec delay for detection..... Depending of the usecase it can be troublesome
  • Helpful post
    #4
    p.kaczmarek2
    Moderator Smart Home
    Well, what about the second one? Those door sensors seems to be different.

    dheenhasty wrote:

    For the door detection, i will got with the pindeepsleep, but they will have a 30sec delay for detection..... Depending of the usecase it can be troublesome

    No worries, it's already solved. It's just that I haven't tested it on N yet, but should work as well?
    https://github.com/openshwprojects/OpenBK7231...mmit/4265f6730384180caa2adda6f7395190f38e75e7
    No commands required.

    One important change that I had to do is to add dInput (and similar) pin handling to pin deep sleep, so it checks the current input state and wakes on transition to separate state.
    [OpenBeken] Battery measurement driver based ADC with voltage divider
    Here is my test setup (the old-school switch, which is older than me, is acting like a door sensors):
    [OpenBeken] Battery measurement driver based ADC with voltage divider
    Measurements:
    [OpenBeken] Battery measurement driver based ADC with voltage divider
    I will be posting guide in separate topic soon.
  • #5
    dheenhasty
    Level 12  
    Ok found the issue with my inconsistency on battery value. i have some bug regarding the battery setup who don't take in account the v_divider.

    Now it's resolve. so BK7231N has effectively a static vref of 2400 (should be the same for the T .....)

    but the v_divider of the Generic Temperature sensor is 2.29 (due to some bad quality resistor i think....)

    i have push a merge with correction for that and correction for the SHT (to activate runframe)
  • #6
    p.kaczmarek2
    Moderator Smart Home
    Yes, something like "SHT30_Auto 0/1" is the correct way, with value 1 by default. Tell me when to merge the changes into the main repository branch.
  • #7
    dheenhasty
    Level 12  
    i have try to respect what i have put in place in Battery driver so talking about cycle.

    so now we have SHT_cycle and Battery_cycle and when it's put to 0 it deactivate the measurement.
  • #8
    p.kaczmarek2
    Moderator Smart Home
    Hm okay may it be, just keep it enabled by default so beginners don't have to write a single line of code to get stuff working.

    Also make sure that HA discovery works for both battery and sensors. It works for my door sensors as well, right now.
  • #9
    dheenhasty
    Level 12  
    Yes, default to 10 seconds for both
  • #10
    p.kaczmarek2
    Moderator Smart Home
    @dheenhasty as far as I know, the battery driver has been updated - can you update the description to include new pin roles and automatic restart?
  • #11
    dheenhasty
    Level 12  
    Ok i have done the modification, waiting for your approval :)