Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. General Discussion
  3. Measuring battery voltage, which way is best?

Measuring battery voltage, which way is best?

Scheduled Pinned Locked Moved General Discussion
24 Posts 6 Posters 313 Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mfalkviddM mfalkvidd

    My pull request has now been merged to the MySensors development branch. I have updated the battery page to show the example sketch for internal measurement. Note that the internal measurement method does not work on all supported mcus (not esp8266 for example) but I don't think it matter much since the battery page recommends a Pro Mini which works.

    Special thanks to @hek for tracking down a tricky display problem with the sketch inclusion, and to @Yveaux, @user2684 and @tekka for feedback on the pull request.

    skywatchS Offline
    skywatchS Offline
    skywatch
    wrote on last edited by
    #21

    @mfalkvidd Good News! :)

    But I am curious where the "+ 0.5" vomes from in this line.....

    int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5;
    

    ???

    zboblamontZ mfalkviddM 2 Replies Last reply
    0
    • skywatchS skywatch

      @mfalkvidd Good News! :)

      But I am curious where the "+ 0.5" vomes from in this line.....

      int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5;
      

      ???

      zboblamontZ Offline
      zboblamontZ Offline
      zboblamont
      wrote on last edited by
      #22

      @skywatch Rounding up to the nearest int ?

      1 Reply Last reply
      0
      • skywatchS skywatch

        @mfalkvidd Good News! :)

        But I am curious where the "+ 0.5" vomes from in this line.....

        int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5;
        

        ???

        mfalkviddM Offline
        mfalkviddM Offline
        mfalkvidd
        Mod
        wrote on last edited by mfalkvidd
        #23

        @skywatch as @zboblamont said, it makes sure we have ”proper” rounding. In C, floating point numbers are always rounded down Ehrn converted to an integer. So 42.8 becomes 42. By adding 0.5, we get the rounding expected by most humans in most cases.

        To read about different rounding methods, see https://en.wikipedia.org/wiki/Rounding#Rounding_to_the_nearest_integer

        My personal favorite is called ”banker’s rounding” but I did not use it in this sketch.

        1 Reply Last reply
        0
        • skywatchS Offline
          skywatchS Offline
          skywatch
          wrote on last edited by skywatch
          #24

          @zboblamont & @mfalkvidd - Thank you for that. It never occured to me that rounding might need some help. But now it makes logical sense.

          @mfalkvidd I guess if you used 'bankers rounding' you would always end up with 0.

          1 Reply Last reply
          1
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          26

          Online

          11.7k

          Users

          11.2k

          Topics

          113.1k

          Posts


          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • MySensors
          • OpenHardware.io
          • Categories
          • Recent
          • Tags
          • Popular