Simple ebike 'fuel gauge'

Jeremy Harris

100 MW
Joined
Oct 23, 2007
Messages
4,208
Location
Salisbury, UK
I know that having a Watts Up or Cycle Analyst bolted to the handlebars gives you loads of useful information, but most of the time I find I only want to know two things:

1. How much battery capacity do I have left?

2. How much current am I pulling?

For practical purposes you only really need to know either of these to about the same accuracy as the gauges in a car. Analogue meters are easier to read 'at a glance' than digital displays, but look clunky. You can get tiny LCD analogue meters though, that not only look neat but are also weatherproof.

As part of an update to my stealth folder (see the other thread in the ebike photos section) I decided to design, build and fit a simple fuel gauge. This is what it looks like on the handlebars:

Gauge on handlebars.JPG


The small switch changes the display from capacity remaining to instantaneous (well, the sum of 100 samples taken over a second) current. There's nothing in the little handlebar box except the meter and display, the stuff that drives it sits inside the controller case. The controller already has a shunt for measuring current, which is easy to tap into, so this is what I used. The controller circuit is a tiny module about 1.5" x 1", covered in heatshrink, that just stuffs inside the controller case. A thin four core cable runs up to the display on the handlebars. The module is powered by the 5V supply on the controller (it only draws a couple of mA or so). I've left the serial programming port in place (the three pin connector in the next photo) so that I can update the code if needed. Here's a picture of the finished microcontroller module:

Microcontroller module.JPG

The way it works is to store the state of charge of the battery (in amp seconds) in non-volatile memory (so it remembers it when the power is off) and just subtract the Ah used as you draw current from the battery, by measuring the voltage across the controller shunt. There's a reset button on the controller that if pushed at power on resets the gauge to 'full'. The actual capacity is programmed in the code - mine is set for 10Ah. If the battery gets within 10% of being flat, then the display switches to overload mode and flashes all the pointer bars as a 'low fuel warning'. You can still switch to current mode to see how much you're drawing and eke out the last bit of capacity as a 'get you home' measure. It'd be best to always recharge before this point though and this gauge makes that pretty easy to work out.

If anyone wants it I can post the schematic and code. It's not hard to build and the code can be programmed into the chip using free software and the same 5V serial interface used to talk to the XieChang controllers.

Jeremy
 
Hi Jeremy,
This looks a very neat little unit that will fit my needs spot on. If you could wouldn't mind, a diagram and code would be just the ticket.
Cheers.
 
gwhy! said:
Hi Jeremy,
This looks a very neat little unit that will fit my needs spot on. If you could wouldn't mind, a diagram and code would be just the ticket.
Cheers.

OK gwhy, here you go. Here's the schematic:

Simple ebike fuel gauge.JPG

And here's the code:

Code:
;Simple 'fuel gauge'


main:

	SYMBOL Capacity = w0					;Capacity holds battery capacity remaining in amp seconds (18.2 Ah max, due to 16 bit 

limit)
	SYMBOL Current = w1
	SYMBOL Voltage = w2
	SYMBOL DisplayCapacity = w3
	SYMBOL DisplayCurrent = w4
	SYMBOL Averagecurrent = w5
	SYMBOL loopcounter = b12

startup:

	;read value of Capacity stored in non-volatile memory
	READ 0, WORD Capacity
	
	
	;Read supply voltage (assumes divider of 120K and 10K, giving 1023 = 65V)
	READADC10 1, Voltage
	
		
	;Reset capacity to 10Ah (36,000 amp seconds) if reset button (which disconnects the voltage sense) is pressed at power on
	IF Voltage < 50 THEN LET Capacity = 36000
	ENDIF
	
	;check for capacity over-range from misreading
	IF Capacity >36000 THEN LET Capacity = 36000
	ENDIF
		
	;Wait for power to settle on power up for more accurate ADC readings and allow time for reset button to be released, 5 second delay
	PAUSE 5000
	
		
mainloop:

	;setloop time calibration output pulse high (use serial out pin for calibration pulse)
	HIGH 0

	;reset averagecurrent drawn over last second to zero
	Averagecurrent = 0
	
measurementloop:

	;take 100 measurements and sum the results to give amp seconds (mainloop time is 1 second)
	FOR loopcounter = 1 TO 100
	
	;Check for battery voltage below about 25V (indicating power going off) and not in reset mode, every 1/100 second
	READADC10 1, Voltage
	IF Voltage < 400 AND Voltage > 50 THEN GOTO poweroff
		
	;Take current reading using voltage across controller shunt. A/D current reading is 321 for 50mV across 5mOhm shunt = 10 amps (~ 31mA 

resolution)
	READADC10 4, Current					
	;At this point Current can be any value between 0 and 1023 (~31.87A)
	
	;divide current by 2 to remain within 16 bit limit during summation
	Current = Current / 2
	;At this point Current can be any value between 0 and 511 (for 0 to 31.87A)
	
	;add current to averagecurrent over one second
	Averagecurrent = Averagecurrent + Current
	
	;Add loop delay time to make main loop execution 1 second	
	PAUSE 4
	
	NEXT loopcounter
		
	;At this point Averagecurrent can be any value between 0 and 51,100 (~31.87A)	
	
	
backtomainloop:
	
	;set loop time calibration output pulse low
	LOW 0
	
	;Add loop delay time to make main loop execution 1 second	
	PAUSE 70
	
	;Divide Averagecurrent by 146 to convert to ~ 330 = 30A ~ full scale for Displaycurrent to drive current display mode
	DisplayCurrent = Averagecurrent / 146
		
	;Convert Averagecurrent to amp seconds, based on 1 second main loop time
	Averagecurrent = Averagecurrent / 1603
	;At this point Averagecurrent can be any value between 0 and 31
	 	
	;Averagecurrent is in amp seconds and loop time is 1 sec, so each time around loop current x amp seconds are subtracted from capacity
	Capacity = Capacity - Averagecurrent				
	
	;DisplayCapacity is used by PWM command in remaining capacity mode, where 330 equals full scale of 10Ah (36000 / 330 = 109)
	DisplayCapacity = Capacity / 109			
	
	;Check for less than 10% battery capacity remaining and exit to low battery warning routine
	IF Capacity < 3600 THEN GOTO batterylow
	IF Capacity >=3600 THEN GOTO display
		
		
display:

	;Mode switch takes pin 3 high or low, low = capacity remaining display, high = average current over last second display
	;99 sets 10kHz on timer, max duty cycle range on PWM is 0 to 330 for 0 to 100% full scale on meter (400 gives overload indication)
	;Check mode switch and output appropriate PWM stream to simple D/A that drives meter
	IF PIN3 = 0 THEN PWMOUT 2, 99, DisplayCapacity ELSE PWMOUT 2, 99, DisplayCurrent
	ENDIF
	
	GOTO mainloop
	
	
batterylow:

	;Set meter to overload display as a low battery warning (all bars flashing) when in capacity mode
	IF PIN3 = 0 THEN PWMOUT 2, 99, 400 ELSE PWMOUT 2, 99, Displaycurrent
	ENDIF
	
	GOTO mainloop
	
poweroff:

	;save value of Capacity in non-volatile storage for use on next power up
	WRITE 0, WORD Capacity
	END

Hopefully this should be fairly easy to work through. If you want to change the battery capacity then you need to change the value that the variable Capacity is reset to in this line:

Code:
;Reset capacity to 10Ah (36,000 amp seconds) if reset button (which disconnects the voltage sense) is pressed at power on
	IF Voltage < 50 THEN LET Capacity = 36000
	ENDIF

from 36000 (which is 10Ah in amp seconds) to the capacity of your battery. Unfortunately it's limited as it stands to a maximum of 18.2Ah (65,535 amp seconds). This could be changed by re-jigging the code to use something other than amp seconds as the measurement figure though, but would need some doctoring of multiplication and division factors elsewhere in the code.

The maximum battery voltage it will take at the moment (with the 120k/10k divider) is about 65V. You can change the resistor values to increase this, but may then need to change the value read from the voltage A/D that detect the power going off. At the moment it triggers at about 25V, which is fine. The next graph shows the decay of the +Vbatt line and the +5V line on the controller, with the module connected. It gives a good indication of how long the microcontroller has to detect a battery off event, finish what it's doing, save the current value of battery capacity in non-volatile memory and then shut down. It has ages to do this, in practice it takes only about 1mS to do the full shut down routine!



The plastic box that the meter is mounted on came from Maplin, a hobby store a bit like Tandy in the US. It's an ABS box that measures 50mm wide x 35mm deep x 17mm deep (about 2" x 1.4" x 0.7") and is Maplin part number N78BQ, manufacturers type number 1551GBK. The box is held on to the handlebars using two self adhesive cable tie pads (the ones that take tywraps), with a couple of tywraps then going around the bars. The meter is a Lascar type EMA1710 and mounts with a single 5mm hole. It only has three wires, +5V, 0V and a 0 to 1V input. I fitted a couple of resistors to the back of it to step the voltage down from the crude PWM D/A output on the controller, so that it reads maximum correctly. The meter data sheet is here: http://www.lascarelectronics.com/pdf-usb-datalogging/data-logger0389744001208250942.pdf

Hope this all helps!

Jeremy
 
Top notch!

That is exactly what I should have put on the bikes I made for my parents.

Maybe an auto-reset if it sense pack voltage at HVC for 20 minutes continously or something as an alternative to the switch for people who have extremely forgetful mothers? ( 20 min delay just so it's not activated by mistake during extended regen going down a hill or something, and not activated when the charger just hits the CV stage).
 
liveforphysics said:
Top notch!

That is exactly what I should have put on the bikes I made for my parents.

Maybe an auto-reset if it sense pack voltage at HVC for 20 minutes continously or something as an alternative to the switch for people who have extremely forgetful mothers? ( 20 min delay just so it's not activated by mistake during extended regen going down a hill or something, and not activated when the charger just hits the CV stage).

That's exactly how I planned to do the reset originally, but then couldn't be bothered to run the pack down, charge it and log the voltage/time curve to work out when to accurately detect a 'full charge' condition. It's one reason the reset button simply disconnects the voltage sense line - I bodged the code to get it working by detecting 0V instead......................

If you can be pretty sure of accurately detecting the voltage rise for full charge, then just remove the push button and change the code so that the IF test in this line:
Code:
;Reset capacity to 10Ah (36,000 amp seconds) if reset button (which disconnects the voltage sense) is pressed at power on
	IF Voltage < 50 THEN LET Capacity = 36000
	ENDIF
tests for the high voltage condition rather than a very low voltage, maybe adding some other conditional factor, like a time loop.

BTW, when I mentioned changing the code for different battery capacities earlier I forgot to add that value in this backstop test line also needs to be changed to the new capacity:

Code:
;check for capacity over-range from misreading
	IF Capacity >36000 THEN LET Capacity = 36000
	ENDIF

This line is there to stop an over-run causing an out of range rollover error. If capacity accidentally tries to go over 65535 then it rolls over to 0..................

Jeremy
 
Thanks Jeremy,
Another very usefull device. I have been looking for options for a realtime amp meter to mount to the bars. This is good (as was your mod for re-mote mounting of the Turnigy watt meter). Things like this are great for guys ike me on the fringe of ellectronics. It something I could actually build & use.
I will attempt one of these soon & one of the servo drive switches you helped the AussieJester out with for gear changing. the more I consider it the better i like it. Very cool
Thanks again. T
 
Thanks Jeremy,
The cabinet is available here under the same part number from mouser (listed as a Hammond product).

For ten dollars more you can buy a seventeen segment display. Would that be suitable?
http://www.jameco.com/webapp/wcs/stores/servlet/ProductDisplay?langId=-1&productId=2116533&catalogId=10001&freeText=SP+5-1710-BL&app.products.maxperpage=15&storeId=10001&search_type=jamecoall&ddkey=http:StoreCatalogDrillDownView
dw1270.jpg
 
Pretty much any meter that will accept a voltage in the range 0 to around 3 or 4V, either directly, or with a potential divider made up from a couple of resistors, would do, so yes, the higher res version of the one I used would be OK. You could even use a clunky looking electromechanical meter if you wanted to, just by fitting a suitable series resistor. I haven't investigated them, but it might even be possible to drive an old car fuel gauge - it depends how much current they need to go full scale.

These people: http://www.surplusgizmos.com/Lascar-9-Segment-Color-LCD-Compact-Meter-Display-Model-EMA-1710_p_1486.html are selling the basic EMA1710 meter for $12 at the moment, around half price, so quite a bargain and a lot cheaper than the 17 segment version.

Jeremy
 
If I ever run into the time to do this, and find the right parts, I've got a bunch of old electromechanical panel meters I can easily print new labels for that would be nifty for this. :)

If I built some transistorized voltage translators, I could even drive some of the aircraft gauges I've got, which I think run on 28VDC IIRC. Have to figure out what their pinout is, though.
 
Thanks for that, Jeremy.
Very usefull and greatly needed.


Christian

p.s.: at the moment it looks like this :mrgreen:
cimg0336u.jpg
 
Thanks, guys. I think there is a gap in the market for something simple like this. I'm guessing that lot's of everyday ebike users, those who just want the bike to work and don't care about the details (and so probably don't frequent this forum), might find a simple capacity gauge pretty useful, in the same way as a fuel gauge in a car is useful.

It's probably not a commercially viable idea, though, as it'd cost as much to manufacture as something far more capable. If it were built in to the controller code then cost would only come down to some form of display, which might make it marketable. The cheapest display would probably be a LED bargraph, they are certainly a fair bit cheaper than the LCD meter I used (I only used that because I had a few left over from another project).

Jeremy
 
Hi Jeremy,
Many thanks for posting this up Im just at the stage of getting the parts together myself and have been looking at using 2 of these as a display:


The extra one being using for a realtime battery voltage gauge as to keep a eye on how well the battery is holding up .

Edit:
Ok I have just had a re-think and to keep things even more simple ( for me anyway ) is to use one of the other outputs to drive a led to monitor battery voltage ( maybe something like, led on >65% , flashing 65-20%, off <20% ).

:oops:
There isnt a free output... Maybe I will just hang on till the stuff that I have ordered gets here and just have a play to see what what I can sort out.
 
Sorry about the lack of free outputs - it's so tight that I even had to use the serial output to kick out the timing calibration pulse. This pulse is used to fine tune the time that the code takes to execute the main loop, if you change the code the duration of the 'pauses' in the two loops may need changing (there is a 'coarse' setting using the pause in the measurement loop and a 'fine' setting in the main loop). You set the time up by making sure that the time between the calibration pulses is as close to 1 second as possible (in practice, +/- 1mS is as close as you can get).

Be very wary of using the Picaxe debug command - it massively slows down the loop and will result in very large Ah errors due to the time error it adds.

You could switch to the 14M, which would give you several more outputs. The main code would work fine on the bigger chip.

Nice to see folk having a go at building themselves one of these - watch this space as there will be another ebike related self-build project posted soon!

Jeremy
 
I've been hesitant to post here, because I have nothing helpful to add. However, I can confirm that it is something that I want! and I suspect many others will want one too.
 
spinningmagnets said:
... I can confirm that it is something that I want! and I suspect many others will want one too.

+1 on adding this to my wish list.

While the Cycle analyst is hugely useful, especially for tinkers most of the time I just want to know how much gas is in the tank.
 
Maybe this is a silly question, but how does the circuit take regen currents into account? Won't the shunt have a negative voltage across it during regen?
 
keyne said:
Maybe this is a silly question, but how does the circuit take regen currents into account? Won't the shunt have a negative voltage across it during regen?

Not a silly question at all, you're absolutely right. It ignores regen, primarily because the very best regen system might, just possibly, manage to put perhaps 5% or so back into the battery pack. Most of the time regen will probably give less then this. The resolution of the gauge is just over 10% per segment, so regen most probably wouldn't change the indication. The geared hub motor on the bike this is fitted to can't regen, so I didn't try to cover it.

If you wanted to make it regen capable, perhaps with a higher resolution display (which is, perhaps moving away from the simple intention of this gauge), then it would be fairly straightforward to make it measure bidirectional current. Just replace the shunt amplifier with a Hall current transducer and re-jig the code to detect + and - current and add or subtract as required. The LEM LTS range is reasonably priced and works OK: http://www.lem.com/hq/en/component/option,com_catalog/task,displayserie/serie,LTS%20-%20LTSR%20-%20LTSP/output_type,instantaneous/ but would make the unit too big to fit inside the controller case.

Jeremy
 
I think in this case, regen probably should be ignored, as even if you have it you may not gain enough from it to extend your range significantly on a typical ebike this kind of gauge would be used on, and if you *know* you are getting another 5% out of the system because of regen you can just ignore the low battery warning for that much longer if you choose.

Personally, I think if you want to monitor regen current, you'd probably be more advanced a user than what this simple project is meant to encompass. ;) At that point, a pair of WUs or TWMs wired in mirror-series, or a single CA would be more appropriate.
 
AussieJester said:
Very nice work Jeremy, are there any plans to do a small run of these for those of us
that would more than struggle to get one together and working?

KiM

I'm afraid I don't really have the time to build a few, plus there are potential problems with each one needing to be customised to some extent to match controller shunt values, battery capacity etc, that means they're really only a DIY job.

I'd love it if someone just took the basic design and came up with a product that was easier for people to just fit and use. The design changes needed to make it controller independent are simple - just fit an independent shunt, or use a DC current sensor. Making it easily settable for different battery capacities is a bit harder and would mean changing the chip to one with more input pins, so that a selector switch could be added. Pretty easy to do if someone wants to have go at it. If I get some free time I may have a go at changing it to a more universal gauge that doesn't rely on a controller shunt or need reprogramming for different battery capacities.

Jeremy
 
spinningmagnets said:
I've been hesitant to post here, because I have nothing helpful to add. However, I can confirm that it is something that I want! and I suspect many others will want one too.
dougnutz said:
spinningmagnets said:
... I can confirm that it is something that I want! and I suspect many others will want one too.

+1 on adding this to my wish list.

While the Cycle analyst is hugely useful, especially for tinkers most of the time I just want to know how much gas is in the tank.

Cycle Analyst tells me more then I want to know. This fuel gauge has always been my dream. The GPS tells me miles per hour. Would someone please start selling a simple battery fuel gauge.

Nice job! - Jeremy
 
marty said:
Cycle Analyst tells me more then I want to know. This fuel gauge has always been my dream. The GPS tells me miles per hour. Would someone please start selling a simple battery fuel gauge.

Nice job! - Jeremy

Thanks marty. I'll happily do what I can to support anyone who want to make these to sell for people. I'd love to make some up myself for folk but don't have the time right now to go into production. A production version might be better as a separate module that doesn't depend on being fitted to a particular controller type, but that isn't hard to do.

Jeremy
 
I have had a quick look to see what would be a suitable (cheap'ish) bargraph driver for leds and the LM3915N-1 would be suitable and very easy for the DIY'er to mount on a bit of vero board, with the addition of 10 leds and a couple of resistors.
http://www.farnell.com/datasheets/9060.pdf


Jeremy, Its not your fault that there is no free outputs :D , I may get a couple of the larger picaxe ( more output/input ) chips to play with.
 
Back
Top