For a project I need to be able to read the battery level of the device regardless of the sketch currently running. For this I plan on adding a custom routine in the Arduino bootloader that reads the battery level and shows it can be accessed even when the current sketch is overwritten with another sketch.

Is creating a custom bootloader a good solution for this? If so how should I go about create that bootloader?

Edit: The battery level will be shown using 3 leds indicator every time a specific button is pressed; I don't want to store the battery level. I need this to happen in every sketch, that's why I think I should do this on the bootloader, having that code on the sketch isn't an option. This is needed because I am developing a custom board and this function will be default on each board.

  • How should be battery level be shown/accessed? And when? When you use a bootloader, it will only work while the bootloader is running – chrisl Nov 5 '19 at 13:47
  • You will have to store the battery level somewhere safe. In the EEPROM for example. Then you can access it afterwards. So to me this sounds feasible. Even I don't understand why I has to live inside the bootloader. Is it supposed to prevent the sketch from being executed on low battery? – Kwasmich Nov 5 '19 at 14:00
  • 1
    With out knowing the overall goal this is difficult to solve. Sure you could spend time to understand and modify the boot loader. But the resulting code will likely leave less memory for the application. On top of that, @chrisl is correct, the application & boot loader do not run at the same time. This is not a real OS. So no new battery measurements unless you waste space duplicating the code or figure out a way to jump to boot loader code from the Application. Instead, consider using a 2nd Arduino to measure the battery level. A much simpler and cheaper approach if this is a 1 off project. – st2000 Nov 5 '19 at 14:55
  • 1
    if your Arduino board uses a second microcontroller for the USB interface, then you could use that for your battery monitor – jsotola Nov 5 '19 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.