ควบุมแรงดันไฟฟ้า ac ด้วย pwm

ช่วยสอนวิธีการควบคุมแรงดันไฟฟ้า ac ด้วย pwm ด้วย บอร์ด arduino uno หน่อยครับ แบบที่ปรับแรงดันขึ้นลงได้ครับ

โดยหลักการก็ประมาณนี้ครับ

ต่อจริง

สวิตช์หรี่ไฟ AC หรือ (AC Dimmer) โดยใช้ทรานซิสเตอร์ IGBT สับคลื่นไซน์ตามเอาต์พุต PWM จาก Arduino จะหรี่แสงหรือเพิ่มแสงความสว่างที่ต้องการด้วยคำสั่งเดียว

analogWrite(พิน, ค่า);

โค้ตตัวอย่าง

หรือเราจะเขียนให้ arduino รับค่าความต้านทานมาเพื่อปรับเปลี่ยนค่า PWM ก็ได้เช่นกัน โดยไม่ต้องเขียนแล้วอัพลงบอร์ดใหม่

โมดูลหรี่ไฟก็มีขายอยู่คือ Dimmer Module PWM control 220VAC 10A เป็นโมดูลหรี่ไฟ หลอดไฟ 220VAC สามารถใช้งานได้กับอุปกรณ์ที่เป็นขดลวด เช่น เตารีด หลอดไฟแบบใส้ มอเตอร์ ฮีตเตอร์ รองรับอุปกรณ์ที่ใช้ไฟฟ้ากระแสสลับแรงดัน 100V ถึง 250V รองรับกระแสไฟฟ้าสูงสุดถึง 10A

Code

#include "hw_timer.h"          
const byte zcPin = 12;
const byte pwmPin = 13;  

byte fade = 1;
byte state = 1;
byte tarBrightness = 255;
byte curBrightness = 0;
byte zcState = 0; // 0 = ready; 1 = processing;
void setup() {
  Serial.begin(115200);   
  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);
}

void loop() {
  // put your main code here, to run repeatedly:
    if (Serial.available()){
        int val = Serial.parseInt();
        if (val>0){
          tarBrightness =val;
          Serial.println(tarBrightness);
        }
        
    }
}


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);
    }
  }
}

libraly

ผมลองตามนี้เเล้วไม่ได้ครับ