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

    Gravity: Analog AC Current Sensor (20A)

    Scheduled Pinned Locked Moved LoPy
    7 Posts 2 Posters 49 Views 2 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.
    • S Offline
      StefanoF
      last edited by StefanoF

      Hello,
      I have a Lopy connected to pin P16 at Gravity: Analog AC Current Sensor (20A) on 3.3 volts, does anyone have a micropython code to share?

      Bye

      Stefano

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

        @StefanoF Just follow the examples from the documentation, basically:

        from machine import ADC
        adc = ADC()
        adc_c = adc.channel(pin='P16', attn=ADC.ATTN_11DB)
        print(adc_c.voltage())
        

        This value will most probably not be the actual current. You have to use a known current to adjust the correction factor.

        S 1 Reply Last reply Reply Quote -1
        • S Offline
          StefanoF @robert-hh
          last edited by

          @robert-hh

          I'm sorry, but I'm having a hard time converting this value to ampere ..
          Thanks a lot

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

            @StefanoF So if you know that the current is for instance 10A, and you get a voltage of 1.5 from the interface, then youi know that the ration is 10/1.5 = 6,6667A/v. You then multiply the Voltage you get from the ADC by 6.6667 to get the Ampere. You should make several ADC reading and use the average of that, and you should use several known current points to validate the transfer ratio.

            S 2 Replies Last reply Reply Quote -1
            • S Offline
              StefanoF @robert-hh
              last edited by robert-hh

              @robert-hh

              adc = ADC()
              adc_c = adc.channel(pin='P16', attn=ADC.ATTN_11DB)
              
              def readACCurrentValue():
                  peakVoltage = float(0)
                  voltageVirtualValue = float(0)
                  ACCurrtntValue = float(0)
                  for i in range(5):
                      peakVoltage += adc_c.voltage() 
                  peakVoltage = peakVoltage / 5 
                  voltageVirtualValue = peakVoltage * 0.707
                  voltageVirtualValue = (voltageVirtualValue / 1024 * 3.3 ) / 2
                  ACCurrtntValue = voltageVirtualValue * 20
                  return ACCurrtntValue
                  pass
              
              
              print(readACCurrentValue())
              

              3.235353 at empty

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

                @StefanoF I tiny comment: adc_c.voltage returns mV. So for V you have to divide by 1000. I do not know why you multiply by 3.3. That is only necessary if you use adc_c.value in a 11db attenuation range. And then the divisor must be 4096.

                Calculating the values back it seems the the averaged peakVoltage value you get is about 141. maybe you could print that during testing. It is absolutely common that sensors have a zero-offset. That has be be determined and subtracted from the test result. Practically you have to make a kind of linear transformation of the result, in the form:

                current = ratio * voltage - offset

                Where offset is ratio * voltage at a current of zero, and ration is the transfer ratio, which has to be determined by testing with known currents.

                1 Reply Last reply Reply Quote 0
                • S Offline
                  StefanoF @robert-hh
                  last edited by robert-hh

                  @StefanoF

                  adc = machine.ADC() 
                  apin = adc.channel(pin='P16', attn=ADC.ATTN_11DB)
                  
                  def readACCurrentValue():
                      peakVoltage = float(0)
                      for i in range(5):
                          peakVoltage += apin.voltage() 
                      peakVoltage = (peakVoltage / 5) - 142  # 142 offset
                      ACCurrtntValue = peakVoltage / 1000
                      return float(ACCurrtntValue)
                      pass
                  
                  print(readACCurrentValue())
                  

                  0.0 at empty

                  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 / 1
                  • First post
                    Last post

                  Pycom on Twitter

                  Pycom