Arduino 220v AC dimmer with remote control with code
This Arduino project help you to control brightness of a 220V dimmable lamp or speed of ceiling fan (or any other electric motor) with an IR remote control using Arduino.
Max. power which can controlled with this dimmer depend on triac model.(BT136 can bear 4A according to Datasheet).
This project consist of 4 basic parts:
1-Microcontroller(Arduino)
2-Zero-cross detector(Bridge and Optocoupler)
3-Display(7Segment)
4-Phase angel controller(Tiac and Triac driver)
The load can controlled by Up and Down bush button or with IR remote controller.
Circuit diagram:
Parts list:
Arduino UNO(or other version)
Max. power which can controlled with this dimmer depend on triac model.(BT136 can bear 4A according to Datasheet).
This project consist of 4 basic parts:
1-Microcontroller(Arduino)
2-Zero-cross detector(Bridge and Optocoupler)
3-Display(7Segment)
4-Phase angel controller(Tiac and Triac driver)
The load can controlled by Up and Down bush button or with IR remote controller.
Circuit diagram:
Parts list:
Arduino UNO(or other version)
Optocoupler pc817
Triac driver MOC3021(Or any other Random-phase triac driver)
1 Digit 7 Segment common cathode
Triac BT136
Diode bridge(4pcs 1N4007)
Push bottom(4pcs)
Triac driver MOC3021(Or any other Random-phase triac driver)
1 Digit 7 Segment common cathode
Triac BT136
Diode bridge(4pcs 1N4007)
Push bottom(4pcs)
Resistor 100K(2pcs)
Resistor470 ohm (2pcs)
Resistor 330 ohm
Resistor470 ohm (2pcs)
Resistor 330 ohm
Resistor 100 ohm 1W
Resistor 10K
Capacitor 100nF 400v
IR receiver 1830B
IR receiver 1830B
Important NOTES:
1- You have to using only RANDOM-phase triac driver such as MOC 3021 or MOC 3023 , etc, and don't use MOC3041 or other triac driver with Zero-cross detector.
2-This project directly connected to 110/220v AC so can be very dangerous for beginners.
3-Red components(100 ohm resistor and 100nF capacitor) shown in above diagram should only used when the load is inductive.
4-Don't use this Dimmer for non-dimmable load such as CFL lamps, dimming can burn your lamp.
5-The number after "Case" in the code need to change with number that shown to you in "Serial Monitor" when you press a button on your remote control toward the IR receiver .
You can watch in following video how this project works:
1- You have to using only RANDOM-phase triac driver such as MOC 3021 or MOC 3023 , etc, and don't use MOC3041 or other triac driver with Zero-cross detector.
2-This project directly connected to 110/220v AC so can be very dangerous for beginners.
3-Red components(100 ohm resistor and 100nF capacitor) shown in above diagram should only used when the load is inductive.
4-Don't use this Dimmer for non-dimmable load such as CFL lamps, dimming can burn your lamp.
5-The number after "Case" in the code need to change with number that shown to you in "Serial Monitor" when you press a button on your remote control toward the IR receiver .
You can watch in following video how this project works:
Code:
#include "IRremote.h"
//-----( Declare Constants )-----
int receiver = 13;
//-----( 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 = 3; // Output to Opto Triac
int buton1 = 4; // first button at pin 4
int buton2 = 5; // 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 = 9; // step for count;
int B = 6;
int C = 7;
int D = 8;
int E = 12;
int F = 10;
int G = 11;
// 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 16724175:
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 16748655:
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 16758855:
{
if (dim<127)
{
dim = dim + pas;
if (dim>127)
{
dim=128;
}
}
}
break;
case 16730295:
{
{
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);
}
//-----( Declare Constants )-----
int receiver = 13;
//-----( 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 = 3; // Output to Opto Triac
int buton1 = 4; // first button at pin 4
int buton2 = 5; // 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 = 9; // step for count;
int B = 6;
int C = 7;
int D = 8;
int E = 12;
int F = 10;
int G = 11;
// 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 16724175:
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 16748655:
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 16758855:
{
if (dim<127)
{
dim = dim + pas;
if (dim>127)
{
dim=128;
}
}
}
break;
case 16730295:
{
{
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);
}
Comments
Post a Comment