Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Always reading same battery voltage

    Scheduled Pinned Locked Moved LoPy
    lopybatteryadc
    24 Posts 4 Posters 10.8k Views 6 Watching
    Loading More Posts
    • 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.
    • M Offline
      mmarkvoort @robert-hh
      last edited by

      @robert-hh

      Thanks again for your repley.
      I have a LoPy and a Expansion board.

      When I measure on pin 16:
      With only USB I get: 1.4 V
      With USB and Battery I get: 1.3 V
      With only Battery I get: 1.2 V

      When I measure battery directly I get: 3.79 V

      When I use attn 3 and the code from the post I get:
      Mean of ADC readings (0-4096) = 1581.0400009155273
      Mean of ADC readings (0-1400 mV) = 540.3944969177246

      When I use attn3 and the code from the post with (meanADC * 3548/4096) instead of (meanADC*1400/4096) I get:
      Mean of ADC readings = 1401.6300201416015
      Mean of ADC readings = 1214.1072750091552

      Which one is good? Is it even good? I am a bit lost what to do.

      robert-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @mmarkvoort
        last edited by

        @mmarkvoort There are two factors top be considered.
        a) the resistor divider. You said that with bettery only you have 1.2 V at P16. Could you measure the battery directly in that mode, connected to lopy and running. Then this ratio, Vp16/Vbat = R is known, and the battery voltage can for attn = 3 be calculated as:

        Vmeas = (ADCmean /4096) * 3.548 /R

        Example:
        ADCmean = 1401
        R = 1.2/3.79 = 0.316
        Vmeas = (1581/4096) * 3.548 / 0.316 = 3.804

        M 1 Reply Last reply Reply Quote 0
        • M Offline
          mmarkvoort @robert-hh
          last edited by mmarkvoort

          @robert-hh

          Wow wait a minute...

          I used 3548 instead of 3.548 in my code :')

          Now when I read the val of pin 16 I get 1439.
          So this means it is 1439*3.548/4096 = 1.246477
          Measure pin 16 directly with multimeter gives me: 1.25

          This is good right??

          But how can I get the voltage of the battery? I can't because I dont know R right?

          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @mmarkvoort
            last edited by

            @mmarkvoort R is the ratio:
            Voltage_at_P16 / Voltage_at_battery
            According to the schematics, it is 0.3275, which gives ~3.8 V = 1.246/ 0..3275 at the battery, but better check.

            M 1 Reply Last reply Reply Quote 0
            • M Offline
              mmarkvoort @robert-hh
              last edited by

              @robert-hh

              But you can't get the Voltage_at_battery through code, right?

              Thanks a lot by the way, you helped me a lot! :)

              robert-hhR 1 Reply Last reply Reply Quote 0
              • robert-hhR Offline
                robert-hh @mmarkvoort
                last edited by

                @mmarkvoort Yes, you can. Since R is a fixed value, you can get determine the Voltage at P16 by code and calculate the battery voltage as:
                Voltage_at_P16/R

                M 1 Reply Last reply Reply Quote 0
                • M Offline
                  mmarkvoort @robert-hh
                  last edited by mmarkvoort

                  @robert-hh

                  But doesn't the value of R changes everytime?

                  You said R = Voltage_at_P16 / Voltage_at_battery, but how can you get Voltage_at_battery in the code.

                  Or in other words, how did you get the 0.3275 as a value?

                  EDIT: 0.3275 seems to be a good value indeed.

                  robert-hhR 1 Reply Last reply Reply Quote 0
                  • robert-hhR Offline
                    robert-hh @mmarkvoort
                    last edited by

                    @mmarkvoort No, R is defined by the circuitry of the expansion board, and is fixed. The schematics show a voltage divider consisting of two resistors, one 56k, the other 115k, and so R can be calculated as:

                    R = 56 / (56 + 115)

                    If you want to find R by measuring, you have to take a Voltmeter and measure the real values at the battery and at P16, while the device is running from battery.

                    M 2 Replies Last reply Reply Quote 2
                    • M Offline
                      mmarkvoort @robert-hh
                      last edited by

                      @robert-hh

                      Robert thank you very much for your help.
                      I finally get it!

                      1 Reply Last reply Reply Quote 0
                      • M Offline
                        mmarkvoort @robert-hh
                        last edited by

                        @robert-hh

                        Just some questions.

                        Is it a good idea to put a capacitor between battery and LoPy to reduce noise?
                        Now when I am reading the battery value it really fluctuates.

                        Also sometimes when connecting the battery it is always reading low value. When move the connector a bit it is changing. What can this be? Is it the startup that needs a bit more?

                        And lastly, does it make a difference if I have a sensor connected?

                        robert-hhR 1 Reply Last reply Reply Quote 0
                        • robert-hhR Offline
                          robert-hh @mmarkvoort
                          last edited by

                          @mmarkvoort The ADC of the esp32 is known to be very bad. If if you apply a perfectly good signal (stable, low impedance), the reading will fluctuate a lot. That's why in the sample code the average is used.
                          A capacitor will only help if placed directly at P16. and it must be a low impedance capacitor (ceramic, short leads).

                          M 1 Reply Last reply Reply Quote 0
                          • M Offline
                            mmarkvoort @robert-hh
                            last edited by

                            @robert-hh

                            Aha, so it isn't me :)
                            Thanks, I thaught I did something wrong.

                            Does it matter if you have sensors connected to the LoPy?

                            robert-hhR 1 Reply Last reply Reply Quote 0
                            • robert-hhR Offline
                              robert-hh @mmarkvoort
                              last edited by

                              @mmarkvoort Other sensors do not hurt. When USB is connected, the battery voltage is higher, since it will be charged at that time.
                              And yes, if the voltage changes when you touch the connector, then you have a bad connector. By maybe that was just coincidence with the other fluctuations.

                              M 1 Reply Last reply Reply Quote 0
                              • M Offline
                                mmarkvoort @robert-hh
                                last edited by

                                @robert-hh

                                Oke, will make a test program.

                                When USB is connected it indeed has a higher voltage :').
                                That was another mistake/issue I had :).

                                Thanks a lot, again!

                                1 Reply Last reply Reply Quote 0

                                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                With your input, this post could be even better 💗

                                Register Login
                                • 1
                                • 2
                                • 1 / 2
                                • First post
                                  Last post

                                Pycom on Twitter

                                Pycom