Smart Energy Meter With Theft Protection Using Arduino
Smart Energy Meter With Theft Protection Using Arduino
Supervised By:
Coordinator:
Engr: Muhammad Umer Asim
The project “Smart Energy Meter with Theft Protection using Arduino” presented by:
Under the supervision of their project advisor and approved by the project Examination
committee, has been accepted by the Institute of Southern Punjab Multan, in partial fulfillment
of the requirements for the Degree of B-Tech (Hons) Electrical Technology.
__________________ ________________
ACKNOWLEDGEMENTS:
CONTENTS
Chapter: 01
Introduction------------------------------------------------------------------------------04
1.1 CURRENT SCENARIO-----------------------------------------------------------04
1.2 COMPARISON OF TECHNOLOGIES---------------------------------------04
1.3 Aim of project------------------------------------------------------------------ -05
1.4.1Electronic Energy Meter------------------------------------------------------05
1.4.2Media Interface Device (In-Station)-----------------------------------------05
1.4.3 Data Communication Media--------------------------------------------------05
1.4.5 Host PC with RMR Software--------------------------------------------------05
1.4.6 Communication Media---------------------------------------------------------05
Chapter: 02
SYSTEM MODEL------------------------------------------------------------------- 06
2.1 Basic Model Of The System----------------------------------------------------06
2.2 Parts Of The System-------------------------------------------------------------07
2.2.1 Sensors (Data Acquisition System)------------------------------------------07
2.2.3 Actuators---------------------------------------------------------------------------07
2.2.4 Display Unit----------------------------------------------------------------------- 07
2.2.9 Current Transformer (CT)----------------------------------------------------07
2.3 Steps Followed In Designing The System------------------------------------07
Electric Buzzer----------------------------------------------------------------------08
Chapter: 03
Chapter: 04
3
Final Project Thesis
CHAPTER #1
INTRODUCTION
4
Final Project Thesis
1.4.6Communication Media:
The communication media is chosen afteranalysis of its:
Cost
Availability
Reliability
Immunity to environmental/ manmade Disturbances
Secrecy and ease of interface
5
Final Project Thesis
CHAPTER# 2
SYSTEM MODEL
2.1 BASIC MODEL OF THE SYSTEM:
The main unit in this model is pic 18F452 microcontroller. Other components are connected to pic
microcontroller .LCD 20*4 are used for display of current, voltage and electricity units.Max232 is
used for computer interfacing .Real time clock(RTC) is used for counting seconds,
minutes,hours,months and leap years. Relay is used for switch on /off electricity delivery. Current
transformer (CT) and potential transformer (PT) is used for current measurement voltage
measurement respectively as shown in fig.
6
Final Project Thesis
2.2.3 ACTUATORS:
An array of actuators can be used in the system such as relays, contactors, and
Change over switches etc. They are change the electrical power to specific approved load.
to be followed. The simplest strategy is to use threshold sensors that directly affect
Actuation of relay. For example, the exceed in the load current above the maximum limit, the
controller automatically trip the electricity.
More complex control strategies are those based not only on the current values of
the controlled variables, but also on the previous history of the system, including the rates at
which the system variables are changing.
Electric Buzzer:
A 12V DC electric buzzer used for beep or indication.
8
Final Project Thesis
CHAPTER #3
HARDWARE DESCRIPTION
3.1. SENSORS (Data acquisition system):
A sensor(also called detector) is a converter that measures a physical quantityand converts it
into a signal which can be read by an observer or by an (today mostly electronic instrument.
A sensor is a device which receives and responds to a signal. A sensor's sensitivity indicates
how much the sensor's output changes when the measured quantity changes.
The sensors used in this system are:
1.Current Transformer.
2.Potential Transform
There are a variety of applications for current transformers. Some devices are used to
measure current in electronics equipment or motors. Others are used in street lighting.
Current transformers with small footprints mount on printed circuit boards (PCBs) and are
used to sense current overloads, detect ground faults, and isolate current feedback signals.
Larger devices are used in many three-phase systems to measure current or voltage.
Commercial class current transformers that monitor low-power currents are also available.
Some current transformers are weatherproof or are rated for outdoor use. Others meet MIL-
SPEC, ANSI C-12, or IEC 1036 standards. Generally, ANSI class devices are intended for
power monitoring applications where high accuracy and minimum phase angle are required.
9
Final Project Thesis
Current Transformer Basics can be stated that power flow and provide electrical inputs to
power transformers and instruments. Current transformers produce either an alternating
current or alternating voltage that is proportional to the measured current.
There are two basic types of current transformers: wound and toroid. Wound current
transformers consist of an integral primary winding that is inserted in series with the
conductor that carries the measured current. Toroid or donut-shaped currenttransformers do
not contain a primary winding. Instead, the wire that carries the current is threaded through a
window in the toroid transformer.
Current Transformer Basics dictate that many performance specifications, including primary
current, secondary current, insulation voltage, accuracy, and burden. Primary current, the
load of the current transformer, is the measured current. Secondary current is the range of
current outputs. Insulation voltage represents the maximum insulation that current
transformers provide when connected to a power source. Accuracy is the degree of certainty
with which the measured current agrees with the ideal value. Burden is the maximum load
that devices can support while operating within their accuracy ratings. Typically, burden is
expressed in volt-amperes (VA), the product of the voltage applied to a circuit and the
current.[4]
3.3.Potential Transformer:
Potential Transformer is designed for monitoring single-phase and three-phase power line
voltages in power metering applications.The primary terminals can be connected either in
line-to-line or in line-to-neutral configuration. Fused transformer models are designated by a
suffix of "F" for one fuse or "FF" for two fuses.
A Potential Transformer is a special type of transformer that allows meters to take readings
from electrical service connections with higher voltage (potential) than the
10
Final Project Thesis
`
Fig 3.2Pin diagram of 2x16 line LCD
12
Final Project Thesis
A real time clock is basically just like a watch - it runs on a battery and keeps time for you
even when there is a power outage! Using an RTC, you can keep track of long timelines,
even if you reprogram your microcontroller or disconnect it from USB or a power plug.
Most microcontrollers, including the Adriano , have a built-in timekeeper called mills() and
there are also timers built into the chip that can keep track of longer time periods like minutes
or days. So why would you want to have a separate RTC chip? Well, the biggest reason is
that mills() only keeps track of time since the Adriano was last powered - . That means that
when the power is turned on, the millisecond timer is set back to 0. The Adriano doesn't
know that it's 'Tuesday' or 'March 8th', all it can tell is 'It's been 14,000 milliseconds since I
was last turned on'.
OK so what if you wanted to set the time on the Adriano? You'd have to program in the date
and time and you could have it count from that point on. But if it lost power, you'd have to
reset the time. Much like very cheap alarm clocks: every time they lose power they
blink 12:00
While this sort of basic timekeeping is OK for some projects, some projects such as data-
loggers, clocks, etc. will need to have consistent timekeeping that doesn't reset when the
Adriano battery dies or is reprogrammed. Thus, we include a separate RTC! The RTC
chip is a specialized chip that just keeps track of time. It can count leap-years and knows how
many days are in a month, but it doesn't take care of Daylight Savings Time (because it
changes from place to place).[16]
3.8. RELAYS:
A relay is an electrical switch that opens and closes under the control of another
Electrical circuit. In the original form, the switch is operated by an electromagnet to open
or close one or many sets of contacts. Joseph Henry invented it in 1835. Because
a relay is able to control an output circuit of higher power than the input circuit, it can be
Considered to be, in a broad sense, a form of an electrical amplifier.
`
Despite the speed of technological developments, some products prove so
Popular that their key parameters and design features remain virtually unchanged for
Years. One such product is the „sugar cube‟ relay, shown in the figure above, which
has proved useful to many designers who needed to switch up to 10A, whilst using
Relatively little PCB area
Since relays are switches, the terminology applied to switches is also applied
13
Final Project Thesis
to relays. A relay will switch one or more poles, each of whose contacts can be thrown by
Energizing the coil in one of three ways:
1. Normally - open (NO) contacts connect the circuit when the relay is activate d; the
circuit is disconnected when the relay is inactive. It is also called a FORM A contact or
“make” contact.
2. Normally - closed (NC) contacts disconnect the circuit when the relay is activated; the
circuit is connected when relay is inactive. It is also called FORM B contact or”
break” contact.
• DPST - Double Pole Single Throw: These have two pairs of terminals. Equivalent
to two SPST switches or relays actuated by a single coil. Including two for the coil, such
a relay has six terminals in total. It is ambiguous whether the poles are normally open,
normally closed, or one of each.
• DPDT - Double Pole Double Throw: These have two rows of change-over
terminals. Equivalent to two SPDT switches or relays actuated by a single coil. Such a
relay has eight terminals, including the coil.
• QPDT - Quadruple Pole Double Throw: Often referred to as Quad Pole Double
Throw, or 4PDT. These have four rows of change-over terminals. Equivalent to four
SPDT switches or relays actuated by a single coil or two DPDT relays. In total, fourteen
terminals including the coil.
The Relay interfacing circuitry used in the application is:
14
Final Project Thesis
The power supply section consists of step down transformers of 230V primary to
9V and 12V secondary voltages for the +5V and +12V power supplies respectively. The
stepped down voltage is then rectified by 4 1N4007 diodes. The high value of capacitor
1000 μF charges at a slow rate as the time constant is low, and once the capacitor charges
there is no resistor for capacitor to discharge. This gives a constant value of DC. IC 7805
is used for regulated supply of +5 volts and IC 7812 is used to provide a regulated supply
of +12 volts in order to prevent the circuit ahead from any fluctuations. The filter
capacitors connected after this IC filters the high frequency spikes. These capacitors
are connected in parallel with supply and common so that spikes filter to the common.
These give stability to the power supply circuit.
As can be seen from the above circuit diagrams, the rectified voltage from the
4 diodes is given to pin 1 of the respective regulators. Pin 2 of the regulators is connected
to ground and pin 3 to Vcc. With adequate heat sinking the regulator can deliver 1A
output current. If internal power dissipation becomes too high for the heat sinking
provided, the thermal shutdown circuit takes over preventing the IC from overheating.
15
Final Project Thesis
PIN CONFIGURATION:
Pin diagram of Arduino Mega 2560
3.10. Arduino Mega 2560
16
Final Project Thesis
17
Final Project Thesis
either from VIN via an on-board regulator, or be supplied by USB or another regulated
5V supply.
3V3. A 3.3 volt supply generated by the on-board regulator. Maximum current draw is
50 mA.
GND. Ground pins.
Memory
The ATmega2560 has 256 KB of flash memory for storing code (of which 8 KB is used
for the bootloader), 8 KB of SRAM
and 4 KB of EEPROM (which can be read and written with the EEPROM library).
Input and Output
Each of the 54 digital pins on the Mega can be used as an input or output, using
pinMode(), digitalWrite(), and
digitalRead() functions. They operate at 5 volts. Each pin can provide or receive a
maximum of 40 mA and has an internal
pull-up resistor (disconnected by default) of 20-50 kOhms. In addition, some pins have
specialized functions:
Serial: 0 (RX) and 1 (TX); Serial 1: 19 (RX) and 18 (TX); Serial 2: 17 (RX) and 16 (TX);
Serial 3: 15
(RX) and 14 (TX). Used to receive (RX) and transmit (TX) TTL serial data. Pins 0 and 1
are also connected to the
corresponding pins of the ATmega8U2 USB-to-TTL Serial chip.
External Interrupts: 2 (interrupt 0), 3 (interrupt 1), 18 (interrupt 5), 19 (interrupt 4), 20
(interrupt
3), and 21 (interrupt 2). These pins can be configured to trigger an interrupt on a low
value, a rising or falling edge,
or a change in value. See the attachInterrupt() function for details.
PWM: 0 to 13. Provide 8-bit PWM output with the analogWrite() function.
SPI: 50 (MISO), 51 (MOSI), 52 (SCK), 53 (SS). These pins support SPI communication
using the SPI library.
The SPI pins are also broken out on the ICSP header, which is physically compatible
with the Uno, Duemilanove and
Diecimila.
LED: 13. There is a built-in LED connected to digital pin 13. When the pin is HIGH
value, the LED is on, when the
pin is LOW, it's off.
I2C: 20 (SDA) and 21 (SCL). Support I2C (TWI) communication using the Wire library
(documentation on the
Wiring website). Note that these pins are not in the same location as the I2C pins on the
Duemilanove or Diecimila.
The Mega2560 has 16 analog inputs, each of which provide 10 bits of resolution (i.e.
1024 different values). By default they
measure from ground to 5 volts, though is it possible to change the upper end of their
range using the AREF pin and
analogReference() function.There are a couple of other pins on the board:
AREF.Reference voltage for the analog inputs. Used with analogReference().
Reset. Bring this line LOW to reset the microcontroller. Typically used to add a reset
button to shields which block
the one on the board.
Communication
The Arduino Mega2560 has a number of facilities for communicating with a computer,
another Arduino, or other
18
Final Project Thesis
microcontrollers. The ATmega2560 provides four hardware UARTs for TTL (5V) serial
communication. An ATmega8U2
on the board channels one of these over USB and provides a virtual com port to
software on the computer (Windows
machines will need a .inf file, but OSX and Linux machines will recognize the board as
a COM port automatically. The
Arduino software includes a serial monitor which allows simple textual data to be sent
to and from the board. The RX and
TX LEDs on the board will flash when data is being transmitted via the ATmega8U2
chip and USB connection to the
computer (but not for serial communication on pins 0 and 1).
A SoftwareSerial library allows for serial communication on any of the Mega2560's
digital pins.
The ATmega2560 also supports I2C (TWI) and SPI communication. The Arduino
software includes a Wire library to
simplify use of the I2C bus; see the documentation on the Wiring website for details.
For SPI communication, use the SPI
library.
Programming
The Arduino Mega can be programmed with the Arduino software (download). For
details, see the reference and tutorials.
The ATmega2560 on the Arduino Mega comes preburned with a bootloader that allows
you to upload new code to it
without the use of an external hardware programmer. It communicates using the
original STK500 protocol (reference, C
header files).
You can also bypass the bootloader and program the microcontroller through the ICSP
(In-Circuit Serial Programming)
header; see these instructions for details.
The ATmega8U2 firmware source code is available in the Arduino repository. The
ATmega8U2 is loaded with a DFU
bootloader, which can be activated by connecting the solder jumper on the back of the
board (near the map of Italy) and
then resetting the 8U2. You can then use Atmel's FLIP software (Windows) or the DFU
programmer (Mac OS X and
Linux) to load a new firmware. Or you can use the ISP header with an external
programmer (overwriting the DFU
bootloader). See this user-contributed tutorial for more information.
Automatic (Software) Reset
Rather then requiring a physical press of the reset button before an upload, the
Arduino Mega2560 is designed in a way
that allows it to be reset by software running on a connected computer. One of the
hardware flow control lines (DTR) of
the ATmega8U2 is connected to the reset line of the ATmega2560 via a 100 nanofarad
capacitor. When this line is
asserted (taken low), the reset line drops long enough to reset the chip. The Arduino
software uses this capability to allow
you to upload code by simply pressing the upload button in the Arduino environment.
This means that the bootloadercanhave a shorter timeout, as the lowering of DTR can
be well-coordinated with the start of the upload.
19
Final Project Thesis
This setup has other implications. When the Mega2560 is connected to either a
computer running Mac OS X or Linux, it
resets each time a connection is made to it from software (via USB). For the following
half-second or so, the bootloader is
running on the Mega2560. While it is programmed to ignore malformed data (i.e.
anything besides an upload of new
code), it will intercept the first few bytes of data sent to the board after a connection is
opened. If a sketch running on the
board receives one-time configuration or other data when it first starts, make sure that
the software with which it
communicates waits a second after opening the connection and before sending this data.
The Mega2560 contains a trace that can be cut to disable the auto-reset. The pads on
either side of the trace can be
soldered together to re-enable it. It's labeled "RESET-EN". You may also be able to
disable the auto-reset by connecting a
110 ohm resistor from 5V to the reset line; see this forum thread for details.
USB Overcurrent Protection
The Arduino Mega2560 has a resettable polyfuse that protects your computer's USB
ports from shorts and overcurrent.
Although most computers provide their own internal protection, the fuse provides an
extra layer of protection. If more
than 500 mA is applied to the USB port, the fuse will automatically break the
connection until the short or overload is
removed.
Physical Characteristics and Shield Compatibility
The maximum length and width of the Mega2560 PCB are 4 and 2.1 inches respectively,
with the USB connector and
power jack extending beyond the former dimension. Three screw holes allow the board
to be attached to a surface or case.
Note that the distance between digital pins 7 and 8 is 160 mil (0.16"), not an even
multiple of the 100 mil spacing of the
other pins.
The Mega2560 is designed to be compatible with most shields designed for the Uno,
Diecimila or Duemilanove. Digital
pins 0 to 13 (and the adjacent AREF and GND pins), analog inputs 0 to 5, the power
header, and ICSP header are all in
equivalent locations. Further the main UART (serial port) is located on the same pins (0
and 1), as are external interrupts
0 and 1 (pins 2 and 3 respectively). SPI is available through the ICSP header on both
the Mega2560 and Duemilanove /
Diecimila. Please note that I2C is not located on the same pins on the Mega (20 and 21)
as the Duemilanove / Diecimila
(analog inputs 4 and 5).ICSP
20
Final Project Thesis
+5V
GND
+5V
GND
GND
+5V
GND GND
47u 47u
GND GND
GND
GND
GREEN
GND
+5V
M7
GND
21
Final Project Thesis
CHAPTER#04
For interfacing with computer connect Max458 to microcontroller pin 25 and pin 26 which
are for transmit and Receivedata respectively to and from microcontroller. Basically Max 458
is used for handshaking with computer in order to transmit data tocomputer (control
room).capacitors connected with Max458 are used as doublers/Tripler. Real time clock
(RTC) is used to get time. It‟s out put pin5 and pin 8 is connected to microcontroller‟s pin 27
and pin 28.Crystal oscillator is connected in order to give it clock frequency.LCD 2*16 is
used for digital display of our data(voltage , current, electricity units etc).A transistor is used
for switch on and off to relay is connected to microcontroller pin 34.The PCB design for this
circuit is shown below
22
Final Project Thesis
capacitors connected after this IC filters the high frequency spikes. These capacitors
are connected in parallel with supply and common so that spikes filter to the common.
These give stability to the power supply circuit.
23
Final Project Thesis
As can be seen from the above circuit diagram, the rectified voltage from the
4 diodes is given to pin 1 of the respective regulators. Pin 2 of the regulators is connected
to ground and pin 3 to Vcc. With adequate heat sinking the regulator can deliver 1A
output current. If internal power dissipation becomes too high for the heat sinking
provided, the thermal shutdown circuit takes over preventing the IC from
overheating.[10]
PCB design of above circuit diagram is given in next page.
4.3.Relay Circuitry:
Relay circuit is used as control module for the approved load .In these meters we made the
approved load as 1.2kv.when the user crosses this load the microcontroller gives a trigger
pulse to relay to cut off the user‟s load and check after 6 seconds, either
The user has decreased the load if he did load will continue, if not relay will again cut off the
load. This task will continue infinitely.
Conclusion
We designed smart energy meter, which can display real time power consumption. It
manages power load in peak hours. Smart energy metering system automatically distributes
power to all consumers according to a predefined schedule. Along with, this system provides
automatic billing and instant SMS facility as well.
REFERENCES
1. Programming of PIC Micro-Controller for Power Factor Correction. First Asia
InternationalConference on Modeling&Simulation..Barsoum N (2007).
2. Design and Implementation ofMulti meter Based On Microcontroller. IATS‟09, Karabük,
Turkey.pp.19-25.Bayhan S, Demirba__ (2009).
3. Designing of a Reactive Power RelayBased on a PIC. GaziUniversity.J. Arch. Eng.
Faculty.Basciftci and Hatay 2317Bayındır R, Kaplan O (2007).
4. Reactive Power Compensation in High Current Facilities. Birsen Publishing,
Istanbul.Bayram M (2000).
5. Measurement of Power Factor Using a Microcontroller.Erciyes University, Journal of
Graduate School ofNatural and Applied Sciences.Çolak ,Bayındır R (2003).
6.Measurement and Compensation in with Harmonicand Unbalanced Network.Master thesis,
Graduate School of Naturaland Applied Sciences, Sakarya.Demirkol Ö (2006).
7. Feed forward and Elman FeedbackHarmonics Compensation Using Artificial Neural
Networks. PhD thesis, Graduate School of Natural and Applied Sciences,
Sakarya.EpdkGüntürkünR (2003).
8. http://www.contractelectronica.ru/files/124/CSNP661.pdf Access
9.Development of Power Factor Controller using PIC Microcontroller. Master thesis
department of electrical and instrumentation engineering, Thapar University, Patiala. Kumar
P (2008).
25
Final Project Thesis
Appendix A (Coding)
1.Main code for ARDUINO MEGA 2560
#include <EEPROM.h>
#include <LiquidCrystal.h>// call for lcd function
#include <SoftwareSerial.h> // call for serial port monitor
#define CURRENT_SENSOR1 A1 // Analog input pin that sensor is attached to
#define CURRENT_SENSOR2 A2 // Analog input pin that sensor is attached to
#define VOLTS_SENSOR1 A0 // Analog input pin that sensor is attached to
constint Led1 = 49; // define led 1 pin
constint Led2 = 47; //
constint Led3 = 45; //
constint Led4 = 43; //
constint Led5 = 41; //
constint Led6 = 39; //
constint Relay1 = 7; //
constint Relay2 = 6; //
constint Relay3 = 5; //
int i;
unsignedint Ctvalue1,Ctvalue2,Volts,Timecount,Ctvalue3,timeval;
int adcval1,adcval2;
unsigned long Units;
booleanbuttonflag = false;
booleanmetalflag = false;
booleanctflag = false;
booleanrainflag = false;
boolean volt1flag = false;
boolean volt2flag = false;
booleantheftflag = false;
//=========================
void setup()
{
Serial.begin(9600); // serial port bit per secnd speed
26
Final Project Thesis
pinMode(CURRENT_SENSOR1, INPUT);
pinMode(CURRENT_SENSOR2, INPUT);
pinMode(VOLTS_SENSOR1, INPUT);
pinMode (Buzzer,OUTPUT);
pinMode (Led1,OUTPUT); //
pinMode (Led2,OUTPUT); // p
pinMode (Led3,OUTPUT); ///
pinMode (Led4,OUTPUT); ///
pinMode (Relay1,OUTPUT); //
pinMode (Relay2,OUTPUT); //
pinMode (Relay3,OUTPUT); //
digitalWrite(Relay1,LOW);
digitalWrite(Relay2,LOW);
digitalWrite(Relay3,LOW);
lcd.setCursor(0,0);
lcd.print("SMART ENERGY METER");
lcd.setCursor(0,1);
lcd.print("& THEFT PROTECTION");
for(int i=0;i<2;i++)
{
digitalWrite(Led1,HIGH);
digitalWrite(Led1,HIGH);
digitalWrite(Led3,HIGH);
digitalWrite(Led4,HIGH);
digitalWrite(Led5,HIGH);
digitalWrite(Led6,HIGH);
digitalWrite(Buzzer,HIGH);
delay(500);
digitalWrite(Led1,LOW);
digitalWrite(Led1,LOW);
digitalWrite(Led3,LOW);
digitalWrite(Led4,LOW);
digitalWrite(Led5,LOW);
digitalWrite(Led6,LOW);
digitalWrite(Buzzer,LOW);
delay(500);
27
Final Project Thesis
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SUPERVISED BY:");
lcd.setCursor(0,1);
lcd.print("ENGR. M ZAHID");
for(int i=0;i<2;i++)
{
digitalWrite(Led1,HIGH);
digitalWrite(Led1,HIGH);
digitalWrite(Led3,HIGH);
digitalWrite(Led4,HIGH);
delay(500);
digitalWrite(Led1,LOW);
digitalWrite(Led1,LOW);
digitalWrite(Led3,LOW);
digitalWrite(Led4,LOW);
delay(500);
}
lcd.clear();
lcd.setCursor(5,0);
lcd.print("Design by");
lcd.setCursor(0,1);
lcd.print(" BY ");
for(int i=0;i<2;i++)
{
digitalWrite(Led1,HIGH);
digitalWrite(Led1,HIGH);
digitalWrite(Led3,HIGH);
digitalWrite(Led4,HIGH);
delay(500);
digitalWrite(Led1,LOW);
digitalWrite(Led1,LOW);
digitalWrite(Led3,LOW);
digitalWrite(Led4,LOW);
delay(500);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("M NAUMAN LIAQAT");
lcd.setCursor(0,1);
lcd.print("BTHET-023R16-2");
for(int i=0;i<2;i++)
{
digitalWrite(Led1,HIGH);
digitalWrite(Led1,HIGH);
28
Final Project Thesis
digitalWrite(Led3,HIGH);
digitalWrite(Led4,HIGH);
delay(500);
digitalWrite(Led1,LOW);
digitalWrite(Led1,LOW);
digitalWrite(Led3,LOW);
digitalWrite(Led4,LOW);
delay(500);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("TALAT MEHMOOD ");
lcd.setCursor(0,1);
lcd.print("BTHET-023R16-6");
for(int i=0;i<2;i++)
{
digitalWrite(Led1,HIGH);
digitalWrite(Led1,HIGH);
digitalWrite(Led3,HIGH);
digitalWrite(Led4,HIGH);
delay(500);
digitalWrite(Led1,LOW);
digitalWrite(Led1,LOW);
digitalWrite(Led3,LOW);
digitalWrite(Led4,LOW);
delay(500);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("TAHIR HASSAN ");
lcd.setCursor(0,1);
lcd.print("BTHET-023R16-9");
for(int i=0;i<2;i++)
{
digitalWrite(Led1,HIGH);
digitalWrite(Led1,HIGH);
digitalWrite(Led3,HIGH);
digitalWrite(Led4,HIGH);
delay(500);
digitalWrite(Led1,LOW);
digitalWrite(Led1,LOW);
digitalWrite(Led3,LOW);
digitalWrite(Led4,LOW);
delay(500);
}
lcd.clear();
29
Final Project Thesis
lcd.setCursor(0,0);
lcd.print("MUHAMMAD UMER");
lcd.setCursor(0,1);
lcd.print("BTHET-023R16-7");
for(int i=0;i<2;i++)
{
digitalWrite(Led1,HIGH);
digitalWrite(Led1,HIGH);
digitalWrite(Led3,HIGH);
digitalWrite(Led4,HIGH);
delay(500);
digitalWrite(Led1,LOW);
digitalWrite(Led1,LOW);
digitalWrite(Led3,LOW);
digitalWrite(Led4,LOW);
delay(500);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("TARIQ ZAFAR");
lcd.setCursor(0,1);
lcd.print("BTHET-023R16-5");
for(int i=0;i<2;i++)
{
digitalWrite(Led1,HIGH);
digitalWrite(Led1,HIGH);
digitalWrite(Led3,HIGH);
digitalWrite(Led4,HIGH);
delay(500);
digitalWrite(Led1,LOW);
digitalWrite(Led1,LOW);
digitalWrite(Led3,LOW);
digitalWrite(Led4,LOW);
delay(500);
}
digitalWrite(Buzzer,HIGH);delay(200); digitalWrite(Buzzer,LOW);delay(200);
digitalWrite(Buzzer,HIGH);delay(200); digitalWrite(Buzzer,LOW);delay(200);
digitalWrite(Buzzer,HIGH);delay(200); digitalWrite(Buzzer,LOW);delay(200);
30
Final Project Thesis
Timecount=EEPROMReadlong(10);
Units=EEPROMReadlong(0);
delay(200);
}
//==================================
voidEEPROMWritelong(int address, long value){
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
}
longEEPROMReadlong(long address){
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one
<< 24) & 0xFFFFFFFF);
}
//====================================mian
loop=======================
void loop()
{
31
Final Project Thesis
if(button1flag==1){buttonflag=0;digitalWrite(Led1,LOW);}
else {buttonflag=1;digitalWrite(Led1,HIGH);}
if(button2flag==1){metalflag=1;digitalWrite(Led3,HIGH);}
else {metalflag=0;digitalWrite(Led3,LOW);}
lcd.setCursor(11,1);
lcd.print(buttonflag);
lcd.setCursor(12,1);
lcd.print(metalflag);
lcd.setCursor(13,1);
lcd.print(ctflag);
lcd.setCursor(15,1);
lcd.print(theftflag);
Volts = analogRead (VOLTS_SENSOR1); /* Tells the Arduino to read the pin and stores
the value in “val2” */
//Volts=Volts/1.46;
lcd.setCursor(0,0);
lcd.print("V=");
lcd.setCursor(2,0);
lcd.print(" ");
lcd.setCursor(2,0);
lcd.print(Volts);
if(Volts>250)volt1flag=1;
else volt1flag=0;
if(Volts<150)volt2flag=1;
else volt2flag=0;
Ctvalue1 = getMaxValue1();
lcd.setCursor(6,0);
lcd.print("C=");
if(Ctvalue1<30)Ctvalue1=0;
if(Ctvalue1>50)Ctvalue1=Ctvalue1-22;
lcd.setCursor(8,0);
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(Ctvalue1);
Ctvalue2= getMaxValue2();
lcd.setCursor(11,0);
lcd.print("C=");
if(Ctvalue2<30)Ctvalue2=0;
32
Final Project Thesis
lcd.setCursor(13,0);
lcd.print(" ");
lcd.setCursor(13,0);
lcd.print(Ctvalue2);
Ctvalue3=Ctvalue2+50;
if(Ctvalue1>Ctvalue3){ctflag=1;digitalWrite(Led4,HIGH);}
else {ctflag=0;digitalWrite(Led4,LOW);}
//=================================================================
lcd.setCursor(0,1);
lcd.print("U=");
lcd.setCursor(2,1);
lcd.print(" ");
lcd.setCursor(2,1);
lcd.print(Units);
if(Ctvalue1>50)
{
lcd.setCursor(8,1);
lcd.print("*");
Timecount++;
if(Timecount>98)
{
Units++;
EEPROMWritelong(0,Units);delay(500);
EEPROMWritelong(10,Timecount);delay(500);
Timecount=0;
}
}
else
{
lcd.setCursor(8,1);
lcd.print(" ");
}
lcd.setCursor(9,1);
lcd.print(" ");
lcd.setCursor(9,1);
lcd.print(Timecount);
if(volt1flag==1 || volt2flag==1)
{
digitalWrite(Relay1,HIGH);
digitalWrite(Relay2,HIGH);
}
else
{
digitalWrite(Relay1,LOW);
33
Final Project Thesis
digitalWrite(Relay2,LOW);
}
if(theftflag==1 &&timeval>2)
{
digitalWrite(Relay3,HIGH);
digitalWrite(Buzzer,HIGH);
}
else
{
digitalWrite(Relay3,LOW);
digitalWrite(Buzzer,LOW);
}
//=================================================================
=
int getMaxValue1()
{
intsensorValue; //value read from the sensor
intsensorMax = 0;
uint32_t start_time = millis();
while((millis()-start_time) < 1000)//sample for 1000ms
{
sensorValue = analogRead(CURRENT_SENSOR1);
if (sensorValue>sensorMax)
{
/*record the maximum sensor value*/
sensorMax = sensorValue;
}
}
returnsensorMax;
}
int getMaxValue2()
{
intsensorValue; //value read from the sensor
intsensorMax = 0;
uint32_t start_time = millis();
while((millis()-start_time) < 1000)//sample for 1000ms
{
sensorValue = analogRead(CURRENT_SENSOR2);
34
Final Project Thesis
if (sensorValue>sensorMax)
{
/*record the maximum sensor value*/
sensorMax = sensorValue;
}
}
returnsensorMax;
}
35