Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Building An Amp Hour Meter

  1. Apr 23, 2005 #1


    User Avatar
    Science Advisor

    This thread is a continuation of my previous thread, except this one is primarily focusing on the contruction of an amp hour meter and programming it, rather than the gathering of parts for it. (since the parts have just arrived, very slow mail order)

    I am getting very confused when looking at the circuit diagram (provided by faust9), I dont really know what is going on here, I am just going off faith that it will work.

    For the purposes of my questions, I will divide up the diagram in to 3 sections,
    Section 1 (from the far left side to R5)
    -W4, and W5 are "connectors of some sort, earlier given as bannana plugs. this is where my circuit that I wish to measure is connected up correct, accorss the . 1 ohm ressitor? that would put the amp hour meter in parallel with my circuit, so far so good if I am understanding.

    Section 2 (from R5 to where R3 reconnects to PB3)
    -The triangle shaped symbol with all the numbers is the Op Amp Right?, Which is really shaped like a recangular chip with no sort of numbers on it coresponding to the diagram, luck me.
    -Pointing up and down from the Op Amp, from #7 and #4 are arrowsm one is +5 v, which I assume I need to connect to my seperate input powersupply to supply it with 5 volts, but what about the other arrow, does that goto a ground?
    -by R7, which is an optional ressitor which I was going to leave out, there is an arrow pointing in both directions, with a -5v, so I assume that this needs to be connected with the -5 volts from my power supply, what is the arrow pointing in the other direction? what do I do if I choose to leave out R7, or even if I keep it in, how is it connected?

    Section 3 (from where R3 reconnects to PB3 to the right side of the page).
    -I am still unlear on why the "I" (labeled SW) symbol is needed or what exactly I am looking to put in there. I know it is a Push Button Normally open switch, but why is it neede, what does it do? and the arrow comming out to its left, is that ground?
    -Then there are several more +5v leads that I need to connect to my input 5 volt power supply.
    -Down at the bottom, on the ship, #4, there is a ground arrow, but I dont even know where I will connect this to a ground, the only ground I ever use is a screw in a plate of a light switch cover on the wall. but I dont think this is what you were meaning.
    -Then there are the W s, which are for in circuit programming I think. What if I decide I dont want to do any of that, I will just program it correctly out of circuit, then once I get it right, solder it in then and only then. couldnt I leave those out then?
    -Then it connects to the LCD screen, I am sure I'll have problems when I get to that stage.

    I am getting really confuses since I dont know what I am doing here, help if very much appreciated.
  2. jcsd
  3. Apr 25, 2005 #2
    1. No, the R0 resistor goes in series with the circuit. It "converts" the current going through it into a voltage for the differential amplifier (741) to measure.

    2. Yes, its an op-amp, yes its 5V, yes, to ground.
    -R7 is a variable resistor across pins 1 and 5 of the 741 and with the resistor wiper at -5V. It is used to offset the op-amp. Depending on how big the offset needs to be you could leave the resistor out, as long as you put something in the program code that will subtract the offset after the chip has done its ADC. This could however lead to continualy re-programming the device to zero the meter.

    3. The "I" is a switch which is used to reset the chip, by taking pin 1 low. It's used to reset the the display. The arrow is connected to ground.
    -Pin4 of the chip goes to the same ground as the others.
    -I would highly recommend that you solder a IC "seat" and not the chip itself. Then you can program, slot the chip in, try it out, unplug the chip to reprogram, put in to try again etc, etc. with out all the soldering in between. It will also prevent you heating the IC up too much if you solder it directly.

    Do you have any deatils on the LCD screen you intend using?

    updated for clarity, grammer and cos I misinterpreted the function :rolleyes:
    Last edited: Apr 25, 2005
  4. Apr 25, 2005 #3


    User Avatar
    Science Advisor

    Thanks, that brought me a little closer to understanding all this.
    All this stuff that apperantly came so easy to you, is all new to me.

    what I meant by the series/parallel above was the my original circuit would be in series with the R0 resistor, but the amp hour meter circuit would be in parallel with my original circuit. so the amp hour meter wont really affect my original circuit.

    As for my LCD screen, I got a slightly different one than I said I was going to in the previous thread, I found one cheaper somewhere else.
    I dont know how to descibe it to well, it is a 2x16 character display and has a 8-bit LCD controller (HD44780 or equiv).
    it has 16 holes along the bottom (and a couple smaller holes).
    Here is the info I have on it:
    1: Gnd
    2: Vdd (power)
    3: Vee (contrast adjust) works well near Gnd
    4: RS
    5: RW
    6: E - Clock line
    7-14: Data0-Data7
    Interfacing to the display simply involves sending a 8-bit command and display data.

    As for the reset button, I will get some, excuse the term, cheapo button that is normally open and I push it to close the circuit.

    For the ground, I dont know what I will do, as I said before, wheneber I see a ground in a circuit, I would always use the screen on a light swirch cover (or a water focet/pipe), but I doubt this is what is meant? Will just leaving it as a wire attached to nothing work in this case, if the ground isnt that necesary in this case. What do "normal" people's circuits use for a ground in this kind of case?

    So if I chose to leave off the optional [varaible] resistor, R7, how would I connect the -5v to the opAmp? would I connect it to both pins 1 and 5?
  5. Apr 25, 2005 #4
    Sorry about the ground. In this application ground is not earthing but simply the 0V line of your power supply.

    If you don't use the optional resistor leave the pins unconnected. But if you have a -5V supply available then I would definitely make use of it.

    Can't find much on your LCD display
  6. Apr 25, 2005 #5
    LCD stuff:

    Ever seen those LCD displays on computer cases?

    As for the ground, in electronics, ground can be something other than zero(floating ground) or earth as mentioned by Delta. Ground is simply a reference that your circuit is built around. Your powersupply to your circuit (your wall wart or batteries) should be used as the reference where the ground is the negative battery terminal or the negative terminal of your wall-wart connector(somoe of these are center (+) while others are center (-)---make sure you verify the polarity if you use one of these).

    Delta covered the rest it seems

    [edit] Oh, those 'W's are used for something called in-circuit programming. You don't need this functionality but it makes life easier. They are a little incomplete in that I didn't show isolation circuits (something like a 10K resistor between the LCD and the W) because these are not needed. They are just a covenience. AVR datasheets cover ICSP pretty well. Also, LCDs read 8 bits ususaaly. A lot of LCD controllers have a provision for two four bit transmission but if yours doesn't working around that isn't really that tough. You'll need to find the datasheet to verify your controllers exact functionality.
    Last edited: Apr 25, 2005
  7. Apr 25, 2005 #6


    User Avatar
    Science Advisor

    I hate to keep asking what might seem like the same questions over and over again, but it is helping.

    this "wall wart", it is some type of external power supply to the amp hour meter circuit, for example a battery or AC/DC converter plugged into the wall?
    This power supply's output should be fairly close to 5v, but it isnt that critical that I get it right on, because I have the voltage regulator to "regulate" it.
    Now if I may turn your attention to the PS circuit (figure 5).
    The input on the left, would that not be the + side of the power supply of whatever I find to power it (battery, ...), and the output, the - side?
    The rectangle in the middle, labeled, "MC78XX/LM78XX", is that the voltage regulator? I now have both my capacitors (1 .1 uF tantalum and a 1 uF electrolytic capacitor <- the closest one I could find to .33 uF). Then at the bottom middle, you see a 3 pronged device, what is that? could it be the voltage regulator insted and the "MC78XX...." is the battery?

    OK, so now where ever I see a ground or -5v in the circuit, I will connect it up to the negative side of the power supply, and where ever I see a +5v, I will connect it up to the positive side of the power supply.

    I did end up getting some of those IC socket/"Seat" things to colder into the circuit rather than the real chips (OpAmp and Microcontroller), so they will make life a bit easier.
  8. Apr 25, 2005 #7


    The answer to your P/S question is yes. The left pin (looking down at the regulator with the metal tab away from you--the metal tab extends up above the plastic and is embedded in the back of the 7805 as well) is the input power pin from your external source. The middle pin is connected to the ground or (-) terminal of your source as well as the ground of all of the components you use. The right pin is the 5V output.

    The rectangle in the middle is the voltage regulator and the three pronged device is know as chassis ground.

    Next, yes. Like delta said if you don't want to offset your OpAmp then leave those pins unconnected (floating) and connect the 5V to all of the appropriate sources as well as all of the GNDS.

    Additionally, the power to the 7805 can be fairly high (up to 18V for an 05 if I recall correctly) so as long as your external P/S is a little over 5V all the way up to 18V then you should be OK.
  9. Apr 30, 2005 #8


    User Avatar
    Science Advisor

    OK, I think I have assembled the circuit correctly (I dont know for sure, since I cant test it, I cant test it until I program it).

    the basic idea for how this will work is, I will be continually (as fast as the chip will let me), be checking the amps in the circuit, then from that, get the amp hours. I will add that current cycles amp hours to the running total, and display it on the LCD screen. Since the chip runs at 20 MHz (so claims the manufacturer), that it will have 20 million (20,000,000) cycles per second, we will also assume a constant current in between cycles, after all how much can it change in 1/20 millionth of a second.
    Check the amps, multiply the number of amps by 1.38888889 × 10-11 (the number of hours in each cycles), and then add that to the total.
    Now obviously, that is going to give us a very small number, a very very small number indeed, also we dont really need that accurate of a measurement, so we might want to use a counter variable to count off every 20,000 cycles or so, and then take a measurement, insted of every single cycle.
    Sound good? we will still get a measurement taken every milisecond (1/1000), and that would mean that we would then multiply the amp ready by 2.77777778 × 10-07, giving us a much higher (and more significant) number.
    Also, maybe insted of displaying the running total of amp hours, we can insted update it only every second, so only update the LCD display every 20,000,000th cycle.
    And then later, if I discover that I want to have "super duper" acuracy, it wont be all that hard to change.

    I think that if I write this code in C, it will one, be a lot easier for me to write, understand, and maintain in the furture, as opposed to writting it in assembly.
    The only problem is, since this is specialized code here (they dont teach this stuff in any learn to program books, nor do you run into it on an everyday basic), I dont have a clue on how to do the most critical elements of the program, read the amps and display the data on the LCD.
    Wanting to do this in C rather than assembly makes it even harder to find any sample source code to use.

    Any ideas?
  10. May 1, 2005 #9
    I would suggest coding in asm because your chip only has 1Kx8 of flash memory to work with. You can do it in C; however, writing code in C as efficient as asm code takes as muck time and effort to do as writing in asm to begin with(it can be done though).

    Now, your chip comes with a default clock speed of 9.6Mhz---you don't need to change that because you are still sampeling the ADC at a rate far greater than one second intervals.

    Here's the basic idea of what you want to do:
    If you do things in this order, then life should be a little easier when you start debugging your program:

    1) set up all of your ports---you need to tell the MCU which ports are input and which are output ports.

    2) Set up your ADC---you need to determine the ADC prescaler, left justify (tthe ADC is 10 bits, the MCU is an 8 bit processor---doing it this way will make things easier and you'll only loose resolution at the very bottom end of your scale: outside of your defined operating conditions I might add) set the voltage reference to Vcc, which channel is read, and what not.

    3) Set up your interrupts. You can make your device constantly poll the ADIF bit (ADC completed conversion flag) or you can let the ADC interrupt your program when it completes. The first solution would probably be easiest for timing pruposes. I'd disable all interrupts and set up a loop that reads the ADIF. If ADIF is set then read ADCH and do all the math needed to determine amps/unit time, if ADIF is cleared then loop to the beginning again.

    4) start your main program. Write everthing you want to do in little functions i.e. send data to LCD and call these little functions as needed. It's a good idea to send data to the lcd every second so your LCD doesn't turn into a blur of numbers. You will have to write a timer function to do this though. The timer on the MCU is an 8 bit timer thus it counts from 0 to 255---every clock cycle. When the clock overflows (goes from 255 to 0 again) an interrupt is triggered (this is a good place to use interrupts). Write an interrupt routine to clear the overflow bit, and increment an a bit register. You'll probably have to chain a few registers together to get your one second delay between LCD updates. You'll be well served to set your timer prescaler to 1024 which means you'll have to count off about 9570 clock pulses between LCD writes or 2^14 bits--two registers. You'll keep incrementing one register when the timer interrupt occurs. Check the carru flag of the sreg. If the carry flag is set then increment the upper reg. check to see if the 5th (5:0) bit is set. If set send data to LCD and clear both high and low registers. If cleared then start process over again. You can do this by decrementing as well (there's actually a good reason to use decrementing for this vice incrementing but it elludes me at the moment).

    Make and ACD read function--move contents of ACDH to a reg and perform the needed math on that. reset adc start process over again.

    If you want to keep a running total of amps consumed then you will need to determine the largest time needed(days, weeks, months, years...) and set aside enough registers to store your results.If you wanted to keep a days worth of data for instance then you'd have 24(3600)(5)=432000 which equates to 19 bits of information (3 registers).

    Your going to have to write the whole program out then once you have everything working you'll need to determine a trimmer variable to correct the time. Basically, make your circuit, connect it to a test circuit with a know current and let things run for a time (the longer the better not to exceed you max time though)---say an hour. Your MCU will say (XXXX) while the real amps/hour may only be XXX. Use this to develope a trimmer value which you can add to a register or use as a multiplier to correct the MCU to real life.

    Well, I know I missed a few things, but the above should help you get going. Good luck and don't let asm scare ya. It's easy if you think things out step by step including "where do I want this value to be stored". C takes care of the where question usually in asm you have to handle it.
  11. May 1, 2005 #10


    User Avatar
    Science Advisor

    OK, that was way over my head. I understood some of it, and the concept of most of it, but knowing how to actually do that is a different story.

    I dont understand how it will be able to calculate the current from just reading the voltage accross the .1 ohm ressitor. If you use Ohms law (V = RI), so the current = V/R, or V/.1 in this case, but the .1 resistor isnt the only resistor in the circuit to be measured, how will the amp hour meter know this? There is something missing in my mind on how it works. And we havent even gotten into the specifics of doing the actual programming yet.
  12. May 1, 2005 #11
    Machine code is by no means my specialty, but I can tell you a little about the .1 ohm resistor concern. Any current going through your main load HAS to also go through the .1 ohm resistor. Using ohms law you can compute the current through it. There is NO MORE to this. Current in .1 ohm = current in load = current in the wires carrying the current to the load. Series circuit, current is the same.
  13. May 1, 2005 #12


    User Avatar
    Science Advisor

    I am afraid I dont understant.

    Lets say we have a 5 volt power source for the mail circuit.
    connect this power source to a circuit with say... a 10 ohm resistor, which is them connected in series with the .1 ohm resistor in the amp hour meter circuit, for a combined ressitance of 10.1 ohms.
    I = V / R
    We know the potential difference/voltage is 5 volts.
    We also know that the real combined resistance is 10.1 ohms.
    So the real current is 5 / 10.1 = .495 amps.
    But for the hour meter, all it knows if that it is measuring the voltage across a .1 ohm resistor, so
    I = 5 / .1 = 50 amps!!

    or is this where the trimmer would normally come in, or in my case, the corretion calculation in code?
  14. May 1, 2005 #13
    Don't forget you also have an opamp (10x gain IIR) and you'll need to multiply each calculation by the time required to complete one sample. The opamp will be easy to factor in as will the timeing. once you have your code you'll then be able to figure out the average time to complete a cycle. You'll end up with some correction factor which consists of a time factor and the gain factor multipied by the adc result(it's a good thing avr's have a floating point because this would be a bear to do as an integer). Keep a running total of the results and you're good to go.
  15. May 1, 2005 #14
    You're measuring the voltage dropped across the 0.1 ohm resistor only. The 10 ohm resistor will drop most of the voltage while the 0.1: [tex]V_{0.1}=\frac{V_{source}}{R_{10}+R_{0.1}}*R_{0.1}}=0.049[/tex]

    Since you know ohm's law and you are measuring the voltage dropped by the resistor and you know the value of the resistor then you know two of the three variables. Here: lets say the voltage on the output of the opamp is 3 volts with a 10x gain. That tellsyou there is a .3 volt drop across the resistor and 0.2volts/o.1ohms=I through resistror=3 amps. Now if it takes 100 clock cycles to figure this out (AVR studio will tell you this) and one clock cycle is 103 nanoseconds then you'd have (103 nanoseconds)*100*3/3600=8.58333333 × 10-09 amp-hours for that one conversion. Just keep adding the conversions to get get the running total.
    Last edited: May 1, 2005
  16. May 2, 2005 #15


    User Avatar
    Science Advisor

    I was discussing this project and the circuit diagram with some other people and they expressed some concerns about why an OpAmp was choosen as "the shunt resistor instead of the differential input of the ATtiny13?". Also, there was a commend made that the 741 [OpAmp] isnt in common use now-a-days, why choose it over say the build in feature of the chip?
  17. May 3, 2005 #16
    I'm sure there's loads of missed stuff in here, as only a sample of the circuit is available.

    However a look at the ATtiny13 datasheet seems to show that the chip only gives a logic output if the analogous signal on one pin (Ain0) is greater on the other (AIN1). Using the chips ADC directly isn't much use if we are measuring incircuit current, unless one end of the shunt is at 0V line with the ATtiny13. Therefore an intermittant comparator is being used.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Building An Amp Hour Meter
  1. Hour Meter (Replies: 35)

  2. 50 Amp Hour Battery (Replies: 2)

  3. Amp Hours (Replies: 3)