Monday, December 28, 2015

Arduino: Micro Processing Renewable Energy - Part 2 - Measuring Current

Measuring Current

A microprocessor can't really measure current directly. It can only read voltage. There are many ways to create a voltage that can relate to a current. The most basic being a shunt. A shunt is just a known resistor that is used in the circuit to be measured. When the voltage drop across the resistor is measured, Ohm's Law can be used to solve for current.

V = I x R
I = V/R
V = Voltage
I = Current
R = Resistance

For instance, if you have a 1 ohm resistor in a circuit, and you measure a voltage across the resistor of 3.7 volts, then the current flowing through that resistor is 3.7 amps (3.7 volts / 1 ohm). That resistor would be pretty hot, though. Since power is equal to the current squared times the resistance, that resistor would be emitting 13.69 watts. A typical resistor that you would see on a small circuit board can only hand a quarter watt, so nearly 14 watts would blow a hole through it and shoot flames out its sides. Not a pretty picture.

Burning resistor as seen on
A more appropriate resistor would be like this 25 watt one.

But, that seems like a lot of wasted heat just to check the current. And the resistor actually limits the current slightly. What if we used a resistor with much less resistance? How about a 0.001 ohm? Then, every milivolt we detect across the resistor would equal one amp of current. For wasted heat, that would be great, only 14 mW instead of 14 watts. Wow, 1/1,000th of the original wasted heat.

There would be a problem though. The Arduino only reads 0 to 5 volts and it does it in 1,023 discreet steps. The lowest value it can detect after zero would be 4.9 milivolts or 0.0049 volts. You could only detect 0 amps, 4.9 amps, 9.8 amps, etc. Not a great solution. There are a few ways to compensate for this though; oversampling, op amp, hall effect, or use a different resistor at 0.01 ohm.


There are many scholarly articles written on oversampling, so I won't go into excessive detail here. I'll just cover the basics. In the above example using a 0.001 ohm resistor, we can only detect a change of 4.9 amp increments. This is because we are reading the voltage at a 10-bit resolution. But what if we somehow (magically?) used 14-bit resolution? That would be 16, 383 steps, each step to 5 volts would be equal to 0.0003 volts or 0.3 mV per step.

We can do that through oversampling. We just take multiple samples and average them to come up with a new, more accurate value. For instance, take a light in your house, that either turns on or off. That is 1-bit resolution. If you flick the light off and on and record those readings, you can then average 4 readings to get 2-bit resolution. Values would include 0, 0.33, 0.66, and 1.0. Another way to express that is OFF, 33%, 66%, and ON.

The rule of thumb is to accumulate 4^n 10-bit samples, where n is the desired extra number of bits in the resolution. For example, to get 14-bit resolution, that is 4 more than 10, so 4 to the 4th power would be 256 samples taken, then divide the result by 256. To get 16-bit resolution, take 4 to the 6th power (4,096) samples and divide by 4,096.

This approach works only because there is almost always some RF or electrical noise in the system to make minor variations occur while taking the samples. More info can be found at

Op Amp

Op Amp, or operational amplifier, is an electronic chip that takes an input and amplifies the output a number of times. In the above example of using a 0.001 ohm shunt resistor to measure current, we could just amplify the voltage reading to get something more appropriate to the Arduino. If, for example, 30 amps is the max current we expected, we could make that equal 5 volts to the Arduino input pin. The voltage drop across the resistor would be 30mV. So, we would have to set the Op Amp to amplify the output by 167 times.

Hall Effect

Many times you can buy a hall effect current measuring device that will measure AC and DC current in either direction. This one will measure 30 amps. There is a trick to make this 30 amp version measure much more. For instance, to double its capacity, just place a jumper wire across the terminals to shunt half of the current. You may have to experiment with the thickness and length of wire to use as the bypass, and you would also have to adjust a variable in your Arduino program to get the accuracy where it should be.


The simple solution to the initial problem is to use a 0.01 ohm resistor instead of a 0.001 ohm resistor. Instead of 1 mV being equal to 1 amp, 10 mV equals 1 amp. Since the Arduino can normally read in 5 mV increments, this would be good enough to measure current in half amp increments. At 3.7 amps, that would be 37 mV. The Arduino could only see 35 mV or 40 mV, so the resolution wouldn't quite be there. Your program would just have to report it as 3.5 amps.

Which solution to use?

Well, they all have their place depending on the scenario. I've used all of the above. One of my favorites is to mix the hall effect with oversampling. Then I add a shunt around the hall effect to up my current capacity. This gives me the best of all worlds, 14-bit accuracy with over 100 amps of measurement on a 30 amp hall sensor budget.

Then Take A Look At This!

Saturday, December 26, 2015

How to Store Lead Acid Batteries for a Very Long Time

Here is some good information on storing lead acid batteries for a long time without having to trickle charge them. I found this on Cody's Lab channel on

Tuesday, December 15, 2015

Arduino: Micro Processing Renewable Energy - Part 1 continued - Reading Voltages


As discussed previously, the Arduino has a 10-bit resolution on its analog read capabilities. That means it can read a voltage between 0 and 5 volts and report a value between 0 and 1,023. If it read with 8-bit resolution, it would generate a value between 0 and 255. If we divide the maximum voltage of 5 volts by the highest resolution value (5/1,023 and 5/255) we get 0.00488 and 0.01960. That number represents the milivolts (mV) for each step in the range. For example, if we have a 8-bit input and we read a value of 134, then that represents 2.6264 volts (134 x 0.01960).

This looks like plenty of resolution. It is obvious from the above examples that 10-bit has 4 times the resolution as 8-bit, but how do you tell if you have enough resolution? Well, it depends on your application. If you are trying to detect if voltage is present, then 1-bit would be enough. That bit could be a 0 or a 1, ON or OFF. But, what if you needed something more precise?

For example, you are trying to charge LiFePo4 batteries and you want to stop charging at exactly 3.65 volts. You definitely want to tell the difference between 3.64 and 3.66 volts to do that. In other words, you need 1/100th of a volt resolution, or 10 mV resolution. If we are using an 8-bit analog input, then the first step after zero volts would be 0.0196 volts, or 19.6 mV. And since 19.6 mV is larger than our minimum requirement of 10 mV, then 8-bits just won't work. But, 0.00488 volts or 4.88 mV would work just fine.

Voltage Range

So, using a 10-bit analog input will give us a resolution of 4.88 mV when we are reading 0 to 5 volts. But, what if we want to read up to 15 volts? If we take 15 volts and divide by 1,023 steps, we get 14.6 mV which is larger than our previous requirement of 10 mV. The problem gets worse with higher ranges. If we tried to read 0 to 60 volts, we would get a final resolution of 58 mV.

There are some tricks you could use to fix your range so it is more tight. For example, when we measure a 12 V battery, we expect a range of about 10 to 15 volts. That is a 5 volt range, it just happens to start 10 volts too high. Alas, there is a way to fix that!

We can just add a 10 volt zener diode to the circuit. It will drop about 10 volts off of the input, resulting in 0 to 5 volts going to the Arduino.

car battery voltage reading

You could also make a circuit like below to read up to a 48 volt battery bank, which could be upwards of 60 volts. In that case, the 40 to 60 volts would be sent through a pot that would be adjusted until 40 volts in would equal 10 volts out. The zener diode would knock off that 10 volts. But, what would the high end of that range be? Since the pot was adjusted to make 40 volts into 10 volts, there is a 4 times reduction in the pot being used as a voltage divider. That means 60 volts is translated to 15 volts. This works perfectly as an input to the 10 volt zener and will give us 0 to 5 volts as the circuits final output.

You also might want a more modular circuit that can take an input from different battery bank sizes. This circuit is meant to measure one battery bank at a time. If you want to measure more than one at a time, duplicate this circuit and apply it to several different Arduino input pins.

Be aware that R4 is only useful when your zener is reverse biased at something less than 10 volts. A typical zener is rated at 5%. That means a 10 volt zener could start passing reverse current at anywhere from 9.5 to 10.5 volts. But, whatever voltage that zener starts at, it will be consistent at a given temperature. For example, if your zener conducts at 9.7 volts, it will always start conducting reverse current at 9.7 volts. So, you could go through a batch and test zeners until you get ones that are the closest to what you want. In this circuit, you should go slightly under 10 volts instead of over. If you have a true 10 volts coming in to R4 and your zener doesn't start conducting until 10.3 volts, no amount of playing with R4 will make up that 0.3 volts.

Some of you are wondering why I chose the 1k resistor (R1) before the zener diode.  Well, I'm assuming a worse case connection is 60 volts into the 12 volt input that has been calibrated for a 12 volt battery. I'll get 50 volts coming out of D4 zener and D2 will redirect 45 volts to ground. And, 45 volts divided by 1000 ohms is 0.045 or 45 mA. The power would be current times volts, or 0.045 amps x 45 volts = 2.025 watts. I'm using a 4 watt zener and I like to limit the power to about half. If you were using a 1 watt, then you would want at least a 4k resistor for R1.

Then Take A Look At This!

Monday, December 14, 2015

Arduino: Micro Processing Renewable Energy - Part 1 - Reading Voltages

Arduino, what the heck is it?

Arduino is a small electronic board containing an easily-programmed microprocessor.
This little board comes in many flavors, but basically allows you to interface with the real world by reading sensors and inputs and controlling things with outputs. I won't get into all the programming ins and outs of the Arduino or Arduino clones in this blog. There are thousands of websites and videos that talk all about it.

But, from this blog's perspective, renewable energy as it pertains to the Arduino isn't talked about that much. There are many questions that the newbie will have such as:

  1. My Arduino only reads up to 5 volts, how can I read a 12 volt battery or even bigger?
  2. How do I measure current?
  3. How do I measure the RPMs of my wind turbine?
  4. How do I control power to larger devices?
These questions will constitute my four part series coming over the next few weeks. Starting with the first question, as this is part 1 of my series, let's discuss measuring voltages.

Part 1 - Reading Voltages

The question is simple, "My Arduino only reads up to 5 volts, how can I read larger voltages?" The answer is simple as well. Use a voltage divider. Well, that sounds simple, but in theory, it takes more explanation.

A voltage divider comes in two types; resistor or capacitor. The capacitor type isn't that relative here because it is used to divide AC voltage. But the resistor type is very relevant. It is used to take a DC input voltage called Voltage In (Vin) and reduce its output voltage called Voltage Out (Vout). Quite simply, it is made up of two resistors in series connected from the positive of the voltage source to the negative. 

Simple voltage divider

As you can see, the current flowing from the Vin connection will be flowing through our voltage divider. Using Ohm's law, we can deduce how much current that will be. 

Current = Voltage / Resistance. 

Assuming 13.5 volts at Vin and 2,000 ohms (R1 + R2), we get 0.00675, or 6.75 mA.

You could figure out power losses as well. 

Power = Current ^ 2 * Resistance

The current through one resistor is the same as the other, so we can use the resistance of one resistor in the equation. This way we solve for power for each resistor. We get 0.0455 watts, or 45.5 mW of power. The small quarter watt resistors can handle up to 250 mW, but I'll tell you now that those two resistors will get fairly hot just sitting there as they use up power from your battery. 

And, what if you had it hooked up to a 48 volt battery bank? Let's say your battery is sitting fully charged at 54 volts. That gives us 27 mA and 729 mW at each resistor. That is three times the ratings for those quarter-watt resistors!

But, what if we used much larger resistors, like 50k instead of 1k resistors? Then we would have 14 mW instead of 729....much cooler. And then we could use a 100k potentiometer (often called a pot) in place of the two resistors. That gives us control on our output voltage. The output would range from 0 volts up to full Vin voltage.

100k Potentiometer used as voltage divider

Just think of the pot as two resistors always adding up to the total rating of the pot. You use one every time you use a volume control or balance knob. If the pot is at its half way mark, then each of the two resistive sections has 50k ohms. If you turn the knob all the way counterclockwise, then the first resistive section would be 0 ohms while the second section would be 100k ohms. The opposite is true if you turn the knob all the way clockwise.

In your  voltage-sensing circuit, it would be best to use a precision trim pot like this one.

These turn about 10 to 20 times for their full range, but you can only adjust them a few times before they start to degrade. It is best to calibrate your circuit once and leave it. Then you could have a smaller valued pot, like 5k ohms, to act as a fine adjustment.

Voltage divider with course and fine adjustment pots
You can and probably should use a pot with higher ohm rating than this example, such as a 1 mega ohm with a 20k fine adjustment.

But, what you do is hook the circuit up to the voltage source such as a battery and use a digital volt meter to measure the output at Vout. Adjust the course pot until your max Vin voltage equals 5 volts at Vout.

Do not hook this up to the Arduino yet. The Arduino can only handle about 5 volts per input and you could easily fry it. Test first!

Getting this could be problematic if you don't have a variable power supply, but it could be done with math. For instance, if your charging / dump circuit will not allow your battery to ever go over 14.5 volts, and when you measure the battery with a meter you get 13.5 volts at the time of the test, then we can use that. We could use a linear ratio such as:

5 / 14.5 = X / 13.5 therefore X = (5 x 13.5) / 14.5 = 4.655 volts
(if using a larger battery, use the appropriate numbers)

The Arduino doesn't say 4.655 volts in its program. It has a 10-bit value between 0 and 1,023. If we take 5 volts and divide it by 1,023, we get 0.00488 volts. Then we could take 4.655 volts and divide by 0.00488 and get about 954. In other words, 4.655 volts would be on the 954th step towards max voltage at step number 1,023. So, in this exact case when we measure 13.5 volts at the battery, we should also get 4.655 volts at the Vout connection. Adjust the pots until you do. Then, when we hook it to the Arduino and write a program to read the input pin of the voltage divider's Vout, then we should get a value of 954. We can use the program in the Arduino to convert that value of 954 to a value of 4.655 volts.

Also, one more thing. It would be a good idea to protect the Arduino using a Zener diode. Just place a zener and resistor in this configuration and it will protect the Arduino from ever seeing more than 5.1 volts.

OK, that's it for now. Some of you find this overly simple while others are picking up little brain giblets on the floor. I'll split the difference and call it quits until part #2.


Then Take A Look At This!