Page 1 of 1

Arduino Nano + INA219 = cheap current scope

Posted: 24 Jun 2020, 16:40
by martinus
In order to checkout current consumption of the ESP doorsensor mod (using ESPNOW and controlling a servo on batteries) i decided to hookup an INA219 module to an old Arduino Nano module and used the Arduino IDE serial plotter to show exact current measures from boot till shut-down:
ESPNOW_Current.png
ESPNOW_Current.png (36.39 KiB) Viewed 33855 times
This will really aid in verifying changes to the code for further optimization.
As the plotter only shows the last 500 points, i used a timescale of 2 ms/point. so the 500 at the right end means 1000 milliseconds.
The graph shows the 90 mA during normal operation, the high peaks around 350 mA and the radio-off moments that use around 20 mA.

Re: Arduino Nano + INA219 = cheap current scope

Posted: 24 Jun 2020, 17:25
by grovkillen
Wow, looks really good!!

Re: Arduino Nano + INA219 = cheap current scope

Posted: 25 Jun 2020, 11:58
by TD-er
What range have you set for the current?
And are you measuring the current in front of a linear voltage regulator?

Re: Arduino Nano + INA219 = cheap current scope

Posted: 25 Jun 2020, 14:50
by dynamicdave
What a great use for a INA219 - I might try that out with a spare Wemos D1 Mini as the current scope.

Re: Arduino Nano + INA219 = cheap current scope

Posted: 25 Jun 2020, 15:30
by martinus
TD-er wrote: 25 Jun 2020, 11:58 What range have you set for the current?
And are you measuring the current in front of a linear voltage regulator?
The range is set to 400 mA. Measuring the entire setup, so the modded doorsensor with the Arduino Pro Mini and the servo. The INA sensor is between the batteries and the Vbat connection of the doorsensor. It uses a step-up boost converter to provide the ESP and Servo with a constant 3.3 volts.
The Pro Mini is directly powered on VBat as well as the 'tuya' MCU.

I could adapt the lab setup to actually measure the current into the ESP module, but i'm actually only interested in the total current consumption as this will predict possible battery life.

This sample also shows the (slight) servo motion in action:
ESPNOW_Current2.png
ESPNOW_Current2.png (46.24 KiB) Viewed 33813 times
Looks like the start peak current when the servo starts to move is beyond the scale of 400 mA.

Re: Arduino Nano + INA219 = cheap current scope

Posted: 26 Jun 2020, 15:45
by martinus
Almost forgot that the Arduino Nano has digital inputs to mimic a cheap logic analyzer and the Arduino Serial plotter can do multiple lines in the chart.

So i used GPIO-0 on the ESP to toggle at some given points in the rules engine and hooked up to an input on the Nano.
Gives more info on what happens at certain moments:
ESPNOW_Current3.png
ESPNOW_Current3.png (52.88 KiB) Viewed 33760 times
It looks like the sleep command does not do anything to reduce current. This needs some further checking.
Also notice that gpio-0 seems to produce 'noise' during bootloader stage.

Re: Arduino Nano + INA219 = cheap current scope

Posted: 26 Jun 2020, 22:39
by TD-er
About the pin behavior during boot, see: http://rabbithole.wwwdotorg.org/2017/03 ... -gpio.html
Link is also mentioned here: https://espeasy.readthedocs.io/en/lates ... on-esp8266 ;)

And about the power consumption of the ESP.
https://www.youtube.com/watch?v=n_A_8Y4xNx8
That video describes how you can reduce the deep sleep consumption even more, which is probably more important then the power consumption while awake.

You can also prevent the RF calibration during initial WiFi connection, which will shave off the high power peaks and also make the connection faster. (when connecting to WiFi access point. Sending to ESP-now is different)

Re: Arduino Nano + INA219 = cheap current scope

Posted: 27 Jun 2020, 09:25
by martinus
I'm using the LSC doorsensor circuit for my battery devices. So every boot is a cold-boot in this case and it cannot use normal deepsleep.
So i guess some issues cannot be avoided with this type of circuit.

But the sleep current of the entire circuit is now 14 uA which is sufficient. Battery life will mainly be affected by the active-time and the number of wake-ups.
Wake-ups are set to 10 minutes. Active time should be as short as possible and every 100 mS really counts here. I am aiming at <500 mS for my projects.

I've seen graphs from another topic that shows a total active time of 285 mSeconds with custom firmware. I have not been able to do this with ESPEasy and i guess it would take some customization to make the boot process on ESPEasy faster. Maybe we could skip a lot of init code in case were running on batteries.

Re: Arduino Nano + INA219 = cheap current scope

Posted: 27 Jun 2020, 12:18
by TD-er
You can look into the RF calibration (one of the mode parameters when enabling wifi if my memory serves me well) and make sure the receiving end of the ESP-now is running in AP-mode and not in STA-mode.
So make sure you try to send to the AP-MAC of the receiving end.
In AP-mode the radio is not set to sleep and is always listening.
Otherwise you may need to wait for the beacon interval (102.4 ms, so 51.2 ms on average) to send data to the STA interface of the receiving end.

Also make sure to have the peer set using the MAC + channel (preferably channel 1) as soon as you start the esp-now layer and before sending out data.
It may also help to do that as the first steps in the setup() and then load the settings etc. so you give it some delay before sending out a message.

ESP-now also tries to send out the message several times until it receives an acknowledgement.
Typically this is 20 msec retry time with every second attempt at a lower bit rate (to get a better range).

Enabling the serial output for logging may also delay it as the HW buffer may fill up and making the write to serial become blocking until the HW buffer has room again.
In the logging of ESPEasy I added a buffer mechanism to overcome this blocking behavior, but still it may try to flush all before going to sleep again.
You may want to have a look at those functions, preparing the sleep.

Re: Arduino Nano + INA219 = cheap current scope

Posted: 27 Jun 2020, 17:36
by martinus
I am using the AP mac interface on the receiver so that should be ok.

About the sleep, it turns out that it does work like planned. The power-off as assumed in the previous graph was not true. I also measured VCC and used a 4 ms timescale, to find out that the VCC remains active for an additional second. The VCC is controlled by the tuya MCU so we have no control over that.
But during that additional second, the ESP is already into deepsleep and uses very little current.

And it seems that it takes about 100 mS before the sleep actually reduces the current after the ESP.deepSleep() command.
ESPNOW_Current4.png
ESPNOW_Current4.png (54.22 KiB) Viewed 33684 times

Re: Arduino Nano + INA219 = cheap current scope

Posted: 27 Jun 2020, 18:27
by TD-er
Maybe there is a capacitor over the EN-pin (which would make perfect sense) which takes some time to discharge.

Re: Arduino Nano + INA219 = cheap current scope

Posted: 07 Jan 2024, 11:53
by martinus
TD-er wrote: 27 Jun 2020, 18:27 Maybe there is a capacitor over the EN-pin (which would make perfect sense) which takes some time to discharge.
After some years being busy with other thinks in life (like moving to a new house, waiting for renovations to complete, etc), this winter time was convenient to get back to ESP life.
I dug up some hardware from the box in the attic and tried if things still worked and it did after some time. This time i googled on ESP sleep behavior and found out that this option also exists:

system_deep_sleep_instant()

This puts the ESP8266 into sleep without any delay, so when the messages have been send using ESPNOW, the power consumption goes to zero immediatly:
DoorSensorCurrent_3.png
DoorSensorCurrent_3.png (41.73 KiB) Viewed 17587 times
This will improve battery life again. BTW the battery operated LSC sensors are still working after several years. Battery life is at least 6 months.

Re: Arduino Nano + INA219 = cheap current scope

Posted: 07 Jan 2024, 14:52
by TD-er
Welcome back Martinus :)