The problem
Owners want to know what a trip costs and how efficient their riding is, and fleet operators track energy per kilometre for every vehicle. All of this rests on a careful energy measurement that keeps integrating accurately over long trips without drifting or overflowing.
Objectives
- Measure cell voltage and estimate discharge power under the switchable loads
- Integrate energy in Wh over a trip without overflow or loss of precision
- Compute Wh/km and trip cost from emulated distance and a user tariff
- Show trip A and trip B on the LCD with reset and tariff entry from the keypad
What you need
From the EV Embedded Development Kit
- Second STM32F103C8T6 as the Smart BMS unit
- STM32F103C8T6 microcontroller unit (main controller)
- 1S battery with connector and T-sense input
- Load section: three switchable resistive loads with LEDs
- Analog potentiometer input
- 4×3 matrix keypad
- 16×2 LCD
Software
- STM32CubeIDE with HAL drivers
- Embedded C
Extra parts
None. Everything this project needs is on the kit.
This project uses the kit's lithium cell. Respect polarity, never short the terminals, and don't leave the cell charging unattended.
Key concepts
- Energy integration
- Energy is power summed over time. Adding P × Δt at a fixed sample rate in a 64-bit integer accumulator keeps precision without overflow.
- Power from voltage and resistance
- With no current sensor on the kit, power is V²/R for the switched-in load, using resistances measured with a multimeter. Resistor tolerance is the main error.
- Scaling to a vehicle
- The kit cell stores a few watt-hours, so a stated scaling factor converts kit energy into vehicle-sized figures for display. The raw kit values stay in the log.
- Trip counters
- Separate accumulators for energy and distance that the user can reset independently, like trip A and trip B on a car dashboard.
Step-by-step guide
- 1
Define the meter
List what the meter shows: trip energy in Wh, distance in km, Wh/km and cost in rupees, for two trips. Fix the scaling factor between kit energy and displayed vehicle energy and the speed range for the potentiometer. Define keypad actions: switch trip, reset trip, enter tariff. Read the cell-safety notes: check polarity, never short the cell and do not leave it charging unattended.
Checkpoint: A one-page specification with display layouts for each screen.
- 2
Measure the loads
With the cell disconnected, measure each load resistor with a multimeter and record the values and combinations. Note the tolerance and work out its effect on power: since P = V²/R, a 5 per cent error in R gives about 5 per cent error in power. Record this in the report as the main uncertainty. Repeat each measurement a few times and use the average, and note the room temperature at the time of measuring.
Checkpoint: A resistance table with all load combinations and the error estimate.
- 3
Board setup
Enable the DIP switches for the battery, loads, potentiometer, keypad and LCD blocks. Set both controllers to 72 MHz. On the BMS unit set the ADC with DMA for cell voltage and load GPIOs, and send voltage and load state to the main controller over the link between the controllers. On the main controller set the ADC for the potentiometer, the LCD in 4-bit mode, keypad GPIOs and a 10 Hz timer tick.
Checkpoint: The main controller receives a fresh cell voltage and load state every 100 ms.
- 4
Calibrate the voltage reading
Calibrate gain and offset of the cell voltage against a multimeter at two points, using the divider ratio from the schematic. Average samples to reduce noise. Express voltage in millivolts as an integer so all later maths can stay in integers on this FPU-less controller. Repeat the calibration after a few days to check it is stable, and store the gain and offset in flash so they need not be re-entered after every firmware update.
Checkpoint: Displayed voltage is within 10 mV of the multimeter across the cell's range.
- 5
Energy integrator
Every 100 ms compute power in microwatts from V² and the active resistance, then add power times 0.1 s to a 64-bit accumulator in microwatt-seconds. Convert to Wh only for display. Check that the accumulator cannot overflow in a realistic trip. Keep both trip accumulators updated from the same sample. Keep the integration in the timer interrupt or a tightly timed task so that no samples are skipped while the display or keypad is busy.
Checkpoint: With a fixed load for a measured time, the computed energy matches V²/R × t worked out by hand within 1 per cent.
- 6
Distance and Wh/km
Map the potentiometer to speed and integrate distance in metres. Compute Wh/km only once distance exceeds a minimum, such as 100 m, so the figure is not meaningless at the start. Link speed bands to the number of loads switched in so consumption rises with speed. State that speed is emulated. Store distance in integer metres in a 32-bit counter so it cannot overflow on any practical trip.
Checkpoint: Driving the emulated vehicle at constant speed gives a steady Wh/km that rises when speed and load increase.
- 7
Keypad, tariff and cost
Scan the keypad with debounce. Let the user enter a tariff in paise per kWh with '#' to confirm, and store it in flash so it survives power-off. Compute cost in paise from scaled energy and tariff with integer maths, and display it in rupees with two decimals. Reject a tariff of zero or an unrealistically large value, and show the stored tariff on its own screen so the user can always check what is being used.
Checkpoint: Entering a tariff and running a trip gives a cost that matches a hand calculation.
- 8
Display screens
Build three screens: energy and distance, Wh/km and cost, and tariff. Use one key to move between screens and another to switch trip A and B. Long-press reset clears the shown trip only. Update the display twice a second and write only changed characters to avoid flicker. Show units on every screen so values cannot be misread, and keep the most used screen, energy and distance, as the default after power-up.
Checkpoint: All screens and keys work, and resetting trip A leaves trip B untouched.
- 9
Testing and report
Run three test trips with different speed patterns and record energy, distance and cost. Compare energy with a hand calculation from logged voltage. Write the report with the specification, design, error analysis, test results and photographs. Include the error budget from the resistance measurements and explain how it affects the cost shown, and add photographs of each screen during a trip.
Checkpoint: A test table and a finished report are ready for review.
Testing & validation
- Energy against a hand calculation for a fixed load and time
- Wh/km and cost compared with manual calculations for three trips
- Independence of trip A and trip B on reset
- Tariff retained after power-off
Expected results
A dashboard energy meter that shows trip energy, efficiency and cost for two trips, with a documented error budget based on the load-resistance method.
Take it further
- Add an INA219 add-on to measure current directly and compare accuracy
- Log trips with RTC timestamps to SPI flash
- Show cost against a petrol vehicle using a user-entered fuel price and mileage
- Send trip data to a phone over an HC-05 Bluetooth add-on
Viva and review questions
Why use a 64-bit accumulator?
Energy keeps growing over a trip; summing small increments in a 32-bit variable could overflow or lose resolution, while 64 bits keep full precision for any realistic trip.
What limits the accuracy of your energy figure?
Mainly the tolerance of the load resistances, since power is computed as V²/R, plus the voltage calibration.
Why not show Wh/km from the first metre?
Over tiny distances the ratio is dominated by noise and start-up effects, so we wait for a minimum distance before displaying it.