View Full Version : YHDC current sensor - the theory please
flotulopex
- 30th November 2025, 13:23
Hi All,
I'm resuming a project I started more than one year ago; it is about to use a YHDC current sensor.
10007
For now, I have a 16F18446 carrying a 12 bits ADC that I hope sufficient just to put me on the track and avoid the usage of an external higher resolution ADC chip adding "difficulties".
The YHDC sensor I have on my bench is a 100A:50mA model.
I found a lot of these drawings like this one below but I can't make them work or better said, I don't know how to "read" their output.
10010
Do I have to sample let's say a 30 to 50 times or so to grab an alternance, find out both zero-crossing values (or as close as possible) and then extract the higher value in between?
What is the correct way to do this?
Thanks for any explanation on this one :wink:
Jerson
- 1st December 2025, 03:25
If you are trying to read the magnitude of current flowing in the primary of the current transformer(CT), you would like to first amplify the AC output of CT using an op-amp. Then do a DC peak conversion on the output of the op-amp. Read this value. It will be proportional to the current flowing in the primary. The ADC on the PIC can now read this DC value in a poll.
richard
- 1st December 2025, 07:53
a simple digital peak detector will give a reasonable result,
eg
2v p-p input offset to approx 1.5v for a vcc of 3.3v for adc
10011
[arduino ide serial plotter]
at a 200us sample rate
10012
at 1000us sample rate , its still not bad
10013
'************************************************* ***************'* Name : peak_detector_ADC_16F18446.pbp
'* Author : richard
'* Notice :
'* : All Rights Reserved
'* Date :
'* Version : 1.0
'* Notes :
'====== PIC 16F18446 FUSES ================================================== ======================
#CONFIG
__config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_ON & _FCMEN_ON
__config _CONFIG2, _MCLRE_ON & _PWRTS_PWRT_64 & _LPBOREN_OFF & _BOREN_SBOREN & _BORV_LO & _ZCDDIS_OFF & _PPS1WAY_OFF & _STVREN_ON
__config _CONFIG3, _WDTCPS_WDTCPS_31 & _WDTE_SWDTEN & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
__config _CONFIG4, _BBSIZE_BB512 & _BBEN_OFF & _SAFEN_OFF & _WRTAPP_OFF & _WRTB_OFF & _WRTC_OFF & _WRTD_OFF & _WRTSAF_OFF & _LVP_OFF
__config _CONFIG5, _CP_OFF
#ENDCONFIG
' ====== REGISTERS ================================================== ===============================
ANSELA = % 00100000
ANSELC = % 00000000
TRISC = % 11101111
ADCON0 = % 10000100 'ADC is ON, RIGHT justified result
' ====== DEFINES ================================================== =================================
DEFINE OSC 32
DEFINE ADC_BITS 12 'Number of bits in ADCIN result
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
'DEFINE HSER_SPBRG 138 ' 57600 Baud @ 32MHz, -0.08%
DEFINE HSER_SPBRG 68 ' 115200 Baud @ 32MHz, 0.64%
SP1BRGH = 0
BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
pause 500
RC3PPS = 15 ;tx PORTC.3
RA0PPS = 15 ;tx PORTA.0 ;PK3
RC4PPS = 0 ;rx unused
LED VAR LATC.4
clear
new_read var word
State Var BYTE 'serial Bd (baud per second)
peak var word
last var word
' ====== setup PROGRAM ================================================== ===========================
led = 1
ADCIN 5, last
pause 1000
HSEROUT ["ready ",13,10]
led = 0
;707 -3670
' ====== MAIN PROGRAM ================================================== ===========================
MAIN:
ADCIN 5, new_read ' ADC
gosub getpeak
HSerout ["ADC:",dEC new_read,",peak:",dEC peak,13,10] ' PLOTTER
pauseus 200 ; sample rate
GOTO MAIN
getpeak:
if state==0 then ;rising
if new_read < last then
state = 1;falling
peak = last
endif
else ;falling
if new_read > last then
state = 0;rising
peak = last
endif
ENDif
last = new_read
return
flotulopex
- 1st December 2025, 08:47
Peak detector! That's it!
Thanks a lot for explaining and the code I can't wait to try when I'm back from office :biggrin:
Though, why do I always read about 3.3V measuring circuits and not 5V?
Also, a "peak detector" can be as simple as a resistor and a capacitor in parallel "smoothing" the output of the sensor, or like Jerson wrote, of the op-amp, right?
If "yes", how do I dimension them? Are trials and readouts on the scope the best way to go?
richard
- 1st December 2025, 11:23
why do I always read about 3.3V measuring circuits and not 5V?
my dev board has a 3.3v reg, otherwise can't see that it makes a difference
a "peak detector" can be as simple as a resistor and a capacitor in parallel "smoothing" the output of the sensor, or like Jerson wrote, of the op-amp, right?
not really , it needs a rectifier too. if the wave is not symmetrical the neg and pos peaks may be needed. plus it still needs adc code to read it in anyway
Are trials and readouts on the scope the best way to go?
almost essential, especially depending on the nature of the current being monitored,
if the current is not sinusoidal and/or varies rapidly then its going to need filtering and other compensation to get any sort of reasonable calibration
flotulopex
- 1st December 2025, 13:49
a "peak detector" can be as simple as a resistor and a capacitor in parallel "smoothing" the output of the sensor, or like Jerson wrote, of the op-amp, right?
not really , it needs a rectifier too. if the wave is not symmetrical the neg and pos peaks may be needed. plus it still needs adc code to read it in anyway
May I kindly ask for a principle circuit to start with the correct foot please?
I don't see how to "rectify" a very low AC signal - I'm an electrician and used to easely handle voltages over 230VAC but when it comes down to millivolts.... 😬
richard
- 1st December 2025, 14:07
Why do want a nasty old analogue peak detector, When you can create a digital one as per my code ?
Using the very cct you posted.
If you realy need to try analogue google ""peak detector circuit"
An accurate analogue peak detector uses an opamp to create an "ideal" diode [overcome the diodes vf ]
flotulopex
- 1st December 2025, 14:25
...sorry Richard, didn't try your code by now - still in office.... 😬
flotulopex
- 1st December 2025, 20:26
Richard,
I've connected my YHDC sensor one pin to ground and the other to RA5. Got readings (see hereunder).
Because I'm not sure what I'm doing right now with the current sensor, I tried to add a 1Mohms resistor and a 100nF polarized cap between GND and RA5. Same readings.
Just for info, my multimeter shows 0.157mVAC - I'm measuring a 230VAC 42W light bulb for now.
I get readings on my serial monitor but the values don't make too much sense to me, for now - there's going to be some kinf of scaling to do, no?
This is what I get for now with bulb "ON":
10014
And bulb "OFF":
10015
richard
- 1st December 2025, 22:11
It can't work wired the way you described. The ac signal must be biased by vcc/2 and be less than vcc peak to peak.
If the serial baud rate is too low it will ruin the sample rate.
If the burden resistance is incorrect extremely high voltage may be possible
Post code and schematic
HenrikOlsson
- 1st December 2025, 22:20
Have you connected your CT as shown in your first post? That is, the bottom to 2.5V (or VRef/2) and the top to input pin AND - VERY IMPORTANT - a load resistor shown as 33ohms in diagram? You must have a load resistor or the output voltage across the secondary of the CT can get very high, possibly damaging the ADC input.
Then, your CT produces 50mA with a 100A primary current. 1A primary give 0.5mA and so yout 42W load gives about 100uA secondary current. Keep that in mind when selecting the load resistor.
EDIT: OK, Richard beat me to it while I was typing.
tumbleweed
- 2nd December 2025, 00:26
You might want to specify the model number of the sensor since there are a number of different types, and the circuit shown may not even apply
amgen
- 2nd December 2025, 01:17
adjust resister to for the mid current used incase of some nonlinearity... shotkey diode has .3 volt drop
10016
flotulopex
- 7th December 2025, 21:27
Sorry guys for not reponding earlier - I'm still waiting for 20A:50mA current sensor to arrive; my 100A:50mA are far too low and "out of range" for what I intend to measure.
BUT I could grab a 5A:1V one from a friend today so I'm back on my workbench.
To have "better" values -measures with FLUKE 177- , I now use my 230VAC hairdryer:
Pos 1: fan only = YHDC:54mVAC - FLUKE: 1.1A
Pos 2: fan + low heating = YHDC: 170mVAC - FLUKE: 3.5A
Pos 3: fan + high heating = YHDC: 320mVAC - FLUKE: 6.6A
Since I now use a voltage output type sensor, I leave the burden resistor out, right?
10017
And here are my readings:
OFF:
10018
Pos 1:
10019
Pos 2:
10020
Pos 3:
10021
I can't still figure out what I'm reading.
richard
- 8th December 2025, 00:06
you cannot use a current transformer that way without a burden resistor.
by voltage sensor do you mean ACS712 or similar ?
if so that circuit is incorrect, typical use
10022
richard
- 8th December 2025, 00:55
I can't still figure out what I'm reading.
the output is intended to be plotted in the freely available easy to use arduino plotter, the average human will have nil chance of interpreting the data with such a small number of samples. if your load current is sinusoidal is should look similar to this
10023
richard
- 8th December 2025, 01:34
sample data
ADC:1632,peak:608ADC:2447,peak:608
ADC:3224,peak:608
ADC:3704,peak:608
ADC:3722,peak:608
ADC:3312,peak:3722
ADC:2528,peak:3722
ADC:1632,peak:3722
ADC:880,peak:3722
ADC:576,peak:3722
ADC:704,peak:576
ADC:1248,peak:576
ADC:2032,peak:576
ADC:2880,peak:576
ADC:3520,peak:576
ADC:3776,peak:576
ADC:3568,peak:3776
ADC:2928,peak:3776
ADC:2032,peak:3776
ADC:1180,peak:3776
ADC:644,peak:3776
ADC:608,peak:3776
ADC:992,peak:608
ADC:1656,peak:608
ADC:2528,peak:608
ADC:3296,peak:608
ADC:3714,peak:608
ADC:3718,peak:608
ADC:3296,peak:3718
ADC:2451,peak:3718
ADC:1551,peak:3718
ADC:864,peak:3718
ADC:576,peak:3718
ADC:736,peak:576
ADC:1272,peak:576
ADC:2079,peak:576
ADC:2928,peak:576
ADC:3568,peak:576
ADC:3776,peak:576
ADC:3552,peak:3776
ADC:2880,peak:3776
ADC:1984,peak:3776
ADC:1138,peak:3776
ADC:632,peak:3776
ADC:608,peak:3776
ADC:1008,peak:608
ADC:1760,peak:608
ADC:2608,peak:608
ADC:3338,peak:608
ADC:3736,peak:608
ADC:3696,peak:3736
ADC:3168,peak:3736
ADC:2316,peak:3736
ADC:1423,peak:3736
ADC:766,peak:3736
ADC:576,peak:3736
ADC:792,peak:576
ADC:1392,peak:576
ADC:2240,peak:576
ADC:3056,peak:576
ADC:3616,peak:576
ADC:3776,peak:576
ADC:3462,peak:3776
ADC:2752,peak:3776
ADC:1840,peak:3776
ADC:1031,peak:3776
ADC:608,peak:3776
ADC:632,peak:608
ADC:1088,peak:608
ADC:1856,peak:608
ADC:2690,peak:608
ADC:3424,peak:608
ADC:3760,peak:608
ADC:3680,peak:3760
ADC:3082,peak:3760
ADC:2240,peak:3760
ADC:1344,peak:3760
ADC:736,peak:3760
ADC:576,peak:3760
ADC:864,peak:576
ADC:1472,peak:576
ADC:2310,peak:576
ADC:3136,peak:576
ADC:3680,peak:576
ADC:3760,peak:576
ADC:3424,peak:3760
ADC:2656,peak:3760
ADC:1760,peak:3760
ADC:992,peak:3760
ADC:608,peak:3760
ADC:646,peak:608
ADC:1120,peak:608
ADC:1904,peak:608
ADC:2752,peak:608
ADC:3440,peak:608
ADC:3776,peak:608
ADC:3648,peak:3776
ADC:3056,peak:3776
ADC:2168,peak:3776
ADC:1295,peak:3776
ADC:704,peak:3776
ADC:576,peak:3776
ADC:880,peak:576
ADC:1520,peak:576
ADC:2368,peak:576
ADC:3168,peak:576
ADC:3696,peak:576
ADC:3744,peak:576
ADC:3376,peak:3744
ADC:2594,peak:3744
ADC:1692,peak:3744
ADC:934,peak:3744
ADC:608,peak:3744
ADC:655,peak:608
ADC:1150,peak:608
ADC:1934,peak:608
ADC:2800,peak:608
ADC:3462,peak:608
ADC:3760,peak:608
ADC:3610,peak:3760
ADC:3008,peak:3760
ADC:2144,peak:3760
ADC:1264,peak:3760
ADC:678,peak:3760
ADC:576,peak:3760
ADC:906,peak:576
ADC:1566,peak:576
ADC:2416,peak:576
ADC:3202,peak:576
ADC:3696,peak:576
ADC:3738,peak:576
ADC:3334,peak:3738
ADC:2544,peak:3738
ADC:1648,peak:3738
ADC:899,peak:3738
ADC:576,peak:3738
ADC:704,peak:576
sample wave, note peaks and mid point values
10024
flotulopex
- 8th December 2025, 09:02
Oh, I get it now, the "plotter".
I just need to monitor a current level that will trigger a relay.
I do have ACS712 but the problem is, the current has to pass inside (though) the component meaning I would need to modify the cabling inside an electric board where it is intended to be installed. But I want to avoid this - it has to be like SHELLY energy counters (https://www.shelly.com/products/shelly-pro-3em-x1) or so.
I just received a module, an analog AC Current Sensor SEN0211 unsing a YHDC current transformer: DFROBOT (https://wiki.dfrobot.com/Gravity_Analog_AC_Current_Sensor__SKU_SEN0211_?gad _source=1&gad_campaignid=22392107167&gbraid=0AAAAADucPlDiW7Wd8h57wezAZ1Fy3A9kb&gclid=Cj0KCQiAi9rJBhCYARIsALyPDtvIy3hq1v34XSqHu3x2 ak8giLe3DdatvZzzYXJRNgwIdnv6UZfgXa0aAgMxEALw_wcB). I'll give it a try.
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.