Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. My Project
  3. Safe AC dimmer with code

Safe AC dimmer with code

Scheduled Pinned Locked Moved My Project
3 Posts 2 Posters 75 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DbagioniD Offline
    DbagioniD Offline
    Dbagioni
    wrote on last edited by
    #1

    All

    Has anyone found a safe and reliable dimmer code to with with an AC dimmer? I have been racking my brains and cant find a good working code. I'm using commercially made pwm/zero cross dimmers from RobotDYN(see picture). I can get a 12 volt version working but have been unsuccessful in getting this to work. Disclaimer - Im a hardware guy not a code guy.
    alt text

    My end goal would be to get it working with the rotary encoder. This is the RAW pwm/zero crossing code.

    #include "hw_timer.h"
    #include <Time.h>
    #include <TimeLib.h>
    #include <TimeAlarms.h>
    
    //Pin Setups
    const int zcPin = 12; //zero-cross detection pin D6
    const int pwmPin = 13;  //pwm pin - fires the triac D7
    
    //Dimmer Settings including fades.
    byte fade = 1;
    byte state = 1;
    byte tarBrightness = 255; //"target" brightness - going to have go convert this to a precentage for OH
    byte curBrightness = 0; //current brightness level
    byte zcState = 0; // 0 = ready; 1 = processing;
    
    char lvl[50];
    
    void setup() {
      Serial.begin(115200); //Sets the data rate in bits per second (baud) for serial data transmission.
    
      //Dimmer pins -set up the pwm and zero cross pins
      pinMode(zcPin, INPUT_PULLUP);
      pinMode(pwmPin, OUTPUT);
      attachInterrupt(zcPin, zcDetectISR, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
      hw_timer_init(NMI_SOURCE, 0);
      hw_timer_set_func(dimTimerISR);
    
     } // END SETUP
    
    void dimTimerISR() {
      if (fade == 1) {
        if (curBrightness > tarBrightness || (state == 0 && curBrightness > 0)) {
          --curBrightness;
        }
        else if (curBrightness < tarBrightness && state == 1 && curBrightness < 255) {
          ++curBrightness;
        }
      }
      else {
        if (state == 1) {
          curBrightness = tarBrightness;
        }
        else {
          curBrightness = 0;
        }
      }
    
      if (curBrightness == 0) {
        state = 0;
        digitalWrite(pwmPin, 0);
      }
      else if (curBrightness == 255) {
        state = 1;
        digitalWrite(pwmPin, 1);
      }
      else {
        digitalWrite(pwmPin, 1);
      }
    
      zcState = 0;
    }
    
    void zcDetectISR() {
      if (zcState == 0) {
        zcState = 1;
    
        if (curBrightness < 255 && curBrightness > 0) {
          digitalWrite(pwmPin, 0);
    
          int dimDelay = 30 * (255 - curBrightness) + 400;//400
          hw_timer_arm(dimDelay);
        }
      }
    }
    

    Can anyone help?

    1 Reply Last reply
    0
    • Michiel van der WulpM Offline
      Michiel van der WulpM Offline
      Michiel van der Wulp
      wrote on last edited by
      #2

      @Dbagioni said in Safe AC dimmer with code:

      zcPin

      Which pin do you use? The comments say something else (twice) than the code.
      Which CPU do you use?
      For many Arduinos, you can only use pin 2 and 3.

      1 Reply Last reply
      0
      • DbagioniD Offline
        DbagioniD Offline
        Dbagioni
        wrote on last edited by
        #3

        The code I posted is the raw code for the zero-cross dimmer. When I had tried to merge the "dimmer with rotary encoder" sketch and the zero-cross code I had wired it up with the ZC pin as 2 and the PWM pin on 3. Encoder was wired: SW pin on 4, CLK pin on 5 and CE pin on 6. 7/8/A2 were used for status LED's.

        I was running it with an older NANO with the 328.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        22

        Online

        11.7k

        Users

        11.2k

        Topics

        113.1k

        Posts


        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • MySensors
        • OpenHardware.io
        • Categories
        • Recent
        • Tags
        • Popular