48v eBike voltage display

Hi all!

Lets start with the common thing.... I'm new here :slight_smile: I've used the search function but could not find what i wanted.

I'm working on a little project for my eBike to installe a display which will show me the battery voltage and percentage. Measuring the voltage is very accurate with only a difference of around 0.3V. Still running it on the test breadboard. Percentage is running good too but the only problem is that, lets say 54 volts ( equals 100%) and 0 volts would be 0 %. I need to have that 54V is 100% and 39V is 0%.

I'm not sure if I have worded it correctly. The coding is not perfect.

I would like to get some advice how i can make the percentage working for only the 39-54 volts. Hopefully I did post it in the right place.

Thank you in advance.

Voltage_Meter_for_56_Volt_updated.ino (1.05 KB)

Need to learn the map() and constrain() functions.

value = analogRead(analogInput);
percentage = map(value, 674, 934, 0, 100); // numbers calculated for 39volt and 54volt

Note that battery voltage is a poor indicator of remaining charge.
A better (and more complicated) way is a coulomb counter.
Leo..

Ebikes mainly use Lithium Ion batteries and the voltage alone gives an extremely poor
indication of capacity left.
Coulomb counting is really the only way , and its not that great either.

Wawa:
Need to learn the map() and constrain() functions.

value = analogRead(analogInput);
percentage = map(value, 674, 934, 0, 100); // numbers calculated for 39volt and 54volt

Note that battery voltage is a poor indicator of remaining charge.
A better (and more complicated) way is a coulomb counter.
Leo..

Thank you for giving me the hint of the map function. I finally have it working. I know it will be not 100% accurate but at least I have something to go with. I will give it a couple charge and discharges to see how well it will work.

I used
Value = analogRead(A0); //collect data from pinA0
Mapped = map(Value, 398.97, 1023, 0, 100);

398,97 will be the low 39V and 1023 the 54.6. Everything has to go on a PCB board so i can get rid of voltage loss and adjust the Vin on pinA0 and Vout

Map() only works with integers, so 398,97 won't work. Use 399
AnalogRead only returns integers anyway.

Leo..