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:
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)
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.
Here is my test setup (the old-school switch, which is older than me, is acting like a door sensors):
I will be posting guide in separate topic soon.
Info:CMD:CMD_StartScript: started autoexec.bat at the beginning
Info:CMD:Battery Setup : Min 2130706432 Max 0 Vref 1084178432 adcbits 0 vdivider 1084715008
Info:CMD:Battery Cycle : Measurement will run every 20 seconds
Info:MAIN:Time 1, idle 278595/s, free 73512, MQTT 0(0), bWifi 0, secondsWithNoPing -1, socks 2/38
Info:DRV:DRV_BATTERY : Measure Battery volt en perc
Info:GEN:CHANNEL_Set channel 0 has changed to 1 (flags 0)
I am working with @DeDaMrAz on smoke sensor:
Here's how the battery measurement is done there:
I had to introduce Bat_Relay_n for this purpose, because here, to do measurement, the P26 has to be low, so R7 and R8 form a voltage divider (divides voltage by two).
So basically it works like it:
- P26 is high by default
- at measurement time, P26 is low, so battery divider works, current flows
- after measurement P26 is high again