United Kingdom
Asked — Edited

Getadc

I'm playing with the voltage monitor for my bot and using the GetADC(ADC Port) script command but having some slight issues with the values returned.

At first I thought it could be my code so I wrote a script to get the adc values from each of the ADC ports and add them as variables.


$vc1 = GetADC(ADC0)
$vc2 = GetADC(ADC1)
$vc3 = GetADC(ADC2)
$vc4 = GetADC(ADC3)
$vc5 = GetADC(ADC4)
$vc6 = GetADC(ADC5)
$vc7 = GetADC(ADC6)
$vc8 = GetADC(ADC7)

In the variable watcher I have the following results - this is without anything connected to the ports.

Port Value ADC0 32 ADC1 241 ADC2 136 ADC3 45 ADC4 14 ADC5 4 ADC6 1 ADC7 255

And when I attach the live of my cells (currently reading 4.09v & 4.06v) to the ADC signal on ADC0 and ADC1 I get

Port Value Description ADC0 255 (4.09v cell) ADC1 154 (4.06v cell)

Something isn't right surely? As far as I can see the GetADC command should report back a value between 0 and 255 for the voltage on the signal pin (0 being 0v and 255 being 5v), is that right? Do I have a problem with my board or software? Or have I not wired it correctly (I have nothing on the VCC and Gnd pins and the +v from each of the cells to ADC0 and ADC1)


ARC Pro

Upgrade to ARC Pro

Experience the transformation – subscribe to Synthiam ARC Pro and watch your robot evolve into a marvel of innovation and intelligence.

United Kingdom
#1  

My full code for the reading of the battery cells is


# Read voltage level of each cell on LiPo battery balance port and report to software.
# Set ADC factor
$factor = 0.019607843
:ReadCells
# Get ADC values
$vc1 = GetADC(ADC0)
$vc2 = GetADC(ADC1)
# Convert values to voltage
$cell1 = $vc1 * $factor
$cell2 = $vc2 * $factor
# Check if below recommended levels
IF ($vc1 <3.5)
  # Do sometihing if voltage low
  Print("Cell 1 Low")
ELSEIF ($vc2 <3.5)
  # Do sometihing if voltage low
  Print("Cell 2 Low")
ELSE 
  # Wait 5 seconds
  Sleep(5000)
  # Go back to the start
  Goto(ReadCells)
ENDIF 
  # Go back to the start
Goto(ReadCells)

It's far from complete, the if's will eventually tell it to go charge up but for now just print a warning. The main thing is the values returned from the ADC ports.

Any help would be very much appreciated as I can't see what I'm doing wrong.

United Kingdom
#2  

And the schematic of how it is wired. User-inserted image

Edit: I just had a thought though, do I need to bring the ground from the battery to the ADC port too?

As I'm using a LiPo which are notorious for being dangerous if misused I'm hesitant to just go connecting things up "willy nilly"

#3  

Hi Rich,

I'm using all my ADC ports on one of the EZ-B boards in my B9. However I'm using DC power from a plug in power converter but there should not be much difference between that and a battery (cell as you all call them on the other side of the pond;) ). Anyway, what I have attached to the ports are both pots and momentary switches. I have no grounds going to the switches, just the 5v and signal from each ADC port to the switchs I want to monitor. That way EZ-B will see the 5v when the switch is closed. The pot is different; from an ACD port I have 5v and a ground going to the outside lugs and the signal going to the center lug. That way EZ-B will see the change in voltage when I turn the knob on the pot.

I would think you would have to have a return path for the voltage so I would expect you would need to bring the ground from your battery to the EZ-B. Now I'm not sure if you should attach it the the ground of the ADC port or the negative side of the main power input of the EZ-B board. Check continuity between the ground pin of the ADC and the negative side of the power input to see if they are tied. If so then its not an issue. If not I'd start by attaching to the negative side of the main power input. I know that's the way to hook up a H-bridge to EZ-B.

One last thing. The ADC ports will only handle voltage up to 5vdc. However this should not be an issue for you.

Wear a pair of safety glasses or cover up the battery.

Let me know what happens. eek

Have fun, Dave

United Kingdom
#4  

I think ill try with a ground to the ezb adc ports too when I get home.

I was a little puzzled by the adc values with nothing attached but maybe its nothing?

#5  

Any kind of electricity naturally flows to ground (hopefully not through you). You need a path for it to move or it just stays where it is or dissipates back into nature. ;)

PRO
Synthiam
#6  

Dave saves the day :)

The lipo (long as it is below 5v) does not need to have the positive (+) connected. Only the GND (-) and Signal (+) should be connected. So connect the GND from the lipo to the GND on the ADC port. And connect the Positive from the lipo to the Signal on the ADC port.

:)

#7  

I am also really interrested in monitoring battery voltage for my bot. However, I am currently using a 9v battery (obviously more than the 5v handled by ADC ports)... Is there a trick with ADC? or another way?

United Kingdom
#8  

Only doing the LiPo cells which are 4.2v each when fully charged so within the 5v.

Such a simple problem and I could not solve it earlier for love nor money, yet now I look at it with a clear head (and the replies - thanks!) it's so obvious I have no idea how I missed it.

Thanks Dave & DJ. Now to make up a small lead and see what happens :)

United Kingdom
#9  

@fredebec, that was my dilemma with the 6v SLA I planned. You can use a voltage divider to basically split the voltage in half, 4.5v in your case, which is under the 5v max threshold. ARC will report back a value, multiply it by the factor 0.019607843 (5/255) and double it.

As for voltage divers circuits, I did knock one up once but it was very primitave, just 2 resistors of equal value from Vcc to Ground, tap the signal off from between the 2 resistors... I don't have it drawn up though, but google voltage divider and a lot of examples come up.

#10  
United Kingdom
#11  

Wow, it looks like it was a combination of a few things but what I didn't realise is that S2 of the LiPo balance plug actually measures both cells voltage so I was feeding over 8V in to ADC1 which must have been giving it problems. Disconnecting one of the two leads (the one with the full voltage) and the other works fine.

I think I will leave it just checking one cell, they are balanced and the charging is at 80mA on a 5000mAh battery so remains very balanced, it's not how I wanted it but it works.

I'll look in to it more when I get chance, I guess that's why I should have at least looked up the balance plug wiring configuration before doing this but no harm no foul I guess, I'm just glad the 8v to the ADC port didn't fry anything.

Edit: I tell you what, no sleep makes me forget the most simple of things. I know how a 2s lipo is wired yet totally forgot that. Ground to 1s is across 1 batter, ground to 2s is across both. 1s to 2s is across the other battery.

but, is it safe to connect the ground from ADC1 to the +v of battery 1 in order to measure the voltage across that battery?

#12  

Awesome thanks to both Rich and Dave I have an idea to monitor my EZ-Board voltage and test out my ADC ports as well. The Zener will reduce the battery pack of 8.5 volts down under 5 and viola! It does feel great to "bag a solution" I know Rich has bagged many and now Dave has just added one to his list. I bagged one the other day and WOW. Once again thanks to Rich for the voltage script which we can all use!

#13  

Rich 2 questions regarding your voltage monitor script...what is the $factor = 0.019607843 varible used for and what are the "chicken feet" symbols? I am going to guess and say multiply?! Thanks a bot

United Kingdom
#14  

The $factor is 5/255, it's actually not required but as I was having problems I didn't know if I had the factor right so used a variable so I could change it easily.

Basically, the ADC signal can receive up to 5v, ADC read reports back a value between 0 and 255 depending on the voltage. 255 being 5v and 0 being 0v. So, to convert that figure to voltage you need to multiply the ADC read value by 5/255. Hopefully that made sense.

Chicken feet symbols? I don't know what symbol you are referring to... * or # ?

United Kingdom
#15  

Updated voltage monitor script for a single battery


# Read voltage level of each cell on LiPo battery balance port and report to software.
# Set ADC factor
$factor = 0.019607843
:ReadCells
# Get ADC values
$adc0 = GetADC(ADC0)
# Convert values to voltage
$volts = $adc0 * $factor
# Adjust voltage if using reducer, change 1 to whatever factor needed.
$actualvolts = $volts * 1
# Check if below recommended levels
IF ($actualvolts <3.5)
  # Do sometihing if voltage low
  Print("Battery Low")
ELSE 
  # Wait 5 seconds
  Sleep(5000)
  # Go back to the start
  Goto(ReadCells)
ENDIF 
  # Go back to the start
Goto(ReadCells)

$factor is 5/255 which converts the ADC value to volts. In the IF ($volts <3.5) you can change the 3.5 to whatever voltage you want it to tell you that it's low. This is based on a battery of less than 5v, if you use a voltage reducer you would need to multiply $volts again, if halved the voltage multiply by 2, if quartered multiply by 4 etc.

I don't know how "clean" my code is, I generally end up with a lot of nests of IFs and ELSEs and a lot of variables that I suspect could probably be avoided. Feel free to alter it however you wish.

Edit: Replace the;) with a ) the code function must do something to confuse it with me wanting to put a;) there

#16  

Totally! Thanks for explanation! chicken feet=* LOL the way they were pasted makes the * look like a chicken foot. :)

United Kingdom
#17  

Ah, I see. I had though that was the case but wasn't sure.

  • means multiply. So if you were to write a script $x = 2 * 2 then $x would be 4.
#18  

Rich, I'm not familiar with these battery packs. Sounds like you have two cells wired in series? Wish I could see it and how it's wired. Off the top of my head; you could attach the negative from the load and the ground from the ADC port to the Negative of battery 1, the positive of battery is attached to Neg of Battery 2, the pos of battery 2 would be attached to your load and the Signal from the ADC port to Battery 2. However it sounds like this will bring the voltage above what the ADC port will handle. Put the Voltage divider in there like we mentioned above and I think you be OK and be able to monitor the entire pack. Again, I'd have to review the wiring of the pack before I would hook it up to my EZB. There was a thread here a few months ago about just this subject. Perhaps a search would be advised.

United Kingdom
#19  

They are 2 cells wired in series. The balance port has 3 wires, black, yellow & red. Black to Yellow is Cell 1. Black to Red is across both cells. Red to Yellow is across cell 2.

Stupidly I had done black/red to ADC1 which was feeding in up to 8.4v, so glad that didn't fry anything though.

I know I need to read from Red/Yellow but I don't know how safe it is to connect ADC1's Ground to the middle of the series of batteries. Hopefully a search will help though, now I know the problem it helps :)

#20  

Thanks Rich and Dave for all these explanations and solutions. By the way @Rich, i like the smiley in the code very much. We should ask DJ to implement it, it will make scripting a little more friendly :D

United Kingdom
#21  

One good thing to come out of this for me is it's inspired me to revisit my code (which is good because I needed a break from building and wiring). I'm just adding in some error checking, new variables for maximum and minimum voltages and multiplier for voltage division with the intention of sharing it (as I don't need the voltage division multiplier).

I just added it to examples in the cloud for those who want it and want to play around with it. It's called Battery Monitor. Or just click here

#22  

here is a easy way i watch all my voltages (total 4) with out a script click here with batteries full charge use resistors to get voltage as close to 5v without going over then just use ADC Value

United Kingdom
#23  

That's a great way and it's how I would do it but doesn't get the EZB to run a command/task when battery is low, it just monitors it... which is fine if you just want to monitor it.

But, with LiPos, you can't let them drain too much otherwise they don't charge, so ideally you want it to either a) shut off all power when it gets low or b) go and charge itself up. For those, it needs a script running to monitor and act upon a battery low alert.

My script doesn't give a graphical representation on screen for monitoring which is a downside but adding an ADC control in ARC would add that, at the cost of processing power as it would be checking twice as often.