To display for a device which have discharging and charging capability, the instant and average current in both direction, from 2A to 100uA. Useful for some device using solar cell per example. Can be use standalone (oled display) or connected to Wifi.

Recently, one of my home made solar camera didn t work well. I should be able to get the average consumption of the camera but also the charging average current from the solar panel.
The camera is using a battery 18650 format.

The consumption of the camera is quite wide, from 100uA to 1.5A sometimes.

This wide range impose to use special circuit with some internal GA (Gain adaptor). The Microchip series MCP3421-22-23-24 are 18 bits resolution, can manage the direction (high bit gave the direction) and with an internal gain from 1 to 8.

With a shunt of 20mohm, with the Vref=2.048v, with the GA 1-8, the range is 2A max, 100uA min.

For the mockup, I used an uC and display based on a PIC18F26K80 and a display oled (simulate 2 lines 8 cars) based on driver SSd1306 .

The MCP3422 has 2 channels, I2C communication. I used one channel for the current and one for the battery voltage. In fact, the battery voltage can use the normal ADC (10 to 12 bits) from the uC itself and on the next version, I'll use the MCP3421 with only one channel.

For the mockup, I use a battery 18650 holder to be the main battery and 2 big alligator clips to connect to the original battery holder (from the device we want to measure)..

- I tried to make it compatible MCP3421..3424 but just MCP3422 is tested
- The main subroutine is to measure the current. As we don t know which value it will be, the SW start with the max gain (GA=8) and if the value is the max possible, the GA is decreased then redo the measurement until a " no maximum" value. The bit 17 indicate the sense of the current, charging or discharging
- However, the time for 18 bits measurement is quite long, up around 200ms.
- For the battery voltage, the subroutine use a 12 bits resolution as it will be enough
- To have a display and average in mAh, the time is measure with timer 0. After each sleep instruction, the timer start to count the time and stop before the next sleep instruction
- The SW will go to sleep for 256ms (watchdog timer).
- Every second an average of the previous measurements is done. We keep the total current charge and current discharge separately.
- On the display, several screens:
=> Actual battery voltage and current value (C or D to indicate charging or discharging)
=> Average charging current over the time from the launch of the product
=> Average discharging current over the time from the launch of the product
=> Estimation in mAh, charging and discharging
=> Balance (charging - discharging) in mAh
=> Average charging current only during charging time
=> Average discharging current only during discharging time
=> total time
To save some current (Oled uses 10mA average), the display will go off after 1 min of no button press

All is written in MikroC pro.

This is the current status of this project. Mock working and I found the issue with the defective solar camera...

Next step:
- Real PCB
- Format like 18650 battery.
- The idea is to put the PCB on the battery of the device. To get the GND and separate the + of the battery and the link with the holder (with a thin PCB dual face)
- Use only 1 channel MCP3421, and the ADC from uC for the voltage
- Possibility to connect an external battery to power the uC and the Oled (and have more real measurement). The MCP need to be connected to the main battery because his power supply cannot be different than the input
- Extension to connect an ESP8266 for either send the data to a cloud (like thingspeak) or internal website
- Use of another uC, PIC 18F26K42

=> On going. Schematic and PCB finished and posted (BUT not tested yet)

Update on October 5th, 2021:
- Received and tested the PCB with the 18F26K42
- Hardware wise, all look ok
- Software wise, little bit tricky:
=> The 18F26K42 has embedded I2C full module with DMA feature. The MikroC Pro has special I2C routines for that uC
=> However, it took days to make it work. Need use first the PPS function (confirm the pin location RC3 and RC4) or it will not work. Strange because it was same than the default value after the POR.

Update on October 15th 2021:
- All the features are ok now. Some issues with the schematic and PCB:
=> A capacitor on the output of the 3.3v (for the ESP8266) is missing on the schematic
=> For an unknown reason, the D2 orientation is wrong on the PCB
=> The track between the sensor resistor (20mohm) and the MCP3421 are too small. It is creating some current offset when no current. Need add solder join and external wire
=> The schottky diode for power the PIC and the ESP regulator was not appropriate. I have to replace it with a 2A version..
- Now, the standalone measurement are ok and displayed on the small Oled. The Oled is a 70*40 pixels and the library didn t know this resolution. It works with the 80*40 library (with some ghost graphics on the left of the screen)

Basic SW is ok now, on both 18F and ESP8266:
- The principle used here is the 18F makes every second the current and voltage measurement
- Every fix period, the 18F power up the ESP8266 if it is present (at 18F switch on, there is an automatic detection of the presence or not of the ESP)..
- When the ESP8266 is wake up and ready to receive the measurement, it put high the GPIO0, which is detected by the 18F and send the data through a soft_uart feature
- When the ESP8266  got the data, extract them, it sends to the Thingspeak database. When the transfer is finished, it put low the GPIO0 which notify to the 18F to switch off the power of the ESP

This principle is to save power as much as possible. ESP8266 is using 70mA in normal mode and the deepsleep function cannot work on that module without external wiring.

Demo here:

Still need to implement user choice for the period of data send to the ESP8266.

Update on Nov 5th 2021:

- Found that despite good sunny days (thanks Hong Kong weather), the batteries didn t recharge enough
- In fact, as the module is powered by the same battery as the camera, it is consuming current (and not display in the data). I choose intentionally that the module should measure only the device current.
- With the Wifi option, the ESP8266 is using around 70mA during 30 sec to 1 min. That is enough to completely make the camera current wrong as the original period to send the data over wifi was every 2 min. Roughly, it "cost"  70mA during 1 min (max) every 2 min., so around 35mah. Much more than the camera.
- So, I have to decrease the period to send the data over wifi every 10 min. In that case, the influence is more around 7mAh. . 
- The best solution will be to use independent battery for the module. It is designed with this possibility but miss a recharge function. For the next version.
- Anyway, with this period of 10min, the graph on thingspeak is much better

New version with INA219: (February 2024):

- After all the issues with the MCP, I made a new PCB version with the INA219 which has no limitation regarding input voltage versus power IC voltage. They can be completely different as long as the sensor input voltage is lower than 28v.
- All the software has been adapted, following routine used on previous project (PC battery powerbank)
- INA219 is not so easy to configure in automatic mode (auto calculation of the current) but easier to use in simple way: Read shunt voltage, divided by shunt value and we have the current...
- As the INA219 can also measure the bus voltage, the ADC reading from the uC is no need anymore
- The only issue with this IC is I found an offset shunt voltage even without current flowing. I need to introduce a calibration process to manage this. It is creating 0.5 to 1mA error which is too much for my expectation. Another solution can be to use another version like the INA232 which is more precise, less offset.. 

Project finished and working: March 2024:

After some light modification made on the PCB and some new features added in the SW, all is working well.


Add calibration function, add measure on the internal battery and send to Thingspeak (1 week autonomy with 1500mAh battery).

Finishing a last PCB with IC to recharge the internal battery (when needed)

Finally, I can monitor my outside pollution detector which sometimes does not recharge well...