User avatar
Etienne
Posts: 203
Joined: Wed Jul 24, 2013 6:23 am

Mains current measurement using current transformer

Wed Aug 05, 2020 9:57 pm

I had the need to measure the consumption of a device connected to main power. I did not need a precise 'metrological measurement', but rather an indication that I can use to know if the device is on or off and if its consumption is varying significantly.
I also wanted to keep the circuit safely isolated from main to avoid any risk (anyway, please ask people who knows about electrical risk before!...).

I ended up with the following solution that works fine at least for my need:
- Current transformer board with ZMCT103C current transformer and adjustable amplifier.
- Analog to digital converter board with PCF8591 circuit.

I hope the below may be helpful for some raspberry pi users...

Here you can find ::
- the circuit diagram
CT_test_schema.jpg
CT_test_schema.jpg (100.63 KiB) Viewed 114 times
- the physical connectivity (not yet integrated)
CT_test_photo.jpg
CT_test_photo.jpg (90.9 KiB) Viewed 114 times
- the result

Code: Select all

pi@raspberrypi:~ $ python3 current_estim.py
start main program
measurement duration: 0.0199 - estimated curent 0.10
measurement duration: 0.0195 - estimated curent 0.07
measurement duration: 0.0206 - estimated curent 0.13
measurement duration: 0.0206 - estimated curent 0.06
measurement duration: 0.0206 - estimated curent 0.04
measurement duration: 0.0207 - estimated curent 0.04
measurement duration: 0.0206 - estimated curent 0.08
measurement duration: 0.0206 - estimated curent 6.00  <-- switch lamp on
measurement duration: 0.0207 - estimated curent 5.93
measurement duration: 0.0210 - estimated curent 5.95
measurement duration: 0.0207 - estimated curent 5.98
measurement duration: 0.0207 - estimated curent 6.19
measurement duration: 0.0206 - estimated curent 6.01
measurement duration: 0.0207 - estimated curent 5.96
measurement duration: 0.0208 - estimated curent 6.30
measurement duration: 0.0207 - estimated curent 6.08
measurement duration: 0.0208 - estimated curent 6.03
measurement duration: 0.0208 - estimated curent 5.95
measurement duration: 0.0208 - estimated curent 5.91
measurement duration: 0.0206 - estimated curent 0.10  <-- switch lamp off
measurement duration: 0.0207 - estimated curent 0.07
measurement duration: 0.0209 - estimated curent 0.04
measurement duration: 0.0208 - estimated curent 0.07
measurement duration: 0.0208 - estimated curent 0.10
measurement duration: 0.0210 - estimated curent 0.12
measurement duration: 0.0207 - estimated curent 0.10
measurement duration: 0.0206 - estimated curent 0.10
measurement duration: 0.0207 - estimated curent 0.06
measurement duration: 0.0207 - estimated curent 0.07
^Cstopped by user
end of execution
pi@raspberrypi:~ $
- the python script

Code: Select all

#!/usr/bin/python
#-*- coding: utf-8 -*-#
# test current estimate measurement using PCF8591 board and
# a current transformer board HW-670 (with ZMCT103C current transformer)
# this not a precise measurment, just an evaluation of the current,
# it is fair enough to check when a device plugged on main power is one or off...
# or when current is changing effectively.
# I use it for low power consumption  ~<60 Watts,
# but the current transformer board is said to be up to 5 amp.

import sys , smbus , time
print("start main program")

i2cbus = smbus.SMBus(1)
PCF8591_addr = 0x48
PCF_ctrl_byte = 0x42
# bit 7 --> 0
# analog out disabled (bit 6 --> 0)
# 4 single ended input (bit 5 & 4 --> 00)
# bit 3 --> 0
# autoincrement inactive (bit 2 --> 0)
# Analog to Digital channel nr = 2 (bit 1 & 0 --> 10)

def init_PCF8591(): #initialyse PCF8591 A to D converter
    i2cbus.write_byte_data(PCF8591_addr, PCF_ctrl_byte, 0x80)

def read_current():
    data = []
    sum_current = 0
    start = time.time()
    for a in range(82): #nr of read to have about 20 ms reading
        data.append(i2cbus.read_byte(PCF8591_addr))
    stop = time.time()

    for a in range(len(data)): #a reading is AC, sum values acquired during 20 ms (50 Hz period)
        sum_current += abs(data[a]-128)
    # value adjusted to get a consistent reading
    # using a basic soldering iron of 30W and
    # adjusting also the pot on current trransformer board
    sum_current *=  0.0050
    duration = stop - start #calculate measurement period to ensure we remain clse to 200 msec
    return(sum_current, duration)

############## main ##############

init_PCF8591()

try:
    while True:
        current, dur = read_current()
        print('measurement duration: {:03.4f} - estimated curent {:03.2f}'.format(dur, current))
        time.sleep(0.3)
except KeyboardInterrupt:
    print('stopped by user')

sys.exit('end of execution')

Return to “Automation, sensing and robotics”