Wednesday, August 30, 2017

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:

Arduino 220v AC dimmer with remote control with code
 

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)
Resistor 100K(2pcs)
Resistor470 ohm (2pcs)
Resistor 330 ohm
Resistor 100 ohm 1W
Resistor 10K
Capacitor 100nF 400v
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 .

Arduino 220v AC dimmer with remote control with code


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

Sunday, December 18, 2016

Auto light switch for bathroom using laser and IC4017

Many people forgets the bathroom and toilet lamps in ON state, absolutely this causes increasing electric bill and even decrease lamps lifespan. This circuit is a simple way to turning ON and OFF the bathroom or toilet lamp automatically without using microcontroller.
The circuit based on the counter IC4017 and laser. when a person go to bathroom his body cut the laser ray that is focused on the LDR in another side of the door frame, connecting and disconnecting the laser ray changed the LDR electric resistance and giving a positive signal to pin14 of the IC4017 through transistor 2N2222, since pin4 jumped to reset pin (pin15) so the output selector will be changed between pin3 and pin2. Pin3 run the transistor to running the relay.
Two LEDs showing ON state and OFF state. 330 ohm resistor used to LEDs protection by giving 15mA current to them.
You can use any other NPN transistor rather than 2N2222 such as BC547, C9013, 2N5551, etc. 
This circuit is suitable for light switching for any place that used by one person such as toilet and bathroom, you can't use it for the room because when second person coming  inside the room the lamp will be turns OFF.

Part list:
IC4017
Transistor 2N2222
Laser 5mW
Relay 5v 
LEDs
LDR
Resistor 1K
Resistor 200K
Resistor 2.2K
Resistor 330R
Capacitor 10nF
Diode 1N4007


Auto light switch for bathroom using laser and IC4017

Auto light switch for bathroom using laser and IC4017

In following video you can watch how the circuit works:


Saturday, November 26, 2016

220V Auto room light switch using Arduino and Lazer (Visitor counter)

Many peoples forgets the Room lamps in running state when nobody in the room, this causes increasing electric bill or battery energy losing in solar home.
This Arduino project is a visitor counter that turns OFF the lamp when nobody in the room automatically, it also can used for lamps of bathroom, toilet, kitchen, etc.
Visitor counter can makes by using different types of sensors, such as IR ray, Ultrasonic, Avoidance sensor, Laser ray, etc.
Here i used Laser because the laser has long distance range, Avoidance sensor is very easy to use but its distance rang is lower than 30cm.
You can make this project with LCD display for showing the numbers of people in the room but as you can see in the video i don't used display because its not important for me, any way the code is same with or without display.
This project help to power consumption optimizing. 

Parts list:
Arduino board 
1 Channel Relay module
Laser diode(2 pcs)
Light sensor modules(2 pcs)
Adapter 5v 200mA
If you want to using display, need :
16X2 LCD Display
Resistor 10K
Resistor 220 ohm

220V Auto room light switch using Arduino and Lazer (Visitor counter)


220V Auto room light switch using Arduino and Lazer (Visitor counter)

220V Auto room light switch using Arduino and Lazer (Visitor counter)

Manual OFF switch used when you are in the room but want to turning OFF the lamp for sleep.

220V Auto room light switch using Arduino and Lazer (Visitor counter)


I used 1000uF 16v capacitor for power line to protect the arduino board from sudden voltage raising but its not necessary.

220V Auto room light switch using Arduino and Lazer (Visitor counter)

220V Auto room light switch using Arduino and Lazer (Visitor counter)



There are some important notes for making this project: 

1-Put the laser diode in the door frame before LDRs to determine the LDRs suitable position and to making sure the laser beam and LDRs are in same straight line. 

2-The laser diodes must set in suitable height to become suitable for all peoples with different long. also you have pay attention to door handle position, when the door is closed, handle maybe cuts the laser beam so put the laser beam above the handle.

3-The laser is harmful for eyes when its directly toward your eyes. 

4-One problem will happen for ONLY FIRST TIME USING this project. That problem is:
When you are in the room and turns this project ON for first time the light will be OFF because the Arduino count zero people come in the room(nobody) when you try to getting out the room the arduino turns ON the light because it count -1 people in the room. So in first time please try to get out the room without cutting the laser beam.
I'm trying solve this problem in the code.  



Code:

#include<LiquidCrystal.h>
LiquidCrystal lcd(0, 1, 3, 4, 5, 6);
#define in 11
#define out 12
#define relay 13
int count=0;
void IN()
{
    count++;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}
void OUT()
{
  count--;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}
void setup()
{
  lcd.begin(16,2);
  lcd.print("Visitor Counter");
  delay(2000);
  pinMode(in, INPUT);
  pinMode(out, INPUT);
  pinMode(relay, OUTPUT);
  lcd.clear();
  lcd.print("Person In Room:");
  lcd.setCursor(0,1);
  lcd.print(count);
}
void loop()

 
  if(digitalRead(in))
  IN();
  if(digitalRead(out))
  OUT();
 
  if(count<=0)
  {
    lcd.clear();
    digitalWrite(relay, LOW);
    lcd.clear();
    lcd.print("Nobody In Room");
    lcd.setCursor(0,1);
    lcd.print("Light Is Off");
    delay(200);
  }
 
  else
    digitalWrite(relay, HIGH);
 
}

Wednesday, November 23, 2016

AC 220V Frequency counter using arduino

In this project i used Arduino and some other electronic components for AC mains frequency measurement.
Since Frequency is equal to: 1 divided by time period, so in first we should measure the time period. Time Period is the time of one full wave in AC voltage, means positive half wave+negative half wave.
Here i used optocoupler for detecting the frequency of AC voltage. 47K resistor decrease the current to about 5mA for running the optocoupler and one diode for half wave rectification  So the arduino here calculate the ON time and OFF time of optocoupler that connected to Pin 13 in arduino board, then divided 1 by (ON+OFF) Time for give the frequency. 
Since in arduino code the time always must insert as millisecond so we devide 1000 by ON+OFF Time for give the frequency.
The circuit 100% insulated from mains voltage and can not be harmful for your arduino board.
You can use any other available optocoupler rather than 817B. 

AC 220V Frequency counter using arduino

Parts list: 
Arduino board
LCD display 16x2
Resistor 10K
Resistor 220 ohm
Resistor 47K / 1W
Diode 1N4007
Potentiometer 10K
Optocoupler 817B or any optocoupler  

NOTE: Don't touch the circuit when it connected to mains voltage.
 
AC 220V Frequency counter using arduino

AC 220V Frequency counter using arduino



Code:

 #include <LiquidCrystal.h>
int input=13;

int high_time;
int low_time;
float time_period;
float freq;
float frequency;
LiquidCrystal lcd(0, 1, 3, 4, 5, 6);
void setup()
{
pinMode(input,INPUT);
lcd.begin(16, 2);
}
void loop()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Frequency Meter");

high_time=pulseIn(input,HIGH);
low_time=pulseIn(input,LOW);


time_period=high_time+low_time;
time_period=time_period/1000;
frequency=1000/time_period;
lcd.setCursor(0,1);
lcd.print(frequency);
lcd.print(" Hz");
delay(500);
}

Tuesday, August 9, 2016

Simple 220v emergency LED light

This circuit is an emergency lamp that charge the battery by 220v electric line and when the power cut, run the LED lamp automatically.
The battery used in this circuit is a 3.7v Li-ion battery 2000mAH. charging current in this circuit is about 50mA, this current is good for slow charging since this circuit is an emergency lamp so it leaves under charging for a long time to charge the battery so high charging current maybe decrease the battery life.
The circuit is simple as possible and very helpful. 
You can use any other PNP transistor rather than S9012.

Parts list:
Transistor S9012
Li-ion 3.7v battery 1000 to 3000mAH(18650)
Resistor 270 ohm 2W
Resistor 1K
Resistor 5 ohm 2W
Capacitor 820nF 400v
Capacitor 470uF 16v
Diode bridge 1A or 4 x 1N4007
Zener diode 4.8v 1W
Diode 1N4007
White LED(24 pcs or more)


Simple 220v emergency  LED light

Simple 220v emergency  LED light







Monday, July 25, 2016

Fridge door alarm circuit with delay time

New fridge models contain an alarm to notice door leaves open, it prevent energy losing.
This circuit is a simple fridge door alarm that active when the fridges door leaves open for 15 seconds, this delay time depend on 10uF capacitor so you can increase this delay time by increasing the capacitor value.
Alarm sound frequency depend on second capacitor value.
The circuit run with 9v battery and draw 40mA when the alarm is active and lower than 10mA when it is in standby state. Its better to use a rechargeable 9v battery.

Parts list:
IC555 (2pcs)
Buzzer
LDR
Resistor 1M
Resistor 1.5M
Resistor 100K
Capacitor 10uF
Capacitor 220nF
Battery 9v

Fridge door alarm circuit with delay time

Fridge door alarm circuit with delay time

Friday, July 22, 2016

1.5V Electric fly zapper circuit

This circuit is an electric zapper that can convert 1.5v to very high voltage, so it can used as a fly killer. you have to connect two thin metal grid to the output exactly look like to commercial fly zapper. output voltage can reach to more than 2000v.
You can use almost any other PNP transistor rather than S8550 and any other NPN transistor rather than S9013.
The circuit run with 1.5v and consume 40mA to 100mA.

Parts list:
Transistor S9013
Transistor S8550
Resistor 30K
Capacitor 100nF
Capacitor 2uF 400v (8pcs)
Diode 1N5408 (8pcs)
Transformer 6v
Battery 1.5v 
Two thin metal grid



1.5V Electric fly zapper circuit1.5V Electric fly zapper circuit