How to measure charging (PWM) capacitor current?


I want to charge capacitor (1F/5,5V) by my Arduino's Duemilanove PWM output. I have already accomplished that, but now I want to have control over charging current - simply by controlling the PWM.

My problem is getting the charging current value. I'm getting really weird readings, ie. 0.5mA while my (really cheap asian make) multimeter tells me 2.5 - 5mA or I can even get negative numbers (???).

I have everything connected as the image describes

with D9 as PWM charging A0 as capacitor voltage reading (and "after" resistor voltage) A1 to get voltage "before" the resistor (other parts are for discharging the capacitor and are driven by program)

Code is simple

cap_current_value = (analogRead(1) - analogRead(0)) * 1000 * 5 / (float)56 / (float)1024;

I get difference from both reading, which is voltage "lost" on resistor, divide by 1024 and multiply by 5 (both is done to convert to volts), then divide by 56 (ohms - the resistor)and multiply by 1000 (to get mA).

But why do I get such bad result? Right now the capacitor is charging at 3mA and I get reading of 0.263mA from Arduino.

Any help is greatly appreciated.

PWM is a pulsed signal - the voltage before the resistor is pulsed - how do you measure that with analogRead()?

I don't know, I know only theory in this circuit. Could you please guide me or tell me what to look at if I want to get (and modify) charging current?

The problem here is that it will be powered by solar cell (not much power there), so I want to have control over the charging process, so that Arduino has enough power left for itself.

For example you can charge the capacitor with single short pulses - that is something you have under control. So measure the voltage on the cpacitor (after the resistor), make a defined pulse, go to high impedance state, and measure the voltage on the capacitor again, and so on.

You can only control the average charging current with PWM, not the peak charging current. The value of the series resistor resistor determines the peak charging current, which by my calculations will be around 57mA using a series diode and 56 ohm resistor - somewhat more than the 40mA absolute maximum rating of the Arduino output pin.

You can measure the peak charging current using that arrangement, but only if you synchronize the analogRead call with the PWM output.

so I want to have control over the charging process, so that Arduino has enough power left for itself.

So measure the voltage on the solar cell (before arduino). Then simply charge the capacitor with short pulses, and after each pulse do measure the voltage before arduino - if it drops below for example 7V, stop pulsing into the capacitor..

The problem here is that it will be powered by solar cell (not much power there), so I want to have control over the charging process, so that Arduino has enough power left for itself.

I don’t understand what you are trying to achieve. If you have a supercap attached to a solar cell (via a schottky diode)
the best strategy is always going to be to charge the cap whenever possible - that gives the greatest endurance when
the light fails. Or is the solar cell voltage too high? In which case the best strategy is charge the cap whenever its voltage
is below the nominal maximum.

I want to build self-powered solar tracker using my own project (which develops itself on the way). I have 4x 6.5V/100mA solar cells (in fact it looks like they can do 5V/15mA when sunny, but not bright sun).

Considering this low-power input, I need to turn the tracker with motor (20-50mA) and to keep the Arduino alive at the same time. This can be achieved from my point of view only by conserving energy (in this project, I have chosen big capacitor, I don't want batteries). Basically I will be low on power almost all the time, so I have to know how much I can spend on charging.

Schottky is there because I got strange voltage redings while using PWM, this little (and stupid) "cheat" prevents me from reading zero state on PWM output. I will replace it with transistor driven by PWM and connect it to 5V/GND directly so I don't overload Arduino output. Transistor could even possibly allow me to measure the charging current.

Another question, this time really stupid one - Is Arduino capable of running on 4V for long time? (connected via USB port, but without data and with 4V).

Is Arduino capable of running on 4V for long time


If you want to achieve low power consumption, you will need to migrate your design from Arduino Uno to a standalone atmega328p, and program it either via ICSP or using a separate FTDI converter. Otherwise, you won't be able to reduce the power consumption very much.

Thanks Grumpy_Mike. That means I can run the tracker even on cloudy days.

I will try to work on charging the capacitor based on the input voltage drop when charging is on as pito suggested.

MarkT: I want to charge the capacitor whenever it is possible, but I have to be careful, because of the lack of power.

dc42: In fact, this is only fun project "just to know I can do it". But thanks for advice, I will keep it in mind in case I want to do something more energy efficient. The current project is limited only by low-power solar cells (I can buy new, but why, it's for fun only = no more money in it).