Gravity: Analog AC Current Sensor (20A)

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

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

@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 zerooffset. 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.

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

@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.

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

@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.