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,410
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,430
    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.

  • Received the RobotDyn Dimmer Module and got it working.
    Zero cross pulses are about 350 µsecs long.
    Without realignment, in the oscilloscope you can see the zero cross and the pulse train drift against each other. Realignment has to happen at least about ten times a second.

    The routine for P2XCForth to start the pulse train is:

    PRIMI(startTriac) { // ( pin -- ) starts 100Hz Trigger pulses NOW
            _pinclear(TOS);
            _pinstart(TOS,P_OE | P_PWM_SAWTOOTH, (1000<<16) + (_clkfreq/100)/1000, 10); // 100us
            _drop();
    }
    

    So the Forth code to align and start the pulse train is:

    variable beTriac        0 beTriac ! \ value in us 0....9999
    9000 constant maxWait \ us
    
    
    : setTriac \ from variable beTriac 0...9999
       beTriac @ 0= if
          triacPin pin@ drop \ switch off smartpin
          0 triacPin wrPin   exit
       then
       beTriac @ 9999 = if
          triacPin pin@ drop \ switch off smartpin
          0 triacPin wrPin
          1 triacPin pin! \ switch on pin
          exit
       then
    
       begin zcPin pin@ until
       maxWait beTriac @ -   0 max maxWait min \ calculate time to wait
       begin zcPin pin@ 0= until
       getus +
       begin dup getus < until drop
       triacPin startTriac
    ;
    

    This routine can be called by a task of the cooperative multitasking as it is not very time critical.
    So all-in-all the concept to use smartpin PWM to generate the pulse train to trigger the triac is working.

    @JonnyMac said:
    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.

    Thank you, for your input!
    Your last sentence puzzled me, if this might work. I have not dared to do it this way, because I am careful, that the pulse never moves into the next phase. I think, that you need some safety margin there.

  • JonnyMacJonnyMac Posts: 9,430

    Received the RobotDyn Dimmer Module and got it working.

    I found that on Amazon so I will order and give my ideas a try.

Sign In or Register to comment.