PDA

View Full Version : voltage monitor



aftab
- 21st July 2007, 23:36
Hello friends,

I would like to make a voltage monitor for my battery pack.
It has 4 li-ion batteries.

When the voltage drops below 3.0 in any of the batteries it will blink a led.

My understanding is that I need an analog to digital converter IC that will
measure the voltage. Just want to make a very very simple circuit.

I have seen some examples here using maxim ICs. Up to this
point I have bought all my ICs from e-bay :-) Could not find the maxim ICs
in e-bay. What about using pic12c671 ?

Any suggestion where to start ?

Thanks

mackrackit
- 22nd July 2007, 01:20
All you need is a PIC with ADC. Here are two examples of how to use the ADC .
http://www.rentron.com/PICX2.htm
http://www.rentron.com/serial.htm

If the voltage that you start off with is higher than 5 volts, use a voltage divider to bring it down to 5 and adjust in soft ware.

w7ami
- 23rd July 2007, 18:47
The simplest and fastest would be to use a PIC with comparator. You would only need two resistors used as a voltage divider to bring your battery voltage down to something the PIC can handle. You can set the comparator reference voltage in code. You also can program the comparator to trip an interupt when the divided battery voltage drops below the reference voltage. You will need to look at the reference manual for the PIC you have to find out how to set the comparator and turn on the correct interupts.

Terry

GioppY
- 23rd July 2007, 23:24
Any suggestion where to start ?


Perhaps you can start from here:
It is for car battery; but you can adjust for any voltage
http://www.pctuner.net/articoli/Pic/571/Monitor_per_batteria_d_automobile_-_Datalogger/

Regards
Gianni

BrianT
- 24th July 2007, 05:55
Li Ion cells are easily damaged if they drop below 3 volts. Run them flat and they lose most of their capacity and instead of being a 1500 mAH capacity, they drop to maybe 1/4 of that capacity. In a series string of 4 cells I suggest you need 4 analog channels on your PIC, say a PIC16F88. You can measure the first cell directly but you need voltage dividers off cells 2, 3 and 4 to ground so you can measure V1, V2, V3 and V4.

V1 is the first LiIon cell and must not be allowed to fall below 3 V. V2-V1 gives you the voltage of the second cell. V3-V2 gives you the voltage for the third cell, etc.

The PIC16F88 needs no crystal and would be a very cheap solution for up to 7 series cells.

HTH
Brian

aftab
- 24th July 2007, 22:18
Hello,

Thanks everyone for the tips and links. I am getting the parts now from e-bay.
Rentron is always my favourite site for tutorials for amateur like me(thanks mkit)

On way is 12c672. And trying to get 16f877 - seems a bit more expensive.

From Wikipedia got valuable info on li-ion cells(thanks briant).

Got $1.59/each (X 8pc) charging circuit from batteryspace.com. The charger works fine but charging is stopped once the voltage reaches 4.2v and stops discharging when <3.0v. It is not current limited and the batteries do not get warm in the process of charging. So, I guess it does not charge the batteries to their full capacity. Interested people might find it useful for ready-made low cost chargers.

This micro controller world is really fascinating.

thanks
Aftab.

samtheboxer
- 25th July 2007, 08:21
The simplest and fastest would be to use a PIC with comparator. You would only need two resistors used as a voltage divider to bring your battery voltage down to something the PIC can handle. You can set the comparator reference voltage in code. You also can program the comparator to trip an interupt when the divided battery voltage drops below the reference voltage. You will need to look at the reference manual for the PIC you have to find out how to set the comparator and turn on the correct interupts.

Terry

Sorry to interrupt here, but im trying to build the same circuit to monitor a lipo battery on my RC heli. do you have a schematic for the voltage divider or what this circuit might look like using a 12v lipo batter? i am really lost on this project and cant quite get a handle on it!

from what i gather, the chip will compare the input voltage from the battery to the internal voltage and detect the difference? am i close?

mackrackit
- 25th July 2007, 08:51
Sorry to interrupt here, but im trying to build the same circuit to monitor a lipo battery on my RC heli. do you have a schematic for the voltage divider or what this circuit might look like using a 12v lipo batter? i am really lost on this project and cant quite get a handle on it!

from what i gather, the chip will compare the input voltage from the battery to the internal voltage and detect the difference? am i close?
This link will explain voltage dividers.
http://www.omega.com/techref/das/divider.html
The divided voltage will the go to an ADC pin of the PIC. The voltage will then either be compared to the voltage that the PIC is running on or a reference from another source(voltage divider).

w7ami
- 26th July 2007, 03:17
Samtheboxer,

Dave has already sent you a link that explains what a voltage divider is and how to figure the correct values of resistors to use so I won't repeat his info.

Whether you use an ADC or comparator you will need to use a voltage divider to divide down the battery voltage into a range that is safe for the PIC.

I suggested using a comparator and interupt because it doesn't require clock cycles from the PIC unless the battery voltage has dropped below the pre set threshold. I was thinking that he just wanted some sort of failsafe that would shut things down if the battery voltage dropped to low but do other things in the mean time. I am using just such a feature on a project that I am working on now. After re reading the original post I see that what was really wanted was to circuit that just monitored the battery voltage and for that the ADC approach is better.

Which circuit and code you use depends upon what it is you are wishing to accomplish.

Terry

samtheboxer
- 26th July 2007, 05:59
Samtheboxer,

Dave has already sent you a link that explains what a voltage divider is and how to figure the correct values of resistors to use so I won't repeat his info.

Whether you use an ADC or comparator you will need to use a voltage divider to divide down the battery voltage into a range that is safe for the PIC.

I suggested using a comparator and interupt because it doesn't require clock cycles from the PIC unless the battery voltage has dropped below the pre set threshold. I was thinking that he just wanted some sort of failsafe that would shut things down if the battery voltage dropped to low but do other things in the mean time. I am using just such a feature on a project that I am working on now. After re reading the original post I see that what was really wanted was to circuit that just monitored the battery voltage and for that the ADC approach is better.

Which circuit and code you use depends upon what it is you are wishing to accomplish.

Terry

Terry - I have attached a schematic of my circuit using the voltage divider. I drew it up really quick using paint, so it doesnt look pretty. Also, the battery input should be +12 and -12, not +12 and -11. I also figure out were to put that voltage divider ground, im just running it into the chip ground. (i will post a proper schematic here shortly, i am working on it now.)

So, i think ive got the circuit down but now i need to work on the program. All i want to do is run a program flashing some LEDS as a strobe light on my helicopter. When the LIPO reaches 9.5v (or so, just not below 9V) i want the chip to change the pattern of the LEDS to warn me that my battery is getting low and its time to land. So far, i understand i need to use the comparator on the 12f629 and compare the battery voltage from the divider to the internal voltage in the chip (supplied by a constant 5v).

Here is my frustration, I am new to all this and might be in over my head, but im not giving up. I understand how to blink a light and different flash patterns, but thats about as far as ive gotten. No matter how hard i look i cant seem to find a program to dissect or an example of what i want to accomplish and how to accomplish it code wise.

I hate to ask, but would it be possible for someone to write the picbasic pro code for me, a working program, so that i can run it and figure out how it works? I feel like if i just has a working program, a lot of it would make sense to me........

mackrackit
- 26th July 2007, 07:37
Samtheboxer,
Have you seen this? http://www.picbasic.co.uk/forum/showthread.php?t=6454&highlight=comparator
Lots of info and good links to even more.

w7ami
- 27th July 2007, 20:57
Sam,

Here is some code that have working on a 18F4620. You might have to change things abit to make it work on your PIC.

Not familiar with the battery packs you are using. Are they really +/- 12V or are they 24 volts? Either way I suspect that your PIC would be very unhappy connected across 10 volts ( +/- 5 V ).

I hope this gets you started. Good luck.

Terry

; PIC18F4620
define OSC 40 ; 40 MHz Oscillator"
TRISA = %00000011 ; portA.0 and A.1 are inputs
TRISD = %00000000 ; Port D all outputs


ADCON1 = %0001101

; Comparator Control
CMCON = %00000110
;CMCON.7 = 0 ; C2OUT Comparator Output 2 (Read Only)
;CMCON.6 = 0 ; C1OUT Comparator Output 1 (Read Only)
;CMCON.5 = 0 ; C2INV Comparator 2 Output Inversion
;CMCON.4 = 0 ; C1INV Comparator 1 Output Inversion
;CMCON.3 = 0 ; Comparator Input Switch
;CMCON.2 = 1 ; )
;CMCON.1 = 1 ; )Mode 110
;CMCON.0 = 0 ; )
; ;
; Comparator Voltage Reference Control
CVRCON = %10101010
;CVRCON.7 = 1 ; CVREN Voltage Reference Enable (1 = on, 0 = OFF)
;CVRCON.6 = 0 ; CVROE VREF Output Enable
;CVRCON.5 = 1 ; CVRR Comparator Range Selection bit
;CVRCON.4 = 0 ; CVRSS Comparator Reference Source
;CVRCON.3 = 1 ; )
;CVRCON.2 = 0 ; ) Set for Reference Voltage
;CVRCON.1 = 1 ; ) 1010 sets 2.083v in Low Range
;CVRCON.0 = 0 ; ) 9.2 Volts from Battery

; Start Main Program Loop
loop:
if CMCON.6 = 1 then
CVRCON = %10101101 ; 2.083V
PortD.7 = 1
else
CVRCON = %10101010 ; 2.708V
PortD.7 = 0
endif

Goto loop ; Do it forever

w7ami
- 27th July 2007, 21:00
Sam,

Here is some code that have working on a 18F4620. You might have to change things abit to make it work on your PIC.

Not familiar with the battery packs you are using. Are they really +/- 12V or are they 24 volts? Either way I suspect that your PIC would be very unhappy connected across 10 volts ( +/- 5 V ).

I hope this gets you started. Good luck.

Terry



; PIC18F4620
define OSC 40 ; 40 MHz Oscillator"
TRISA = %00000011 ; portA.0 and A.1 are inputs
TRISD = %00000000 ; Port D all outputs


ADCON1 = %0001101

; Comparator Control
CMCON = %00000110
;CMCON.7 = 0 ; C2OUT Comparator Output 2 (Read Only)
;CMCON.6 = 0 ; C1OUT Comparator Output 1 (Read Only)
;CMCON.5 = 0 ; C2INV Comparator 2 Output Inversion
;CMCON.4 = 0 ; C1INV Comparator 1 Output Inversion
;CMCON.3 = 0 ; Comparator Input Switch
;CMCON.2 = 1 ; )
;CMCON.1 = 1 ; )Mode 110
;CMCON.0 = 0 ; )
; ;
; Comparator Voltage Reference Control
CVRCON = %10101010
;CVRCON.7 = 1 ; CVREN Voltage Reference Enable (1 = on, 0 = OFF)
;CVRCON.6 = 0 ; CVROE VREF Output Enable
;CVRCON.5 = 1 ; CVRR Comparator Range Selection bit
;CVRCON.4 = 0 ; CVRSS Comparator Reference Source
;CVRCON.3 = 1 ; )
;CVRCON.2 = 0 ; ) Set for Reference Voltage
;CVRCON.1 = 1 ; ) 1010 sets 2.083v in Low Range
;CVRCON.0 = 0 ; ) 9.2 Volts from Battery

; Start Main Program Loop
loop:
if CMCON.6 = 1 then
CVRCON = %10101101 ; 2.083V
PortD.7 = 1
else
CVRCON = %10101010 ; 2.708V
PortD.7 = 0
endif

Goto loop ; Do it forever

samtheboxer
- 28th July 2007, 06:43
Its an 11.1v lithium polymer battery. I plan to run a voltage regulator to bring the chip voltage down to 5v. Sorry about the schematic, it may not look right, i drew it up pretty quick.

Thanks for all your help, the link was great and the code will help out alot. Seing it action helps me to figure it out.

I may have some more questions here soon! I started from the beginning and am starting to get a handle on all this pic stuff.....

Thanks to all

aftab
- 16th August 2007, 01:22
Hello friends,

I am posting this code for 12C672 which are OTP pics.
I do not have the 12C672JW and UV eraser--not for armature like me !

I have added my questions "???". I would appreciate your answers on these questions
Each error will cost me one 12C672, so please see if these are OK:
[ code ] code here [ /code ]


'**********************************************STA RT*******
'-----------------------------------------
'Pic 12C672 TEST adopted from Rentron.com
'
'Trying to get 4 channel A/D conversion
'
' and the book "Programming PIC Microcontroller with PicBasic"
'
' and code samples in the great user forum of picbasic.co.uk
'
' Will program in JDM board with IC-Prog 1.05e,Win98SE, Tecra CDM750 with com port - does not work in inspiron 700m e USB--RS232
'
' WHICH OPTIONS DO I SELECT in IC-Prog:
' a) Oscillator-->"IntRC GP4" or "IntRC CLOCKOUT" ; I want to use the onboard 4MHz osc. ???
' b) Fuses---> "WDT" unchecked, "PWRT" checked, MCLR unchecked; Are these OK ???
'
' Can it also be programmed with PonyProg--I am asking because there are less options than IC-Prog and
' it does not show the OSCCAL value for pic12C672<----???
'-----------------------------------------



'..8.......5
'.-|-|-|-|-.
'|PIC12C672|
''*|-|-|-|-'
'..1.......4
'
'PIN Name Use/Connection
'1 Vdd +5VDC
'2 GPIO.5 N/C Serial output to PC/ another PIC
'3 GPIO.4 (Channel 3) 1K Resistor to center of 5K Pot // 33k resistor between +ve and -ve of 1.5v battery OR a voltage divider with <= 5v
'4 GPIO.3 N/C Not Connected
'5 GPIO.2 (Channel 2) Same as GPIO.4
'6 GPIO.1 (Channel 1) Same as GPIO.4
'7 GPIO.0 (Channel 0) Same as GPIO.4
'8 VSS Ground

'---------------INCLUDE FILES-------------

Include "modedefs.bas" 'Include serial modes//Not mentioned in Rentron


'--------------------FUSES---------------
@ device pic12c672, INTRC_OSC_NOCLKOUT ;4MHz internal oscillator
@ device pic12C672, WDT_OFF ; Watch Dog Timer is OFF
@ device pic12C672, PWRT_ON ;Power On RESET On ??? is it OK ??


'???<---Any more fuses to setup--????
'
'TRISA <---? how to declare it here ?? Is it needed ?
'

'----------------DEFINITIONs and VARIABLES and SETUPS--------------
Define OSCCAL_2K 1 'Set OSCCAL for PIC12C672 // for 12C671 it is OSCCAL_1k

'???WHAT is the use/need of the following line IF I use the above line ---???
OSCCAL = $58 'Got this by READING the PIC12C672 using IC-PROG;PonyProg does NOT show this <---EXTRA(???)

DEFINE CHAR_PACING 100 'SEROUT pacing 0.1 millisecond

RESULT var byte[4] 'Array for FOUR channels

J var byte 'A/D channel number byte variable, this shortens the code

ADCON1 = %00000000 'Set GPIO.0, GP1, GP2 and GP4 (NB NOT GP3) to ANALOG input, Vref=VDD, No pins left for DIGITAL I/O


'-------------------PROGRAM STARTS HERE-------------------

MAIN:

FOR J = 0 to 3 'Count 4 channels

'???<-->Shouldn't I set the ADCON0 as %11000001 to use the internal OSC, I want to use the internal OSC
'ADCON0=%10000001 'Set A/D Fosc/32,[4MHz],Channel-0 = on <----???? For External clock?

ADCON0=%11000001 'Use the internal clock
'Do I need to do this every time a channel is read ????
'Any alternate ???
' %11xxxxxx <---This means ADCS1(bit7)=1 and ADCS0(bit6)=1; see table by Rentron/data sheet
' %xxxxxxx1 <---This means ADON (bit0) = 1 ie. turn the A/D on
pauseus 10 'Wait for 10 u Sec for channel setup <---??? is the time OK?

'ADCON0.2 is bit2 which is the GO/DONE bit; see table by Rentron/data sheet
ADCON0.2=1 'Start conversion by setting GO/DONE-bit to HIGH
'???Can ADCON0.2=1 be substituted with ADCON0=%11000101 <----?????

LOOP:
IF ADCON0.2 = 1 THEN 'Check if conversion completed
pauseus 5 'Wait for 5 micro second <---Is this time OK or should I increase it ??
GOTO LOOP
ENDIF

RESULT[J] = ADRES

NEXT

Pause 250 'Pause 1/4th sec so that conversion setup is done
'In what format will the output go to pc ? Binary, floating, integer, hex ????
'Is it possible to send in a format of xx.xxx volt(e.g. 3.456v or 11.123) ? i.e. with 3 points after decimal ?
'
SEROUT GPIO.5,N2400,["@",RESULT[0],"#",RESULT[1],"#",RESULT[2],"#",RESULT[3],"#"] 'Send the value of conversion to PC Serial port; @ will be used in PIC-PIC serout/serin2
'If you want to use baud rate above 2400, use external oscillator.
Pause 250 'Pause 1/4th sec to let the Serial communication to complete; it takes much less than 1 sec
GoTo MAIN



'********************************************END** *******

aftab
- 17th August 2007, 09:57
some modifications:


MAIN:

FOR J = 0 to 3 'Count 4 channels

'???<-->Shouldn't I set the ADCON0 as %11000001 to use the internal OSC, I want to use the internal OSC
'ADCON0=%10000001 'Set A/D Fosc/32,[4MHz],Channel-0 = on <----???? For External clock?


'ADCON0= 65 + (J*8) 'Set A/D Fosc/8, and Set a/d = on (This is actually ADCON0=%01000001
'65 = 1000001 channel 0,(GP0/AN0)
'73 = 1001001 channel 1,(GP1/AN1)
'81 = 1010001 channel 2,(GP2/AN2)
'89 = 1011001 channel 3,(GP4/AN3)

'".....if you're using the internal ADC RC oscillator, you need to set the ADCON0 bits ADCS1, ADCS0 to 11.
'Using the internal ADC clock is only desirable when running the PIC at low clock frequencies to conserve
'power. If you're using a 4MHz ceramic resonator, crystal or other clock source, you'll need to set these
' bits to either 01, or 10."--Rentron



'ADCON0= 129 + (J*8) 'Set A/D Fosc/32 Use internal 4MHz clock(8 usec to acquire data), and Set a/d = on
'129 = %10000001

ADCON0= 193 + (J*8) 'Use internal 4MHz clock(2-6usec to acquire data), and Set a/d = on
'193 = %11000001


'ADCON0=%11000001 'Use the internal clock
'Do I need to do this every time a channel is read ????
'Any alternate ???
' %11xxxxxx <---This means ADCS1(bit7)=1 and ADCS0(bit6)=1; see table by Rentron/data sheet
' %xxxxxxx1 <---This means ADON (bit0) = 1 ie. turn the A/D on
pauseus 10 'Wait for 10 u Sec for channel setup <---??? is the time OK?