Monday, July 16, 2018

Arduino 220V AC Dimmer (JLCPCB)

220V AC Dimmer using Arduino and making PCB order from JLCPCB 

Arduino 220V AC Dimmer

Wiring:


Arduino 220V AC Dimmer
Convert to PCB using EasyEDA:

Arduino 220V AC Dimmer

Arduino 220V AC Dimmer

PCB order on JLCPCB.COM:

Arduino 220V AC Dimmer (JLCPCB)



Parts list:
Arduino Nano
Triac driver MOC3021
Optocoupler  PC817
Resistor 100K (2pcs)
Resistor 470R (2pcs)
Resistor 100R 1W
Resistor 330R
Resistor 10K
Capacitor 100nF 400V
7Segment (Common cathode)
Triac BT136
Diode bridge 0.5A
IR receiver 1838B
Terminal connector 2Pin (2pcs) 
Push-button switch (2pcs)

Download my Gerber File from here:



Video: 


Code:

#include "IRremote.h"
//-----( Declare Constants )-----
int receiver = 9;
//-----( Declare objects )-----
IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'
//-----( Declare Variables )-----


#include <TimerOne.h>           // Avaiable from http://www.arduino.cc/playground/Code/Timer1

volatile int i=0;               // Variable to use as a counter
volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 11;                 // Output to Opto Triac
int buton1 = 12;                 // first button at pin 4
int buton2 = 10;                 // second button at pin 5
int dim2 = 0;                   // led control
int dim = 128;                  // Dimming level (0-128)  0 = on, 128 = 0ff
int pas = 10;                   // step for count;
int A = 4;                  // step for count;
int B = 5;
int C = 6;
int D = 7;
int E = 8;
int F = 3;
int G = 13;
// version: 4m7 (15.04.2013 - Craiova, Romania) - 16 steps, 4 button & LED blue to red (off to MAX)
// version: 7m6.1 (23.01.2014 - Craiova, Romania) - 16 steps, 2 button & LCD1602

int freqStep = 75;    // This is the delay-per-brightness step in microseconds.

char incomingByte;  // incoming data from serial 9bluetooth)

void setup() {  // Begin setup

  Serial.begin(9600); // initialization
 
  irrecv.enableIRIn(); // Start the IR receiver (classic remote)

  pinMode(buton1, INPUT);  // set buton1 pin as input
  pinMode(buton2, INPUT);  // set buton1 pin as input
  pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);                       
  attachInterrupt(0, zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
  Timer1.initialize(freqStep);                      // Initialize TimerOne library for the freq we need
  Timer1.attachInterrupt(dim_check, freqStep);     
  // Use the TimerOne Library to attach an interrupt

}

void zero_cross_detect() {   
  zero_cross = true;               // set the boolean to true to tell our dimming function that a zero cross has occured
  i=0;
  digitalWrite(AC_pin, LOW);
}                                

// Turn on the TRIAC at the appropriate time
void dim_check() {                  
  if(zero_cross == true) {             
    if(i>=dim) {                    
      digitalWrite(AC_pin, HIGH);  // turn on light      
      i=0;  // reset time step counter                        
      zero_cross=false;    // reset zero cross detection
    }
    else {
      i++;  // increment time step counter                    
    }                               
  }   
}                                     


//-----( Declare User-written Functions )-----
void translateIR() // takes action based on IR code received

{
  switch(results.value)
  {
 
   
case 16714230:
 
    dim=128;
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, LOW);
    }
    break;
   
case 16744575: 
    dim=120;
    {
    digitalWrite(A, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, LOW);
    }
    break;

case 16728255: 
    dim=105;
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, LOW);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, LOW);
    digitalWrite(G, HIGH);
    }
    break;

case 16760895: 
    dim=90;
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, HIGH);
    }
    break;
   
case 16720095: 
    dim=75;
    {
    digitalWrite(A, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
    }
    break;

case 16752735: 
    dim=60;
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
    }
    break;

case 16736415: 
    dim=45;
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
    }
    break;

case 16769055: 
    dim=30;
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, LOW);
    }
    break;

case 16716015: 
    dim=15;
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
    }
    break; 
   
case 16722390: 
    dim=00;
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
    }
    break;  


  case 16716270: 
    {
    if (dim<127) 
   {
    dim = dim + pas;
    if (dim>127)
    {
     dim=128;
    }
    }
    }
    break;

  case 16724430: 
    {
      {
  if (dim>5) 
  {
     dim = dim - pas;
  if (dim<0)
    {
      dim=0;  // in vechiul sketch era 1
    }
   }
   }
   }
    break;
  
   if(dim>100)
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, LOW);
    }
  
    default:
    Serial.println(results.value);
  }

}


void loop() { 
  digitalWrite(buton1, HIGH);
  digitalWrite(buton2, HIGH);


 if (digitalRead(buton1) == LOW)  
   {
  if (dim<127) 
  {
    dim = dim + pas;
    if (dim>127)
    {
      dim=128; // in vechiul sketch era 127
    }
  }
   }
  if (digitalRead(buton2) == LOW)  
   {
  if (dim>5) 
  {
     dim = dim - pas;
  if (dim<0)
    {
      dim=0;  // in vechiul sketch era 1
    }
   }
   }
    while (digitalRead(buton1) == LOW) {  }             
    delay(10); // waiting little bit... 
    while (digitalRead(buton2) == LOW) {  }             
    delay(10); // waiting little bit...   

 if(dim>127)
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, LOW);
    }
   
      
 if(127>dim && dim>115)
    {
    digitalWrite(A, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, LOW);
    }

 if(115>dim && dim>105)
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, LOW);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, LOW);
    digitalWrite(G, HIGH);
    }

 if(105>dim && dim>90)
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, HIGH);
    }
   
 if(90>dim && dim>75)
   {
    digitalWrite(A, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
    }

 if(75>dim && dim>60)
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
    }

 if(60>dim && dim>45)
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
    }

 if(45>dim && dim>30)
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, LOW);
    }

 if(30>dim && dim>15)
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
    }
     
 if(15>dim && dim>00)
    {
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, LOW);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
    }

// remote
   if (irrecv.decode(&results)) // have we received an IR signal?
  {
    translateIR();
    irrecv.resume(); // receive the next value
  } 
 delay (100);
}