PDA

View Full Version : Trying to determine dc current - 12v lead acid battery charger



tasmod
- 21st June 2012, 13:34
Hi all,
I had a 16f877 automatic battery charger all working on an lcd with voltage and current shown. This from the net some time ago. However the unit crashed and corrupted the code which is no longer available.

I 'm in the process of writing my own code in PBP to suit the built board.

So far all is good, I have the voltage readout, automatic startup on connection, switching to trickle charge as appropriate.

Now I'm having trouble getting my head around determining the current. (Maybe because I've got a stinker of a head cold!)

The board has two resistor divider adc inputs across a 0.1 ohm resistor on the output.

I know if I connect a mutimeter set to volts on either side of the resistor I get a reading in mV directly proportional to the current. But for the life of me I can't figure the code out.

I know it's I=V/R so i tried something like this:-

amp = adc1 - adc2 ; subtracting output adc reading from the input adc
mamp=(amp*100)/10 ; ohms law on mV result?
LCDOUT $FE,$80,#mamp,"mA"

This doesn't work, so where am I going wrong. The maths was there in the original code to make it work with this setup.

Rob

falingtrea
- 21st June 2012, 18:44
First question is what adc1 and adc2 units are? Are they mV, V, or adc_counts? Translation would depend on that. But basically with a 0.1 ohm shunt you should see 0.1V drop for every amp of current. So if the unit of amps is volts, just multiply by 10 to get amps or multiply by 10000 to get milli-amps:

A=V/0.1= V*10 or mA=(V*1000)/(0.1)=V*10000

If the adc1 and adc2 results are in milli-amps, then divide by 100 for amps or multiply by 10 for millamps:

A=(mV/0.1)/1000=mV/100 or mA=mV/0.1=mV*10

SteveB
- 21st June 2012, 21:53
Rob,
You need to account for the ADC reference voltage and the number of bits. Assuming you are using the pic ADC, the ADC is 10 bits and is likely referenced to 5 volts.

This gives: 5V/1024 = 0.0048828 Volts per bit (4.8828mV per bit).

Now, as Tim mentioned, 100mV across the shunt = 1 Amp*.

Putting that together, each bit from the result of adc1 – adc2 will represent .048828 Amps (48.828mA).



amp = adc1 – adc2
mamp = amp * 4883
LCDOUT $FE, $80, DEC mamp/100, ".", DEC2 mamp, "mA"



*But, you mentioned “The board has two resistor divider adc inputs…” I would guess that these are dropping the voltages down to the input range of the pic. If this is the case, then you may not be getting 100mV per 1 Amp. For instance, if the voltage divider was set up to give 1/10 of the voltages before and after the shunt, you would end up with 10mV per 1 Amp getting to the ADC. So you will need to adjust the numbers above accordingly.

HankMcSpank
- 21st June 2012, 22:58
Putting that together, each bit from the result of adc1 – adc2 will represent .048828 Amps (48.828mA).



Which isn't a whole lot of resolution.

I'm not sure which PIC you're using, but I use the latest range of PIC16f182x ....certainly with that suite of PICs (& mybe others...dunno), you can tie the ADC internal 'positive reference' to an internal derived fixed reference voltage.....so you'd then get over 4x resolution if you made the ADC's positve ref the 1.024V internal reference (vs a PIC 5V supply voltage)

Also just another way of looking at the above (which are obviously all correct, but sometimes it's nice to have another way of looking at things).

1. First establish the voltage drop across your current sense resistor.....adc1-adc2 ... multiply this result by your ADC 'volts per bit' in play (therefore if 1.024V fixed ADC positive ref I mentioned & 10 bits, that's 1mV per ADC 'bit')
2. Now you have the voltage drop ....you already know the resistor value, it's then just ohms law (I= measured voltage drop/0.1R) ....you'll need to scale the number up as PICbasic isn't keen on decimals.

tasmod
- 22nd June 2012, 07:48
Thanks Guys,

Getting my head around this now it's a bit clearer.

I'm wondering if this was ever working correctly as I'm sure the LM317 used is in constant voltage mode. That would explain why I am getting such a small change from the adc count?

Counts are typically 985 on one side, 961 on the other, giving me a count difference of 24 representing the voltage drop. The charger at the moment has a 1amp max output which I will be changing later to 5 amp using a LM338. Going from 300mA trickle to 1000mA charge at the moment only changes the count difference to 22, a change of 2 which doesn't seem right.

I'm on a different computer to the one I program on, so I will load the code and schematic for all to see. The original link to this project is now dead, it had all the details plus the hex code.

My coding style may not suit all but it usually gets the job done for me.

Rob

tasmod
- 22nd June 2012, 08:06
OK, hopefully I've attached the code and pdf of the project.

tasmod
- 22nd June 2012, 08:12
Clearer view of the schematic as the one in the pdf is cutoff at the output.

Aussie Barry
- 22nd June 2012, 08:12
Hi Rob,

Is the 0R1 shunt resistor connected on the high side or low side of the load?
If it is on the low side then you should only need one ADCin measurement; the second leg of the shunt being directly connected to ground.
The maths becomes very simple, measure the voltage drop across the shunt resistor and scale directly to Amps or MilliAmps as required.

Cheers
Barry
VK2XBP

Edit: Ignore my last post. I was typing this as you were sending the circuit diagram...

tasmod
- 22nd June 2012, 08:29
I originally blindly built this unit before realising only the hex was available at the time. I've since changed computers a few times and the code was lost.

Now it's a project for me as I'm experimenting (playing :) ) with car batteries. I want to go on and design a versatile charger with data storage for downloading charge discharge/curves along with control of a desulphator unit. I've a couple of 24LC16b chips which I can I2C for storage, along with a Max232 for PC connection.

I've managed to recover one battery that had sulphated after being left for a month during Feb when temps were below freezing. Normally indoors and float charged necessity meant it was left out connected to a non used vehicle. The battery was only a year old and current replacement costs meant I wasn't going to let it die without a fight.

tasmod
- 22nd June 2012, 08:34
OK thanks Barry.

Rob
G4NQX

HankMcSpank
- 22nd June 2012, 08:37
300ma x 0.1R = 30mV

1000ma x 0.1R = 100mV

Your PIC is on a 5V regulator, so assuming 10 bit ADC = 5V/1024 = 0.0048828125V per ADC 'bit'.

30mV divided 0.0048828125V = ADC reading of 6 for 300mA current (through your sense resistor)

100mV divided by 0.0048828125V = ADC reading of 20 for 1000mA current (through your sense resistor)

As mentioned earlier, that's insufficient resolution - you can get 4x more by making your PIC's positve reference the PIC's internal 1.024V fixed reference voltage. (probably still not enough, so I'd consider making the sense resistor 0.2 ohms etc)
current (through your sense resistor).

Also, how does each side of the sense resistor 'tap off' connect to your PIC ADC pins, becuause they are sitting at 12V?

tasmod
- 22nd June 2012, 08:49
Thanks Hank,

I've come to the conclusion the LCD output current value originally was either dummy or flawed.

The ADC divider circuits are shown seperate just below the output on the schematic. Note the arrowbox symbols are breaks in circuit diagram, the diagram is the original authors for whom I cannot give credit as all references are now dead.

Rob

pedja089
- 22nd June 2012, 14:29
Try MAX4080...

tasmod
- 22nd June 2012, 17:02
pedja089, thanks for info I will check that Max chip out.

Meanwhile,

Hmmm interesting one http://http://electronics-diy.com/70v_pic_voltmeter_amperemeter.php (http://electronics-diy.com/70v_pic_voltmeter_amperemeter.php)

This uses a .047 in ground line. I've just been out and bought a .047 3W resistor anyway, so will try a mod to my board and see if I can re-configure things around.

Rob

fratello
- 22nd June 2012, 19:39
...and this is the original code from this schematic :

'************************************************* ***************
'* Name : VOLTMETRE.BAS *
'* Author : [Erol Tahir Erdal] *
'* Notice : Copyright (c) 2005 [ETE] *
'* : All Rights Reserved *
'* Date : 01.03.2005 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
DEFINE LCD_DREG PORTB 'LCD data bacakları hangi porta bağlı?
DEFINE LCD_DBIT 4 'LCD data bacakları hangi bitten başlıyor?
DEFINE LCD_RSREG PORTB 'LCD RS Bacağı Hangi Porta bağlı ?
DEFINE LCD_RSBIT 1 'LCD RS bacağı Hangi Bite bağlı ?
DEFINE LCD_EREG PORTB 'LCD Enable Bacağı Hangi Porta bağlı?
DEFINE LCD_EBIT 3 'LCD Enable Bacağı Hangi bite bağlı ?
define lcd_RWREG PORTB
define LC_RWBIT 2
DEFINE LCD_BITS 4 'LCD 4 bit mi yoksa 8 bit olarak bağlı?
DEFINE LCD_LINES 2 'LCD Kaç sıra yazabiliyor

DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS

@ DEVICE pic16F877
@ DEVICE pic16F877, WDT_off
@ DEVICE pic16F877, PWRT_ON
@ DEVICE pic16F877, PROTECT_OFF
@ DEVICE pic16F877, XT_OSC

ADCON1=%10001110
TRISA=%00010011
TRISB=0
TRISC=0

VOLT VAR WORD
HAM VAR WORD
HVD var word
AMPER var word


MV VAR BYTE
MA VAR BYTE

PORTB=0
PORTA=0
VOLT=0
PAUSE 100
LCDOUT $FE,1


BASLA:
ADCIN 0,HAM
BAK: IF ADCON0.2=1 THEN BAK
PAUSE 5
GOSUB HESAPV

ADCIN 1,HAM
PAUSE 5
GOSUB HESAPA

GOSUB EKRAN
PAUSE 1500
GOTO BASLA


EKRAN:
' LCDOUT$FE,2,"HAM=",#HAM 'gerekirse okanan ham değerini görmek için
LCDOUT $FE,$02,"VOLT =",DEC VOLT,",",DEC1 MV 'dec2 yazdık çünki virgülden sonra 2 hane hassasiyetimiz var.
LCDOUT $FE,$C0,"AMPER=",DEC2 AMPER,",",DEC2 MA
' LCDOUT $FE,$C0,"AMPER=",DEC HVD
RETURN

HESAPV: '0-25V değerlerini oukuyup göstermek içindir
HVD=((HAM+1)*/875)/10 'bu ifade aslında (HAM+1)*625/256 nın karşılığıdır
'CARPAN=625 yani ((25x10)/1024)*256 sonuç 62,5 çıktığı için 625 almak için
' 10 yerine 100 ile çarpıyoruz. yani CARPAN=((25*100)/1024)*256
VOLT=HVD/10 'başlangıçta 100 ile çarpılmış halini hesaplamış idik ohalde 100 e böleceğiz
MV=(HVD)//10 'seaplanan sayını 100 bölünmesinden kalan sayı ondalık hanesidir
RETURN
' Formül;
' Okunacak Maksimum Volt=OMV olsun
' CARPAN= ((OMV*10)/1024) * 256 veya ((OMV*100)/1024)*256
' CARPAN değerini 3 haneli rakam olarak kullanmaya gayret edin.
' şayet 62,5 gibi 2 haneli bir rakam çıkar ise bunu 625 kabul edin ve
' (OMV * 10) değerini (OMV * 100) olarak kullanın. Sonuçta hesaplanan volt değerini
' 10 yerine 100'e bölmeniz gerekecek tabiiki. OV hesabında da Yine 100 bölünmüş
' değerden kalanı hesaplamak gerekecek. Yukarıdaki örnekte olduğu gibi.
' Hesaplanacak Volt Değeri =HVD olsun
' ADC den okunan Değer HAM olsun
' HVD=(HAM+1)*/CARPAN bulunacak değer gerçek volt değerinin 10 ile çarpılmış halidir.
' Tamsayı VOLT (TV)= HVD/10 veya TV=HVD/100
' Ondalık VOLT (OV)= HVD//10 veya OV=HVD//100
'Ekrana yazdırırken;
' LCDOUT $FE,2,"VOLT:", DEC TV,",",DEC1 OV veya DEC2 OV olacaktır
HESAPA:
HVD=(HAM+1)*/250
AMPER=HVD/100
MA=HVD//100
RETURN

Credits to 320volts.com !

HankMcSpank
- 22nd June 2012, 20:39
pedja089, thanks for info I will check that Max chip out.

This uses a .047 in ground line. I've just been out and bought a .047 3W resistor anyway, so will try a mod to my board and see if I can re-configure things around.

Rob

Whilst that higher resistor will give you 4x the ADC reading vs a 0.1R resistor, that's still only an ADC variance of about 60 steps between 1000mA & 300mA for a PIC supply of 5V....like I say, if you use a PIC where the ADC positive reference can be tied to it's internal 1.024V fixed reference (eg 16f1824), you'll then get a variance of about 240 steps between 1000mA & 300mA current draw ...which is starting to become useable.

tasmod
- 22nd June 2012, 21:30
fratello. Yes I saw that one but it isn't this one, this one had various calibration routines etc in the code. Notice the defines for xtal osc. I do remember the author for mine came from India.

Hank,
I'm getting old I just don't get this. I've modded the board to have the .47 (not.047 as i put above) in series to the ground line and put a 100k to the adc input as per the schematic from the link above. This uses a 16f876 and I've duplicated the adc input circuit. They are claiming up to 10amp and with 10mA resolution.

HankMcSpank
- 22nd June 2012, 23:15
Hank,
I'm getting old I just don't get this. I've modded the board to have the .47 (not.047 as i put above) in series to the ground line and put a 100k to the adc input as per the schematic from the link above. This uses a 16f876 and I've duplicated the adc input circuit. They are claiming up to 10amp and with 10mA resolution.

It's pretty simple...let me try & explain.

If your PIC is running at 5V, then you're ADC resolution becomes 5V/1024 (the 1024 being 10 bits)...this results in a resolution of 0.0048828125V (4.9mV) per ADC bit, which sounds great, but not if your sense resitor is very small & therefore the maximum voltage drop will be very small. So at 1000mA & a .47 ohm resistor, your maximum voltage drop will be .47V across it (V=IR). ) .....so if you PIC has the aformentioned resolution of 0.0048828125V, then the maximum ADC reading you'll get is .47V/0.0048828125V ...or a max reading of 96 ...that's with 1A flowing (that's an awful lot of wasted bits, bearing in mind you've got 1024 bits available in your PIC).

To get more resolution, if the PIC is modern & has a P-ref register for the ADC module (16f1828 etc)....you tell the PIC that rather than use 5V as the upper limit for the ADC, to use an internally generated fixed 1.024V instead (again the PIC needs to be up to date & have one onboard) - there are no more components involved, you just need to set a register in the PIC & you're off to the races.

Now your ADC resolution becomes 1.204V/1024 bits ....or 1mV per ADC bit, therefore with 1A flowing through the sense resistor, your maximum ADC reading will be 470 which is a lot healthier amount of 'reading' to play with.

Of course if you want up to 10A, then this is not the way to go (but I thought you were talking of 1A max?)

tasmod
- 22nd June 2012, 23:42
Thanks Hank,

That all makes perfect sense and is what I believed and kinda saw all along.

I'm therefore puzzled by the circuit and claims for the above of 10A and 10ma resolution. This using a 16f876 which is the smaller brother of the 16f877 I have in the circuit.

I've read the datasheets. Looked at the circuit, there's nothing different to what I'm doing. I see no offset input, it has no internal 1.024v ref, I'm also now in the ground line with the .47.

This isn't the first circuit I've seen setup this way which claims 10A measurement with 10ma resolution but also using a 16f877. With 1023 bits to play with "it does not compute Captain"

HankMcSpank
- 23rd June 2012, 01:28
Thanks Hank,
This isn't the first circuit I've seen setup this way which claims 10A measurement with 10ma resolution but also using a 16f877. With 1023 bits to play with "it does not compute Captain"

It is kind of correct..... 10mA through a 0.47R resistor = 0.0047V drop across it (which, if you look back above the above posts, with a 5V ADC reference & 1024 bits, you can resolve 0.0048828125V per ADC bit)

the spec seems to have changed a little though ...I'd thought earlier on you were speaking of 1000mA being your maximum anticipated current ...if that were the case, then 10mA resolution didn't strike me as being that granular....however if 10A is to be your max current, then 10mA of resolution is just dandy! So I guess you need to bottom out your requirements & take it from there.....

Aussie Barry
- 23rd June 2012, 02:18
Hi Rob,

If you plan to increase the maximum current from 1 Amp to 10 Amps then I would suggest that you change the value of the shunt resistor due to power handling ratings. 10 amps flowing through a 0R47 shunt resistor will need a hi power resistor (P=I^2 x R = 47 Watts).

Your circuit shows the original 0R1 shunt resistor power rating as only 3 watts which has a theoretical limitation of just under 5.5 amps.

Cheers
Barry
VK2XBP

tasmod
- 24th June 2012, 19:57
Thanks Guys,

I probably confused you a bit.

The 1000mA unit is the one already built which i wanted to upgrade to 5amp later. It used a .1 ohm resistor in the positive output which I removed and linked out. I then added a .47 in the ground leg.

I then intended to design and build a multi function charger/desulphator unit which would probably be capable of 10amp.

I would be quite happy with 10mA resolution on any of them. It's just that the counts I'm getting from the ADC just don't seem right anyway.

I have another new 16f877 which i will test out.

I mentioned the advertised unit because of it's specs and it's circuit. 3W .47ohm resistor in ground leg with 10amp measurement ? Capable of recalibration to greater amps?
Probably a catchpa in the instructions when you get it.

Rob

falingtrea
- 26th June 2012, 19:01
As Barry pointed out, if you use a 0.47 ohm shunt and are planning to draw 10 amps through it, the resistor will need to be rated for at least 47 watts!!! :eek: Thats a lot of wasted power!! And one big honking resistor! What you may need to do is supply a Vref for the ADC that is lower than Vcc and use a smaller shunt resistor. Or put an op-amp across a smaller shunt resistor to amplify the difference. You may need to drop the shunt down to 0.01 ohm in order to get a reasonable sized resistor. That would be a 1 watt resistor. If you drop Vref down to about 1V, then should be able to get 1 mV resolution. With a 0.01 ohm shut, that would give you about 100 mA per step. So you may still need to use an op-amp if you want better resolution.

tasmod
- 26th June 2012, 21:18
I've looked at a shunt that I have for a 100A meter which I can use.

Out of interest today I programmed the code that fratello posted and tried it on my board after changing the lcd defines etc.

RESULT !

Well, the voltage code is incorrect BUT the current code works just fine. Now if only I could get my head around the language used :D

Ramius
- 27th June 2012, 14:38
Maybe this might help? http://www.picbasic.co.uk/forum/showthread.php?t=16387 Best, Ed

tasmod
- 27th June 2012, 18:21
Yes interesting chip, that'll do nicely for the large current version.

Finally success with the 'current' routine.

I've been completely puzzled by the readings and results so I rewrote the code just to get the adc counts.

The adc count for the current input mirrored the current output of the unit. Great, now just alter the code again and run.

Hmm no joy, it was out again by a long way. So I isolated the code to study the routine, no it was correct. Then I wondered why the routine should be so far out from the count. Logic said it wasn't the count but something else. So I checked the defines. Argh, the ADC osc had been commented out and I had another oscon0 that set it at 8Mhz.
Changed the adc back to internal rc osc and the routine now worked.

tasmod
- 27th June 2012, 18:29
Regarding the 0.47 ohm resistor.

As it's in the ground leg am I not correct in thinking the power rating is different, as it's current is say, 1amp, but it's the voltage drop across the resistor that counts which is in mV.

Put it this way with real world on the board. On the positive output it ran quite warm at 1amp. In the ground leg it is still cold at 1amp.

HankMcSpank
- 28th June 2012, 00:13
Regarding the 0.47 ohm resistor.

As it's in the ground leg am I not correct in thinking the power rating is different, as it's current is say, 1amp, but it's the voltage drop across the resistor that counts which is in mV.

Put it this way with real world on the board. On the positive output it ran quite warm at 1amp. In the ground leg it is still cold at 1amp.

It doesn't matter whether the resistor is 'in line' with the 12V supply wire ....or inline with return to ground wire....the same current will be present (the current leaves the charger, goes through the battery & returns to the charger's negative reference .....one current path if you like)

If it was hot before & it isn't now, then that can only mean one of three things...

1. You had more than 1A running through the resistor in its original position.

2. You've not got 1A running through the resistor in it present/new position (what does a DVM say the voltage drop is) ....at 1A through a 0.47R resistor, should see a voltage drop across the resistor of 470mV. You've got 470mW being disppated across the resistor...which I reckon should be warm to touch.

3. You somehow connected the resistor in situ wrong....it should sit between the negative return wire from the battery & the circuit's 0V reference.

tasmod
- 28th June 2012, 13:31
Thanks for your parience Hank. Sometimes I wonder where my head is. I know all this, but I've been in a blank place since having the head cold, amazing how I can't think straight.

As for the heat, I realised later that the .1 was in series with regulator which runs quite hot, there was heat transfer. The .47 is in the ground leg which is nowhere near the reg.

The unit is running fine now and the basic (sic) software works, now I need to add the frills such as logging to eeprom, temperature compensation etc.

For the larger unit I have in mind making it multi battery, nicad, nimh etc. with menu choices.

tasmod
- 30th June 2012, 08:57
OK I've given up on this board and I'm going to design my own and start again. There's something peculiar happening with it. I had it working just fine and left it to one side running but not connected to the test battery for a while. I was busy on the bench making up a temperature and memory board.

Later I connected the test battery and nothing happened to the current but the output was suddenly 7.9v ?? So I disconnected it, switched off and then tried again a minute or so later. It powered up fine gave the correct output but now the current routine was way off. I checked against a multimeter.

I suspect the LM317 is playing up.

On another tack, this months (June) QST has an interesting article on carrying batteries in a cooler box. What interested me was the authors use of a steel 3/16th (4mm) setscrew to make a high current shunt.

He used nuts and wire tags to make the shunt and calibrated against a multimeter by adjusting the nuts along the thread length till he got it correct. At the head end he used two nuts to clamp two tags tight, then at about 1 inch (25mm) he used two nuts again to clamp two tags. This end he screwed along the thread length to get the right reading. Quite novel.

Pic added with acknowledgemen to QST.

Ramius
- 1st July 2012, 00:26
Hi Rob,
The link I gave you accurately measures the current from .02 amp upto 70 amps and using the ACS758 takes care of the shunt resistor as it is part of the device. The program gives you the actual current as a digital number you can use for anything you wish as well as the voltage. http://www.picbasic.co.uk/forum/showthread.php?t=16387 Best, Ed

tvibakar
- 4th July 2012, 05:37
Hi all,
I am new to this forum. This is my first thread in this forum. Kindly help me. I am using 12V 7.2Ah Amaron Quanta lead acid battery for an Emergency Lamp. I am using the following charging circuit which is attached with this post. In this charging circuit, 230 ac voltage is given as input to the 16-0-16 transformer. The output of transformer is filtered through rectifier diodes and then given as input to the LM317 of charging circuit. As of now transformer is needed to step down the ac input voltage. I need a charging circuit without a transformer. So kindly suggest me a circuit which consists of input rectification without a transformer. Please suggest it with a moderate cost. And also I need one more clarification. I am using "Constant Voltage" - trickle charging method to charge the battery. I am using a load of 2 lamps of total 110 W lamps which stand for around 50 minutes of full charged battery. The lamps will cut-off at 9.5-9.8 V. Then it takes around 16 hours to make the battery full charge. Could you please confirm me if I can charge the battery soon (min 6 hours). Is so at what rate I should use the load. Please help me in this situation.
6563