Shop OBEX P1 Docs P2 Docs Learn Events
Controlling an AC Dimmer Circuit via Smartpin PWM — Parallax Forums

Controlling an AC Dimmer Circuit via Smartpin PWM

Christof Eb.Christof Eb. Posts: 1,394
edited 2025-08-16 11:34 in Propeller 2

Goal is to control an AC universal motor with an AC dimmer module.
The module seems to look like or similar to this one: https://forums.ni.com/t5/LabVIEW-Interface-for-Arduino/Firmware-of-AC-dimmer/td-p/3427715 So it has a MOC3021 fototriac to trigger the power triac. (It seems to have an additional snubber.) And also a 4N25SR2VM fototransistor which provides a 100Hz trigger pulse after each zero cross. (I have ordered it, but must wait for it.)
Here is some simple Arduino code: https://electronoobs.com/eng_circuitos_tut20_code2.php

I could do the code using an extra COG but I think, it should be possible to output repeated trigger pulses using Smartpin pwm mode? Frame length is fixed 0,01 seconds for 100 Hz and pulse length is also fixed to perhaps 100 microseconds. So we actually do not modulate the pulse width here. To get different power values, the alignment of those trigger pulses to the zero crossings must be changed instead. I think that precision of both mains frequency and the crystal is very high, so that it should be sufficient to realign pulses to zero cross after some seconds.

From the docs:

%01001 = PWM sawtooth

This mode overrides OUT to control the pin output state.

X.[15..0] establishes a base period in clock cycles which forms the empirical high-time and low-time units.

X.[31..16] establishes a PWM frame period in terms of base periods.

Y.[15..0] establishes the PWM output value which gets captured at each frame start and used for its duration. It should range from zero to the frame period.

A counter, updating at each base period, counts from one up to the frame period. Then, Y.[15..0] is captured, IN is raised, and the process repeats.

At each base period, the captured output value is compared to the counter. If it is equal or greater, a high is output. If it is less, a low is output. Therefore, a zero will always output a low and the frame period value will always output a high.

During reset (DIR=0), IN is low, the output is low, and Y.[15..0] is captured.

**Do I understand this right: When I start PWM smartpin mode, then the first pulse starts immediately? **
So all I have to do is:
Wait for zero cross
Wait for length of the part of the power cycle, the power must still be off
Start PWM

To start PWM I have:

    int xvalue= (1000<<16) + (_clkfreq/100)/1000;
        _pinclear(TOS);
        _pinstart(TOS,P_OE | P_PWM_SAWTOOTH, xvalue, 10); // 100us

pin-number is in variable TOS.

I wonder, if _pinstart() alone is sufficient to realign?

Cheers and thanks for your comments!
Christof

Comments

  • JonnyMacJonnyMac Posts: 9,417
    edited 2025-08-16 15:20

    I wrote a 4-channel AC dimmer for the P1 that seemed to work well -- though looking back at the code now I'd think it could be improved (it's attached for your entertainment ).

    At first I had doubts about using the PWM smart pin mode, but with the P2 speed, you may be on to something. If I were doing this again I'd probably build a ZC detect circuit (I used the H11AA1 in my board) and connect that and the triac control (PWM) output to a Logic Analyzer. It looks like you're using FlexProp so the compiled code will have minimal delays. My code syncs with ZC on every cycle. You could start PWM in the ZC and by inverting the pin (and your PWM value), have the "on" portion of the cycle arrive at the desired point following ZC.

Sign In or Register to comment.