from machine import ADC, Pin
import utime
# Initialize ADC
adc = ADC(2) # Assuming you are using GPIO 26 for ADC input
burden_resistor = 2.2 # Total burden resistor value in ohms (10 ohm + 3.9 ohm)
adc_resolution = 65535 # 16-bit resolution
max_expected_voltage = 3 # Maximum expected voltage across the burden resistor
ct_ratio = 400 # CT ratio
def read_voltage_and_current():
# Read ADC value
adc_value = adc.read_u16()
# Convert ADC value to voltage
voltage = (adc_value / adc_resolution) * max_expected_voltage
# Calculate measured current (I = V/R)
measured_current = voltage / burden_resistor
# Scale up to real-world current
real_world_current = measured_current * ct_ratio
return voltage, measured_current, real_world_current
# Main loop
while True:
voltage, measured_current, real_world_current = read_voltage_and_current()
print(f"Measured Voltage: {voltage:.3f} V, Measured Current: {measured_current:.3f} A, Real-World Current: {real_world_current:.2f} A")
utime.sleep(1) # Read every 1 second