TAAugust
Posts: 11
Joined: Fri Nov 17, 2023 2:23 pm

Measuring voltage of batteries

Thu Nov 23, 2023 11:55 pm

Hi,

I'm building a robot with a raspberry which I plan to power with 2 different battery sets of 2 18650 lithium ion batteries: 2 in the UPS HAT (B) to power my pi, and 2 connected in parallel to a motor driver (controlling 2 DC motors) and a 5v regulator (to power my servos)

I would like to measure the voltage across the 2 batteries being used to power the motors so that once the voltage reaches 6V (or just above) then the raspberry pi switches a relay off to shut off power to the motors. This is because the 5v regulator I have has a minimum input voltage of 6V.

Please could someone advise me as to how I can do this? I've done a bit of research and it seems I'll need to use an analogue to digital converter since the pi has no analogue pins built in but I've never used one of these before and I'm not sure what I need to do.

I'm also not sure what relay I need to get to control this. The maximum current output of my batteries is 10A and the operating voltages of them are 5-8.4V. All the relays I've found so far either have a maximum current of less than 10A or have a switching voltage of up to 120V for example, some of which also had a minimum switching voltage of about 20V. Is there an in between for the type of thing I want to do? or is it ok to use a relay with a higher switching voltage than what I'll be using it for?

pcmanbob
Posts: 13746
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Measuring voltage of batteries

Fri Nov 24, 2023 10:14 am

To measure voltage you are correct in needing an ADC , a common one to use on the pi is the MCP3008 there are lots of examples of how to connect this to a pi on the web and you will find an example in the physical computing guide.
https://projects.raspberrypi.org/en/pro ... -computing

As for the relay I assume you just want to to switch the batteries on and off.

If you are using a mechanical relay the any relay with a suitable coil voltage 5V is often used with a simple driver transistor circuit to enable the 3.3V gpio to switch 5V, and relay contact with a rating equal or greater than you battery/motor voltage/current requirement.

You only need to worry about minimum switch voltages is you are using solid state relays and then you have the problem that many can only switch AC volts and DC volts.

a suitable relay might be songle-relay-spdt-10a SRD-5VDC-SL-C
these are available from may only suppliers
example https://www.ebay.co.uk/itm/122195388493 ... %3A4429486

This is the simplest for of driver circuit

Image

but you many want to drive it using an opto isolator to prevent interference on the 5V pin on the pi and use the 5V from the battery to power the relay, especially of you are using more than one relay.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jayben
Posts: 569
Joined: Mon Aug 19, 2019 9:56 pm

Re: Measuring voltage of batteries

Fri Nov 24, 2023 11:15 am

You say that the 5v regulator has a minimum input voltage of 6V, then surely you want to disconnect the motor drive if the voltage is below 6V, not above?

Anyway, you don't have to use an ADC to detect a single voltage threshold, you can use a part such as a TS431 which switches on when its input pin reaches 2.5V; so a simple potential divider on that pin will raise the switching level to 6V, or whatever you like.

The resulting output can either be used to switch the relay (via a suitable higher-current driver transistor) or can be fed into an I/O pin to provide a digital signal for the CPU to handle.

ghp
Posts: 3670
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany

Re: Measuring voltage of batteries

Sat Nov 25, 2023 1:13 pm

You should use LiPo batteries with auto protection which shut off when reaching min voltage. Then no need to have a relay or othe r means to shut off motors.
Good idea to measure batt voltage anyway. You could run an emergency program when batt goes low, trying to reach home base. As voltage of battery id higher than voltage range of RPi, you need to build a voltage divider from two resistors for the ADC.

When using a relay for shut off, then the relay coil needs around 60mA to 100mA. Perhaps better to look for a high side mosfet switch (you need a n-channel and p-channel on high side capable of driving the motors). How much current do the motors need ?

TAAugust
Posts: 11
Joined: Fri Nov 17, 2023 2:23 pm

Re: Measuring voltage of batteries

Sun Nov 26, 2023 10:41 pm

pcmanbob wrote:
Fri Nov 24, 2023 10:14 am
To measure voltage you are correct in needing an ADC , a common one to use on the pi is the MCP3008 there are lots of examples of how to connect this to a pi on the web and you will find an example in the physical computing guide.
https://projects.raspberrypi.org/en/pro ... -computing

As for the relay I assume you just want to to switch the batteries on and off.

If you are using a mechanical relay the any relay with a suitable coil voltage 5V is often used with a simple driver transistor circuit to enable the 3.3V gpio to switch 5V, and relay contact with a rating equal or greater than you battery/motor voltage/current requirement.

You only need to worry about minimum switch voltages is you are using solid state relays and then you have the problem that many can only switch AC volts and DC volts.

a suitable relay might be songle-relay-spdt-10a SRD-5VDC-SL-C
these are available from may only suppliers
example https://www.ebay.co.uk/itm/122195388493 ... %3A4429486

This is the simplest for of driver circuit

Image

but you many want to drive it using an opto isolator to prevent interference on the 5V pin on the pi and use the 5V from the battery to power the relay, especially of you are using more than one relay.
Great thank you, that was very helpful!

Yea I just want to be able to switch the batteries on and off so that I don't need to worry about them getting too low and damaging components.

To make it even simpler, would I be able to use a relay that uses a 3.3v switching voltage and connect that directly to a gpio pin and ground? or would this draw too much current and damage the pin?
Its not that clear to me but it looks as if this uses 3.3v for the control circuit: https://www.ebay.co.uk/itm/134212229550 ... 30QAvD_BwE
It seems as if both the relay I have sent here and the one you linked to before have a minimum switching voltage of 28V (I assume thats what the lower number means). I know you said that this doesn't matter for this case but I don't understand why they'd give it if it doesn't matter. Are you able to briefly explain why or link me to something that does?

EDIT( As someone else said above, the relay will draw about 60-100mA, is that right? If so I wont be able to do this arrangement with just the 3.3v gpio pins will I as the maximum current output of the gpio pins is 16mA each so it seems my only option is to use the transistor setup either with a 5v or 3.3v relay using the power pins on the gpio header)

I would only be using 1 relay so would it be ok to not use the opto isolator? The other batteries are in a pi hat that monitors them for me and regulates the 5v to the pi.
And when you say power the relay with 5v from the battery do you mean from the regulator I'm controlling my servos with? Since I want to turn the battery off with the relay then surely this wont work as I want to turn off the battery completely and then once its off I wont be able to turn the battery back on without it already being on.

Would I also be able to measure the current of the batteries by attaching a small resistor to their output and measuring their current? I have some resistors but I'm not sure if they can withstand 10A so I might have to look into others if this is feasible.
Last edited by TAAugust on Sun Nov 26, 2023 11:03 pm, edited 1 time in total.

TAAugust
Posts: 11
Joined: Fri Nov 17, 2023 2:23 pm

Re: Measuring voltage of batteries

Sun Nov 26, 2023 10:49 pm

jayben wrote:
Fri Nov 24, 2023 11:15 am
You say that the 5v regulator has a minimum input voltage of 6V, then surely you want to disconnect the motor drive if the voltage is below 6V, not above?

Anyway, you don't have to use an ADC to detect a single voltage threshold, you can use a part such as a TS431 which switches on when its input pin reaches 2.5V; so a simple potential divider on that pin will raise the switching level to 6V, or whatever you like.

The resulting output can either be used to switch the relay (via a suitable higher-current driver transistor) or can be fed into an I/O pin to provide a digital signal for the CPU to handle.
The batteries will be discharging so their voltage will be decreasing (from 8.4V to 5V for my batteries) and I don't want it to get exactly to 6V as this is the limit so thats why I said just above 6V.

That also sounds like a good idea although I'd like to get a ADC anyway so that I can measure other things easily if needed.

TAAugust
Posts: 11
Joined: Fri Nov 17, 2023 2:23 pm

Re: Measuring voltage of batteries

Sun Nov 26, 2023 11:01 pm

ghp wrote:
Sat Nov 25, 2023 1:13 pm
You should use LiPo batteries with auto protection which shut off when reaching min voltage. Then no need to have a relay or othe r means to shut off motors.
Good idea to measure batt voltage anyway. You could run an emergency program when batt goes low, trying to reach home base. As voltage of battery id higher than voltage range of RPi, you need to build a voltage divider from two resistors for the ADC.

When using a relay for shut off, then the relay coil needs around 60mA to 100mA. Perhaps better to look for a high side mosfet switch (you need a n-channel and p-channel on high side capable of driving the motors). How much current do the motors need ?
I have batteries with a protective tip but they switch off at 2.5V, I could get other batteries that switch off at 3V however I've already got these batteries so I'd like to use them.

When you say use a voltage divider, should I just use 2 high resistance resistors in series with each other but in parallel with all other components in order to ensure the current through them is low?
Does the mosfet not need any current draw then? I've never used mosfets before so I'm unsure of what they do.
I got the motors as part of a robot kit and they dont have any markings on their outer casing so I cant get a data sheet for them, however I'm going to test them and find out soon.

ame
Posts: 8733
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Measuring voltage of batteries

Mon Nov 27, 2023 1:25 am

You don't need a relay to cut the power to the motors. If you detect that the batteries are low then set a software flag and just don't run the motors if the flag is set.
Oh no, not again.

ame
Posts: 8733
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Measuring voltage of batteries

Mon Nov 27, 2023 3:02 am

One thing to add. You'll get a different reading for the battery voltage when the motors are off, and when they are under load. You can choose when you measure the voltage, because you are in control of the motors. When you do, make sure you have a bit of hysteresis in your code that decides whether to allow the motors to run or not.
Oh no, not again.

pcmanbob
Posts: 13746
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Measuring voltage of batteries

Mon Nov 27, 2023 10:18 am

You can't use a relay with a 3V coil and drive it from the gpio pin directly because a gpio pin can only supply 16mA and a relay will draw 100mA or more, you cant even power the relay from the 3.3V on the pi gpio header because doing so would put likely cause fluctuations of the 3.3V supply and crash and possibly damage you pi.

Even driving relay from the 5V gpio header pin can cause the pi to crash if you have a lot connected to the pi which is why I said use the 5V servo supply to power your relay that way you can make the 5V servo supply bigger and able to carry both the servo and relay load, especially if switching more that one relay.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

drgeoff
Posts: 13950
Joined: Wed Jan 25, 2012 6:39 pm

Re: Measuring voltage of batteries

Mon Nov 27, 2023 10:53 am

jayben wrote:
Fri Nov 24, 2023 11:15 am
Anyway, you don't have to use an ADC to detect a single voltage threshold, you can use a part such as a TS431 which switches on when its input pin reaches 2.5V;
A TL431 is more commonly available.
Quis custodiet ipsos custodes?

TAAugust
Posts: 11
Joined: Fri Nov 17, 2023 2:23 pm

Re: Measuring voltage of batteries

Mon Nov 27, 2023 6:33 pm

ame wrote:
Mon Nov 27, 2023 1:25 am
You don't need a relay to cut the power to the motors. If you detect that the batteries are low then set a software flag and just don't run the motors if the flag is set.
If it becomes too complicated and large to fit on my robot I might do this, but I’d prefer yo shut off the battery completely with a relay because the 5v regulator would still use some power even if none of the motor are on.

TAAugust
Posts: 11
Joined: Fri Nov 17, 2023 2:23 pm

Re: Measuring voltage of batteries

Mon Nov 27, 2023 6:33 pm

ame wrote:
Mon Nov 27, 2023 3:02 am
One thing to add. You'll get a different reading for the battery voltage when the motors are off, and when they are under load. You can choose when you measure the voltage, because you are in control of the motors. When you do, make sure you have a bit of hysteresis in your code that decides whether to allow the motors to run or not.
Oh ok, I didn’t realise that, I’ll try and integrate that into the code

TAAugust
Posts: 11
Joined: Fri Nov 17, 2023 2:23 pm

Re: Measuring voltage of batteries

Mon Nov 27, 2023 6:34 pm

pcmanbob wrote:
Mon Nov 27, 2023 10:18 am
You can't use a relay with a 3V coil and drive it from the gpio pin directly because a gpio pin can only supply 16mA and a relay will draw 100mA or more, you cant even power the relay from the 3.3V on the pi gpio header because doing so would put likely cause fluctuations of the 3.3V supply and crash and possibly damage you pi.

Even driving relay from the 5V gpio header pin can cause the pi to crash if you have a lot connected to the pi which is why I said use the 5V servo supply to power your relay that way you can make the 5V servo supply bigger and able to carry both the servo and relay load, especially if switching more that one relay.
Ok thanks. I’ll try to use the 5v regulator to power it then, but how will I make this work since I want to turn off the 5v regulator with the relay as well?

ame
Posts: 8733
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Measuring voltage of batteries

Mon Nov 27, 2023 7:01 pm

What voltage regulator are you using? Does it have an enable pin?
Oh no, not again.

TAAugust
Posts: 11
Joined: Fri Nov 17, 2023 2:23 pm

Re: Measuring voltage of batteries

Tue Nov 28, 2023 3:27 am

ame wrote:
Mon Nov 27, 2023 7:01 pm
What voltage regulator are you using? Does it have an enable pin?
I’m using this regulator: https://thepihut.com/products/sparkfun- ... 1052419267
It looks like it does. Should I connect the motor driver to the battery through the relay and then connect the regulator directly to the battery and switch this on and off with the enable pin instead? That way I can turn on the regulator and then close the relay.

ame
Posts: 8733
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Measuring voltage of batteries

Tue Nov 28, 2023 5:04 am

TAAugust wrote:
Tue Nov 28, 2023 3:27 am
ame wrote:
Mon Nov 27, 2023 7:01 pm
What voltage regulator are you using? Does it have an enable pin?
I’m using this regulator: https://thepihut.com/products/sparkfun- ... 1052419267
It looks like it does. Should I connect the motor driver to the battery through the relay and then connect the regulator directly to the battery and switch this on and off with the enable pin instead? That way I can turn on the regulator and then close the relay.
No. I was thinking of driving the enable pin to turn the regulator on and off instead of using a relay. Read the data sheet and do some experiments.
Oh no, not again.

ghp
Posts: 3670
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany

Re: Measuring voltage of batteries

Tue Nov 28, 2023 5:12 am

The regulator board from sparkfun has an enable pin, but this is connected to a resistor to VIN which makes it unsuitable for direct control by a GPIO pin. Could still be controlled by using a 2N2222 transistor and a resistor.

There are two approaches discussed so far:
- use a relay to switch on/off the motors and regulator board, needs a transistor and resistor to drive the relay
pro: OP feels comfortable with this solution
con: additional current for relay coil needed; space needed
- switch off motors by using the motor driver chip; switch off regulator by enable pin (with a transistor and resistor)
pro: no relay needed, no additional current for relay
con: (I consider this a good solution)

There is a third option: use a simple switch on the robot. Low space, no additional current and assuming the robot is not out of range it can be operated manually.

TAAugust
Posts: 11
Joined: Fri Nov 17, 2023 2:23 pm

Re: Measuring voltage of batteries

Wed Nov 29, 2023 11:28 am

ghp wrote:
Tue Nov 28, 2023 5:12 am
The regulator board from sparkfun has an enable pin, but this is connected to a resistor to VIN which makes it unsuitable for direct control by a GPIO pin. Could still be controlled by using a 2N2222 transistor and a resistor.

There are two approaches discussed so far:
- use a relay to switch on/off the motors and regulator board, needs a transistor and resistor to drive the relay
pro: OP feels comfortable with this solution
con: additional current for relay coil needed; space needed
- switch off motors by using the motor driver chip; switch off regulator by enable pin (with a transistor and resistor)
pro: no relay needed, no additional current for relay
con: (I consider this a good solution)

There is a third option: use a simple switch on the robot. Low space, no additional current and assuming the robot is not out of range it can be operated manually.
Thanks for all the help and advice, as it’s becoming more complicated to do this automatically I think I’ll just add a switch for now and use an indicator light to know when it’s getting low and I should switch it off. Then later when I expand the project I’ll add in an automatic way of switching off the batteries.

Thanks again

Return to “Automation, sensing and robotics”