ertank
Posts: 40
Joined: Sat Feb 18, 2017 4:44 pm

Want to measure battery volts

Tue Jul 09, 2019 4:21 pm

Hello,

- I have a set of 24Volts batteries. (3 or 4 total)
- They are connected to this inverter: https://imgyukle.com/i/kK9UrY as power input
- Inverter outputs power to a refrigerator of 220Volts.
- Everything is mobile.

I am asked to measure battery level (volts or percentage or any other way possible).

There are similar questions asked in this forum before. I tried to read them all and even more from out of this forum.

My current plan is to power Raspberry Pi using a wall adapter 220V. Since Raspberry Pi has digital I/O, I think of using ADS1115 module. That module has max 5V analog input. I think of using something like this: https://imgyukle.com/i/kKd441

However;
1- I am not completely sure that will work. I do not have any electric/electronics education. I cannot think of a problem of any kind in this design.
2- I do not know what to do with the open end cable at bottom.
3- I am not sure what tolerance resistors should be. I think I can find 10% ones in the market as usual.

Any suggestions and help is appreciated.

Thanks & regards,
Ertan

W. H. Heydt
Posts: 10881
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Want to measure battery volts

Tue Jul 09, 2019 4:30 pm

ertank wrote:
Tue Jul 09, 2019 4:21 pm
Hello,

- I have a set of 24Volts batteries. (3 or 4 total)
- They are connected to this inverter: https://imgyukle.com/i/kK9UrY as power input
- Inverter outputs power to a refrigerator of 220Volts.
- Everything is mobile.

I am asked to measure battery level (volts or percentage or any other way possible).

There are similar questions asked in this forum before. I tried to read them all and even more from out of this forum.

My current plan is to power Raspberry Pi using a wall adapter 220V. Since Raspberry Pi has digital I/O, I think of using ADS1115 module. That module has max 5V analog input. I think of using something like this: https://imgyukle.com/i/kKd441

However;
1- I am not completely sure that will work. I do not have any electric/electronics education. I cannot think of a problem of any kind in this design.
2- I do not know what to do with the open end cable at bottom.
3- I am not sure what tolerance resistors should be. I think I can find 10% ones in the market as usual.

Any suggestions and help is appreciated.

Thanks & regards,
Ertan
Keep that ADC circuit well away from a Pi or you will let all the magic smoke out. Without looking it up, I wouldn't care to bet that the ADS1115 can take 24v, either.

The digital pins of a Pi are limited to 3.3v.

1. Well... my electrical/electronic education is over 50 years old and hasn't been use much since, but that design looks just plain wrong to me.
3. In my youth, standard resistor were 20% tolerance, so 10% is pretty good and should work (once you design a proper circuit).

dl324
Posts: 122
Joined: Mon May 06, 2019 7:33 pm
Location: Pacific Northwest, USA

Re: Want to measure battery volts

Tue Jul 09, 2019 4:35 pm

ertank wrote:
Tue Jul 09, 2019 4:21 pm
2- I do not know what to do with the open end cable at bottom.
If you're referring to the wire dangling from the bottom of the 4.7k resistor, connect it to GND
3- I am not sure what tolerance resistors should be. I think I can find 10% ones in the market as usual.
5% and 1% should be more common than 10%. What precision do you require for your voltage measurements?

chipace
Posts: 69
Joined: Sat Jun 29, 2019 2:56 am
Location: brown paper bag in a septic tank

Re: Want to measure battery volts

Tue Jul 09, 2019 11:25 pm

You could use an INA260 to measure each battery's voltage (differentially) and you can even measure the current that each battery is supplying to the load. They can measure up to 36V and 15A. You would have one for each battery in your system. They connect to the RPi's i2c bus.
If you are not interested in current measurement there are cheaper ADCs out there.
Just a thought.

https://www.adafruit.com/product/4226
https://github.com/charkster/INA260

wh7qq
Posts: 1325
Joined: Thu Oct 09, 2014 2:50 am

Re: Want to measure battery volts

Wed Jul 10, 2019 1:23 am

The ADS1115 or its cousin, the ADS1015 are both fine A-D converters but are gross overkill for what you are trying to do. If you have not already purchased an RPi and ADS1115, you might consider the Arduino Uno which has 6, ten bit analog inputs which is more than sufficient to your need. A nice tutorial for an Arduino battery monitor is at https://startingelectronics.org/article ... h-arduino/. The resultant circuit is simpler and if you purchase one of the Arduino clones from Amazon, less expensive. You will also find circuits and code to use a small display here as well as the serial (USB) output from the Arduino. The Arduino has a much wider range of power voltage so that the batteries themselves might be used as the source with suitable reduction. The optimal max for the Arduino supply is 12 vdc.

If you have already purchased the RPi and ADS1115, the circuit shown can work if the free end of the 4.7k resistor is tied to ground but it puts the input of the A-D very close to its input absolute max limit of Vdd + .3V so I would be inclined to go to something around 30K for the high value. 5% resistors are fine, as are 10% if they are easier to find. The RPi is very picky about its supply voltage so a good 220 vac to 5.1 vdc regulator (NOT a phone charger) would be a good choice and the same supply voltage can be used for the ADS1115 or better, the 3.3 vdc line on the RPi GPIO, as the 1115 is very low powered. The outputs from the I2C pins on the 1115 will need scaling or level shifting to the maximum allowable 3.3 volts max on any RPi GPIO pin if the 5.1 vdc supply voltage is used. A data sheet for the ADS1115 is at http://www.ti.com/lit/ds/symlink/ads1115.pdf.

ertank
Posts: 40
Joined: Sat Feb 18, 2017 4:44 pm

Re: Want to measure battery volts

Thu Jul 11, 2019 5:07 pm

Thank you to all.

I got updated specs. Batteries are 12V and 4 of them connected together. I have to measure 48V.

I was interested in INV260 unfortunately it is up to 36V.

If I can use resistors I will be back to using them again.

Measurement correctness is required as 1%.

chipace
Posts: 69
Joined: Sat Jun 29, 2019 2:56 am
Location: brown paper bag in a septic tank

Re: Want to measure battery volts

Fri Jul 12, 2019 1:45 am

You could do your voltage divider and use a MCP3008. It is SPI, but it has 4 differential analog inputs. You could measure each battery voltage differentially.

User avatar
davidcoton
Posts: 4131
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Want to measure battery volts

Fri Jul 12, 2019 8:50 am

ertank wrote:
Thu Jul 11, 2019 5:07 pm
Measurement correctness is required as 1%.
To achieve 1% measurement accuracy without calibration, you will need to use better than 1% resistors in every relevant potential divider. You will need high stability components, stable power supplies, a good accurate reference voltage, excellent decoupling, and generally a high standard of design and build. Achievable but not simple.

Alternatively, you can design your system to be stable (and therefore consistent), but not worry about absolute accuracy. Then you can add a calibration method to your system -- either on the analogue (potential divider) section, or by correcting the readings on the digital side (in your program). Either way requires stable components and a high quality build, and a better than 1% reference voltage to calibrate against.

You could monitor each battery individually using a differential input to an A2D. 1% accuracy does not require more that 10 bits conversion. (I hope you can explain why.) You will have to get the scaling right so that the battery voltage (nominal 12V, but what is the maximum on charge?) scales to almost the full range of the A2D.

Alternatively with a 12 bit A2D you could measure the battery voltage at each junction directly and derive the individual battery voltages in software. (Again, explain to yourself why more than 10 bits might be needed. This should be part of your design spec, which I assume is the first milestone of your project.)

Is this a education project? If so I think you will need more help from the teachers/academics -- your electronic design skills are at present some way below what you will need, and I hope no-one here or elsewhere will just give you a complete solution (that could be called cheating, and could lose you any hope of a qualification).
Signature retired

ertank
Posts: 40
Joined: Sat Feb 18, 2017 4:44 pm

Re: Want to measure battery volts

Sat Jul 13, 2019 6:05 pm

davidcoton wrote:
Fri Jul 12, 2019 8:50 am
Is this a education project?
Actually, this is a real life customer job (not mine).
I have some basic knowledge of Raspberry Pi GPIO and so I am asked for the job to be completed.
Unfortunately, I did not get any lectures about electrics or electronics.

User avatar
mikronauts
Posts: 2722
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Want to measure battery volts

Sun Jul 14, 2019 1:25 am

I would suggest hiring someone with appropriate knowledge and experience to do the work.

If it was a one-of for your own personal use, and you were learning, that is one thing, although still not wise without appropriate experience.

If the customer wanted a one-of and was not only aware of your lack of apropriate background, and you had a written waiver from the customer, you should stll pass.

Note that such a setup is potentially lethal.
ertank wrote:
Sat Jul 13, 2019 6:05 pm
davidcoton wrote:
Fri Jul 12, 2019 8:50 am
Is this a education project?
Actually, this is a real life customer job (not mine).
I have some basic knowledge of Raspberry Pi GPIO and so I am asked for the job to be completed.
Unfortunately, I did not get any lectures about electrics or electronics.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
jbeale
Posts: 3491
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Want to measure battery volts

Sun Jul 14, 2019 1:50 am

If you are not comfortable with electronic design, I do not recommend working with +48V from a high-current source (anything strong enough to run a refrigerator probably qualifies). Accidentally shorting out a power supply like that could be rather spectacular in a not-so-good way.

It may seem too big and expensive, but a basic digital voltmeter like a UNI-T UT61B (or various other models) comes with a RS-232 serial or USB interface so it can connect to the Pi or any other computer. The good thing here is it is completely (optically) isolated from the Pi, so there is no electrical noise coupled and no shock hazard, the meter can even handle AC line voltage and +48V DC is no problem.

I have the UNI-T UT61E which has higher resolution (4-1/2 digits instead of 3-1/3). It sends out about 2 readings per second out the data port. I have hooked it up to my computer and taken readings for many days. (Normally it runs on an internal battery but I made a fake "9V battery" connected to a 9V wall-wart supply). If you do that, you do need to take care about the isolation of your power supply, but it would be true of other approaches as well.

chipace
Posts: 69
Joined: Sat Jun 29, 2019 2:56 am
Location: brown paper bag in a septic tank

Re: Want to measure battery volts

Sun Jul 14, 2019 5:33 am

You could make a proof of concept setup using 4 regular 9V batteries. That is what I would do before using very large and high-current batteries. I would keep your 3-to-1 ratio for the voltage divider even though a 2-to-1 (with 5V Vref) would be more accurate.
I think I will give this a try tonight.

chipace
Posts: 69
Joined: Sat Jun 29, 2019 2:56 am
Location: brown paper bag in a septic tank

Re: Want to measure battery volts

Sun Jul 14, 2019 6:53 am

I tried a stack of 4 AAA rechargeable batteries (no resistor divider) and a MCP3008. I was able to read each battery voltage.
First battery positive terminal connected to CH0, negative terminal connected to CH1... final battery positive terminal connected to CH6 and negative terminal to CH7. I then read 2, 3 and 4 battery stack voltages (moved the first battery away from CH0/CH1, and used that differential channel to read the various stack voltages.

MCP3008.py

Code: Select all

#!/usr/bin/python

import spidev

# ============================================================================
# MCP3008 8 channel 10-Bit ADC
# ============================================================================

class MCP3008 :
    spi = None
    
    _MAX_ADC_VAL = 1023

    # Constructor
    def __init__(self, spi_device=0, spi_channel=0, max_speed_hz = 100000, adc_vcc=5.0, debug=False): # 100kHz
	self.spi = spidev.SpiDev(spi_device, spi_channel)
        self.spi.max_speed_hz = max_speed_hz
        self.adc_vcc = adc_vcc
        self.debug = debug

    def read_voltage(self, channel, diff=False):
	# Only 8 channels 0 thru 7, else return -1
	if ((channel > 7) or (channel < 0)):
	    return -1
           
	# Send start bit, sgl/diff, odd/sign, MSBF
	# channel = 0 sends 0000 0001 1000 0000 0000 0000
	# channel = 1 sends 0000 0001 1100 0000 0000 0000
	# sgl/diff = 1; odd/sign = channel; MSBF = 0
	if (diff == False):    
	    r = self.spi.xfer([1,(8+channel)<<4,0])
	else:
	    r = self.spi.xfer([1,(0+channel)<<4,0])
        
	ret = ((r[1]&3) << 8) + (r[2])
	voltage = ret * self.adc_vcc / self._MAX_ADC_VAL
	if (self.debug):
	    print "r0, R1, R2 is 0x%2x,0x%2x,0x%2x " %(r[0], r[1], r[2])
	    print "Raw hex value read is 0x%3x" % ret
	    print "ADC channel %d voltage is %0.2f" % (channel,voltage)
	return voltage    

test_mcp3008.py

Code: Select all

#!/usr/bin/python

from MCP3008 import MCP3008

mcp3008 = MCP3008(spi_device=0, spi_channel=0, adc_vcc=5.0)

print "Reading ADC value on channel 0, differential"
adc_value = mcp3008.read_voltage(channel=0, diff=True)
print "ADC value is %f" % adc_value

https://www.adafruit.com/product/856

ertank
Posts: 40
Joined: Sat Feb 18, 2017 4:44 pm

Re: Want to measure battery volts

Sun Jul 14, 2019 11:11 am

chipace wrote:
Sun Jul 14, 2019 6:53 am
I tried a stack of 4 AAA rechargeable batteries (no resistor divider) and a MCP3008. I was able to read each battery voltage.
First battery positive terminal connected to CH0, negative terminal connected to CH1... final battery positive terminal connected to CH6 and negative terminal to CH7. I then read 2, 3 and 4 battery stack voltages (moved the first battery away from CH0/CH1, and used that differential channel to read the various stack voltages.
I ordered one MCP3008 and waiting for it to arrive. In the mean time, I have some questions as to all wiring.

- What ports on MCP3008 did you connect Pi?
-Does MCP3008 power on from bateries connected? Or Does it need external power?

If possible a complete drawing is appreciated just for one battery.

Thanks a lot.

User avatar
B.Goode
Posts: 8501
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Want to measure battery volts

Sun Jul 14, 2019 12:11 pm

The mcp3008 ADC chip is supported by the gpiozero python library which is a standard part of Raspbian.

The documentation includes code and wiring diagrams, from Section 2.26 onwards:
https://gpiozero.readthedocs.io/en/stab ... entiometer

chipace
Posts: 69
Joined: Sat Jun 29, 2019 2:56 am
Location: brown paper bag in a septic tank

Re: Want to measure battery volts

Sun Jul 14, 2019 4:00 pm

Here is the datasheet.
http://ww1.microchip.com/downloads/en/D ... 21295d.pdf

Two parts share the same datasheet, the 4 channel MCP3004 and the 8 channel MCP3008. They are identical except the number of ADC channels. The MCP3008 is taller as it has 2 more pins.
Here is the 40pin header diagram.
https://pi4j.com/1.2/pins/model-3b-rev1.html

Here is how I wired it up to the RPi.
MCP3008 Pin --> Raspberry Pi Header Pin
Vdd --> 5V
Vref --> 5V
AGND --> (floating, not connected)
CLK --> SCLK (SPI)
Dout --> MISO (SPI)
Din --> MOSI (SPI)
CS/SHDN --> CE0 (SPI)
DGND --> Ground

If you want to repeat my setup (and use AAA batteries).
MCP3008 Pin --> AAA Batteries
CH0 --> BATTERY #1 Positive
CH1 --> BATTERY #1 Negative
CH2 --> BATTERY #2 Positive
CH3 --> BATTERY #2 Negative
CH4 --> BATTERY #3 Positive
CH5 --> BATTERY #3 Negative
CH6 --> BATTERY #4 Positive
CH7 --> BATTERY #4 Negative

If you are using my python code, to read each battery voltage...

Code: Select all

bat1_value = mcp3008.read_voltage(channel=0, diff=True)
bat2_value = mcp3008.read_voltage(channel=2, diff=True)
bat3_value = mcp3008.read_voltage(channel=4, diff=True)
bat4_value = mcp3008.read_voltage(channel=6, diff=True)
Here is the wikipedia page if you want to learn more about SPI.
https://en.wikipedia.org/wiki/Serial_Pe ... _Interface

chipace
Posts: 69
Joined: Sat Jun 29, 2019 2:56 am
Location: brown paper bag in a septic tank

Re: Want to measure battery volts

Sun Jul 14, 2019 4:07 pm

Sorry one typo.
MCP3008 Pin --> Raspberry Pi Header Pin
Vdd --> 5V
Should be:
Vdd --> 3.3V

All the SPI pins are 3.3V, which means the Vdd should also be 3.3V

User avatar
davidcoton
Posts: 4131
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Want to measure battery volts

Sun Jul 14, 2019 7:13 pm

chipace wrote:
Sun Jul 14, 2019 4:00 pm
Here is how I wired it up to the RPi.
MCP3008 Pin --> Raspberry Pi Header Pin
Vdd --> 3V3
Vref --> 5V
(Quoted with correction)
There are two problems with that:
  1. Vref should not exceed Vdd
  2. You need a noise-free reference, 3V3 and (worse) 5V Pi supplies are not suitable. You should use a high-precision (better than 1%) reference chip, probably derived from 5V.
There is significant design work to do in determining the range of voltage you need to measure, and scaling the inputs to make the best use of the range available from your chosen Vref.
Signature retired

chipace
Posts: 69
Joined: Sat Jun 29, 2019 2:56 am
Location: brown paper bag in a septic tank

Re: Want to measure battery volts

Sun Jul 14, 2019 8:27 pm

Vref should not exceed Vdd
That's right... the original instructions are best. The sclk, mosi and miso are fine with being at 3.3V with the Vdd at 5.0V.
Brain fart.

You can power the RPi (I recommend the RPi Zero) from one of the 12V batteries with a step-down buck converter.
Here is one:
https://www.adafruit.com/product/1385

Using the buck regulated 5V as a reference for your ADC will have inaccuracies.
It will be more accurate to use a separate LDO regulator as your reference (don't worry about it until you are comfortable using the MCP3008 with the RPi's 5V as a Vref).
https://www.adafruit.com/product/2236

Are you going to show the battery voltages on a LCD screen, or use a web server on the RPi?

If you are just wanting to show the individual battery voltages, you could just have standalone LCD voltmeters like these:
https://www.amazon.com/MakerFocus-Digit ... B07MX5QG5W

chipace
Posts: 69
Joined: Sat Jun 29, 2019 2:56 am
Location: brown paper bag in a septic tank

Re: Want to measure battery volts

Sun Jul 14, 2019 8:52 pm

I saw this on www.tindie.com, it is a MCP3008 hat with resistor dividers on-board.
https://www.tindie.com/products/cburges ... ry-pi-v21/
Not sure of how it will perform, but it looked interesting.
It doesn't have a separate LDO for a Vref.

User avatar
IOTON
Posts: 95
Joined: Tue Jul 02, 2019 1:00 pm
Contact: Website

Re: Want to measure battery volts

Mon Jul 15, 2019 6:02 am

Just add correct resistor dividers.
Also, you will need a rectifier diode for AC voltage measurements.

chipace
Posts: 69
Joined: Sat Jun 29, 2019 2:56 am
Location: brown paper bag in a septic tank

Re: Want to measure battery volts

Mon Jul 15, 2019 6:07 am

I was also thinking about the effective voltage range that you are going to measure on each battery. I am thinking that if the 12V battery drops to 6V, it will be in deep cycle. Also, open circuit measurement of a 12V battery can be as high as 13V. Maybe your effective measurement range would be between 6V to 13V. If you used multiple LDO regulators, this could allow you to use the majority of your ADC range (10bits) in your desired voltage measurement range (6V to 13V).

I drew a diagram of what that circuit might look like:
https://imgur.com/LN99Wux

Maybe one of the 4 LDOs could also be used as the ADC Vref.

User avatar
IOTON
Posts: 95
Joined: Tue Jul 02, 2019 1:00 pm
Contact: Website

Re: Want to measure battery volts

Mon Jul 15, 2019 7:04 am

chipace wrote:
Mon Jul 15, 2019 6:07 am
I was also thinking about the effective voltage range that you are going to measure on each battery. I am thinking that if the 12V battery drops to 6V, it will be in deep cycle. Also, open circuit measurement of a 12V battery can be as high as 13V. Maybe your effective measurement range would be between 6V to 13V. If you used multiple LDO regulators, this could allow you to use the majority of your ADC range (10bits) in your desired voltage measurement range (6V to 13V).

I drew a diagram of what that circuit might look like:
https://imgur.com/LN99Wux

Maybe one of the 4 LDOs could also be used as the ADC Vref.
you don't need any LDO for tasks like this. use ads1115 it has an internal reference. you will able to get 16adc channels on one i2c bus,

ertank
Posts: 40
Joined: Sat Feb 18, 2017 4:44 pm

Re: Want to measure battery volts

Mon Jul 15, 2019 9:35 am

I appreciate all help.

I have a basic question: Can I read individual batteries which are serial connected to each other?

What I understand from this drawing is this is fine: https://imgur.com/LN99Wux

Just want to be sure about it.

Thanks & regards,
Ertan

ertank
Posts: 40
Joined: Sat Feb 18, 2017 4:44 pm

Re: Want to measure battery volts

Mon Jul 15, 2019 9:39 am

chipace wrote:
Sun Jul 14, 2019 8:27 pm
Are you going to show the battery voltages on a LCD screen, or use a web server on the RPi?
I am going to upload readings to a web server in the cloud. RPi will be headless.

Return to “General discussion”