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.

Richard


IF YOU'RE INTERESTED IN LEARNING ELECTRONICS 
Then Take A Look At This!

6 comments:

  1. Hi Richard,

    I live off-grid with an under-powered solar system set-up and keeping track of my batteries voltages is crucial. I have basic electronic skills and understanding so it is gonna take serious study and re-reading to grasp all that is said. Been reading about arduinos for some time and I am glad to find a good starting point that actually dovetails with my interests and lifestyle.

    Thanks,
    Geoffrey

    ReplyDelete
    Replies
    1. Thanks, Geoffrey. I don't go into specifics about hooking this up to the Arduino or the code needed, but if you have any questions, feel free to ask.

      Delete
  2. Hi, I think your last circuit is wrong. R3 should go between R1 and (zener + arduino).

    ReplyDelete
    Replies
    1. (Or, equivalently, Vout should go between R3 and the Zener diode)

      Delete
    2. Thanks, Ricardo. You are exactly right. I'll fix that.

      Richard

      Delete
  3. This comment has been removed by a blog administrator.

    ReplyDelete