Sunday, May 20, 2018

Arduino 220V AC Power meter using PZEM004T

Arduino 220V AC Power meter using PZEM004T
Library : https://github.com/olehs/PZEM004T

Part list:
Arduino board
LCD 16x2
PZEM004T module
Resistor 220 ohm
Potentiometer 10K


Arduino 220V AC Power meter using PZEM004T

Video:


CODE:

#include <SoftwareSerial.h> // Arduino IDE <1.6.6

#include <PZEM004T.h>

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

PZEM004T pzem(2,3);  // (RX,TX) connect to TX,RX of PZEM

IPAddress ip(192,168,1,1);


void setup() {

  Serial.begin(9600);

  pzem.setAddress(ip);
  lcd.begin(16, 2); // lcd rows and columns

  
}


void loop() {
 


  float v = pzem.voltage(ip);

  if (v < 0.0) v = 0.0;

  Serial.print(v);Serial.print("V; ");
  lcd.setCursor(0,0);
  lcd.print("V= ");
  lcd.setCursor(2,0);
  lcd.print(v);


  float i = pzem.current(ip);

  if (i < 0.0) i = 0.0;

  Serial.print(i);Serial.print("A; ");

  lcd.setCursor(9,0);
  lcd.print("A= ");
  lcd.setCursor(11,0);
  lcd.print(i);




  float p = pzem.power(ip);

  if (p < 0.0) p = 0.0;


  Serial.print(p);Serial.print("W; ");
  lcd.setCursor(9,1);
  lcd.print("W= ");
  lcd.setCursor(11,1);
  lcd.print(p);




  float e = pzem.energy(ip);

  Serial.print("PF= ");Serial.print((p)/(v*i));

  lcd.setCursor(0,1);
  lcd.print("PF=");
  lcd.setCursor(3,1);
  lcd.print((p)/(v*i));

 
  Serial.println();

  delay(100);
}
 

Thursday, May 17, 2018

High voltage probe for multimeter

High voltage probe for multimeter

High voltage probe for multimeter


High voltage probe for multimeter


Monday, January 8, 2018

Wireless voltmeter using Arduino and RF 433MHz receiver and transmitter

In this project i showed you how to make a wireless voltmeter using arduino and RF 433MHz receiver and transmitter.
You can use this code and concept for many other projects, any project which used sensor with analog voltage output to sense temperature, gas leakage or light , etc.
For example you can put a LM35 temperature sensor rather than potantiometer, you just have to putting its formula which convert LM35 output pin voltage to exact temperature.
You also can make a wireless AC or DC power meter by doing some change in code and hardware (using hall effect current sensor and single phase voltage sensor).
This type of RF module has distance range up to 200 meters according to datasheet but when i bought this RF module from Aliexpress its distance rang was very low, but i set its adjustable RF coil very carefully and i get very higher distance rang.(Don't play with RF coil if your module works good), You also have to use a 6 inch antenna to increase distance range to max as possible. 
This RF module is cheap and simple to use and suitable to works with 5v.

Parts list:
Arduino any version (2pcs)
RF 433MHz receiver and transmitter
Potentiometer 10K (2pcs)
LCD display 16x2

Download library of RCSwitch.h from this link:


Receiver Wiring:


Wireless voltmeter using Arduino and RF 433MHz receiver and transmitter
Receiver Code:

#include <RCSwitch.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 7);
RCSwitch mySwitch = RCSwitch();
float value;
void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  mySwitch.enableReceive(0);
 // Receiver on inerrupt 0 => that is pin #2
}

void loop() {
lcd.clear();
     
  if (mySwitch.available()) {
   
    int value = mySwitch.getReceivedValue();
    lcd.setCursor(0,0);
      lcd.print("Voltage=");
    if (value == 0) {
      Serial.print("Unknown encoding");
     
    } else {
     
     
      lcd.setCursor(0,1);
      lcd.print((value * 5.0)/1024.0);
      lcd.print(" ");
      delay(500);
    }

    mySwitch.resetAvailable();
   
  }
}


Transmitter Wiring:


Wireless voltmeter using Arduino and RF 433MHz receiver and transmitter


Transmitter Code:

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

void setup()
{
  mySwitch.enableTransmit(7); //Connect data transmitter to Pin7 of Arduino
  }


void loop() {
 int sensorValue = analogRead(A0);
 delay(100);
 mySwitch.send(sensorValue , 12);
 delay(2000); 
}

Video:
In this video you can see how this project works


Monday, December 18, 2017

Power factor measurment using Arduino

In this project i used arduino and XOR gate to power factor measurement.

What is power factor?
Power factor is a number between 0 and 1 refers to phase shift between voltage phase and current phase in AC appliance.

Power factor measurment using Arduino

How to measure PF with Microcontroller (Arduino)?

To measuring power factor with microcontrollers in first we used Op-Amp to zero cross detection to find the phase shift between current and voltage phase, the op-amp convert the sine wave signal comes from CT and PT with different amplitude to square wave with about 4v amplitude, then we connect these two square wave to XOR gate inputs. 
XOR gate output will be 1 just when the input have different signal so when the load is resistive XOR gate output is 0 because both voltage and current phases start and ends in the same time, but when the load is inductive or capacitive XOR output is 1 because there are phase shaft between voltage and current,

Power factor measurment using Arduino


Hence we can find the power factor by measuring the "ON-time" of XOR output, and put it in the power factor formula :


PF= COS(f*dt*360)

Where:
PF=Power Factor
f= Frequency
dt=Time difference between two phases and/or XOR output ON-time
Circuit:
Here we have two circuits, the first one using Current transformer(CT) and Voltage or potential transformer(PT), the only difference between them is that second circuit with no transformer and no insulation between microcontroller and main voltage so its dangerous if you touch any point in the circuit, but its cheap, small size, light weight and similar to commercial socket power meter.    
Filtering capacitors in this circuit is very important to removing power supply noise( voltage ripple) to insure logic gate works well.

Power factor measurment using Arduino
Power factor measurment using Arduino
Arduino LCD wiring:
Power factor measurment using Arduino

 
Results:

This picture shown voltage and current phases when PF=1


XOR output signal when PF=1



Power factor measurment using Arduino
Power factor measurment using Arduino



Power factor measurment using ArduinoPower factor measurment using Arduino



Power factor measurment using ArduinoPower factor measurment using Arduino



Power factor measurment using Arduino
Power factor measurment using Arduino



NOTES:
1-This project deal with main voltage so its dangerous.
2-However transformers haven't polarity but here transformer winding point(dot) is very important, because if CT and PT dot don't match, the arduino will read low PF even with pure resistive load.

Power factor measurment using Arduino


In this video you can see how this project works:



 
You can improve this project to power factor correction by adding some relay and capacitor and improving the code.


Parts list:
Arduino board
IC 4030 or IC 4070 (XOR gate)
Voltage Transformer 15V or less
Current transformer
Resistor 100K
Resistor 220 ohm
Potentiometer 10K or 100K
Capacitor 47pF (2pcs)
Capacitor 100nF
Capacitor470uF
LCD display 16x2
Diode 1N4148 (4pcs)
LM324 or 741(2pcs)


CODE:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int pin = 13;
float rads = 57.29577951; // 1 radian = approx 57 deg.
float degree = 360;
float frequency = 50;
float nano = 1 * pow (10,-6); // Multiplication factor to convert nano seconds into seconds

// Define floats to contain calculations

float pf;
float angle;
float pf_max = 0;
float angle_max = 0;
int ctr;

void setup()
{
 pinMode(pin, INPUT);
 Serial.begin(9600);
 lcd.begin(16, 2);
}

void loop()
{
 
for (ctr = 0; ctr <= 4; ctr++) // Perform 4 measurements then reset
  {
  // 1st line calculates the phase angle in degrees from differentiated time pulse
  // Function COS uses radians not Degree's hence conversion made by dividing angle / 57.2958
   angle = ((((pulseIn(pin, HIGH)) * nano)* degree)* frequency);
  // pf = cos(angle / rads);
  
   if (angle > angle_max) // Test if the angle is maximum angle
     {
      angle_max = angle; // If maximum record in variable "angle_max"
      pf_max = cos(angle_max / rads); // Calc PF from "angle_max"
     }
   }
   if (angle_max > 360) // If the calculation is higher than 360 do following...
   {
    angle_max = 0; // assign the 0 to "angle_max"
    pf_max = 1; // Assign the Unity PF to "pf_max"
   }
   if (angle_max == 0) // If the calculation is higher than 360 do following...
   {
    angle_max = 0; // assign the 0 to "angle_max"
    pf_max = 1; // Assign the Unity PF to "pf_max"
   }
   Serial.print(angle_max, 2); // Print the result
   Serial.print(",");
   Serial.println(pf_max, 2);
  lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("PF=");
   lcd.setCursor(4,0);
   lcd.print(pf_max);
   lcd.print(" ");
   lcd.setCursor(0,1);
   lcd.print("Ph-Shift=");
   lcd.setCursor(10,1);
   lcd.print(angle_max);
   lcd.print(" ");
  
   //delay(500);
   angle = 0; // Reset variables for next test
   angle_max = 0;
}

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