Professional Documents
Culture Documents
Energy Meter: Group 8
Energy Meter: Group 8
ENERGY METER
GROUP 8
Abstract.
This report describes the approach and processes used to develop a final design of a system capable of
measuring real power from the mains. A description of communication between the MCP3909 and
microcontroller is implemented in the report using the method of serial peripheral interface (SPI).The
MCP3909 will be transmitting values of voltage and current to the microcontroller in the form of a 32
bit number and using these values of current and voltage we are able to calculate the power and this is
displayed both on the computer and LCD. The report contains hardware schematics, simulation results
and software.
2
Electrical Design 4
Energy Meter
Contents
Abstract.................................................................................................................................................ii
1. Introduction...................................................................................................................................1
2. Project Description........................................................................................................................2
2.1. Overview...............................................................................................................................2
2.2. Features..................................................................................................................................2
3. Hardware Design...........................................................................................................................3
3.1. PCB – POWER SUPPLY......................................................................................................3
3.2. PCB Design...........................................................................................................................4
4. Software Design............................................................................................................................6
4.1. Interface 3: LCD display configuration and circuitry.............................................................6
Configuring the LCD Display:.......................................................................................................6
Data Transmission.........................................................................................................................7
4.2. Interface 2..............................................................................................................................7
Serial Peripheral Interface (SPI) - Data Transmission...................................................................7
4.3. Interface 4: Communication between Microcontroller and Computer...................................9
RS-232 Communication:...............................................................................................................9
Level conversion:..........................................................................................................................9
Data transmission via RS-232 port:.............................................................................................10
4.4. Choosing a Microcontroller.................................................................................................11
5. Microcontroller pseudo Code:.....................................................................................................12
Program Overview.......................................................................................................................15
Visual basic Software design.........................................................................................................16
6. Testing & Results........................................................................................................................22
7.1. Full schematic & setup.................................................................................................................22
7. Management................................................................................................................................25
Work distribution between group members:....................................................................................25
Current Status..................................................................................................................................25
Timeline..........................................................................................................................................25
8. Conclusion...................................................................................................................................26
References...........................................................................................................................................27
Appendix............................................................................................................................................A1
3
Electrical Design 4
Energy Meter
1. Introduction
A final design of an energy meter is presented in this report. It was required to create a system
interface to a computer on one side, and mains on the other. The circuit is capable of measuring
power; this is done by multiplying the measured values of voltage and current. The power is to be
displayed on an LCD panel, we used the LMB162ABC which is manufactured by - topward - 2 line
16 character. We had the option of transmitting data to the computer via RS-232 or USB, we chose
the RS-232 as we were more familiar with it and it is inexpensive, and it only requires four capacitors
for its connection. The circuit is simple and consists of the MCP3909 measurement unit; we also use a
transformer to step down the high voltages from the mains to a suitable voltage that the MCP3909
could handle. We not only needed to display values of power on the computer but had to create an
application on the computer that reports and logs power reading.
4
Electrical Design 4
Energy Meter
2. Project Description
2.1. Overview
The purpose of this project is to measure power from the mains 230Vrms, 50Hz supply. The
dangerously high mains supply is stepped down appropriately using a transformer in order to make
use of the measuring IC, the MCP3909. Power is obtained through the MCP3909 taking in analogue
input signals for both voltage and current measurements. The voltage and power values are now sent
to the microcontroller via SPI. It is here in the microcontroller were the power value is determined
(voltage*current*gain). From the microcontroller we display the power value on an LCD via GPIO.
The power value is sent to the PC through serial communications. This makes use of the RS232 cable
to send data serially to the computer. A VB interface is used to display the value on the PC.
2.2. Features
Power supplied via the mains(230 stepped down to 5V)
LCD screen user interface – LCD screen used to display the power
Hardware circuitry
IC- MCP3909 (energy meter)
PC interface
Interface 4
Interface 1 Interface 2
Interface 3
Power
LCD
Supply
(5V)
5
Electrical Design 4
Energy Meter
3. Hardware Design
3.1. PCB – POWER SUPPLY
6
Electrical Design 4
Energy Meter
7
Electrical Design 4
Energy Meter
8
Electrical Design 4
Energy Meter
Two additional connections were necessary to make the PCB work. Pin 9(Master
Clear pin) of the MCP3909 is connected to Port B pin 3 of the Atmega 16. This is
used in order to enable the SPI for communication. Also, Pin 14 (Chip Select) of the
MCP3909 is connected to ground as this pin needs to be a low when serial
communication takes place.
4. Software Design
4.1. Interface 3: LCD display configuration and circuitry
9
Electrical Design 4
Energy Meter
Figure 15 shows the necessary connections for the LCD module. Pins 1 and 2 are the power supply
pins. The R/W pin (Pin 5) requires a LOW signal for writing to the LCD display and a HIGH signal to
read from it. Pin 4 is the register select pin used to select whether display data or instruction data is
being sent and pin 6 is the enable pin. Pins 7 to 14 are the data pins with pin 14 being the most
significant bit.
Data Transmission
All data transmitted to the LCD needs to be characters for transmission to work. Using the predefined
function on C,” lcd_putf()”, this displays the value on the LCD display. Since we are using 4 bit
mode, the first 4 bits from the MSB side (i.e. bit 7 to4) will first be transmitted then the next 4.
4.2. Interface 2
Shift Direction
MSB LSB
MOSI 1 0 0 1 1 0 1 1 MISO
SCK CS
10
Electrical Design 4
Energy Meter
SO SI
0 1 0 0 1 1 0 0
MSB LSB
Shift Direction
SLAVE
The SPI master and slave interconnection diagram shown above explains how the shift register is used
to transfer and receive the data.The master transfers0b10011011 (0×9B) to the slave and at the same
time the slave device also want to transfer the 0b01001100 (0×4C) data to the master.By activating
the CS (chip select) pin on the slave device, now the slave is ready to receive the data.On the first
clock cycle both master and slave shift register will shift their registers content one bit to the left, the
SPI slave will receive the first bit from the master on its LSB register while at the same time the SPI
master will receive its first data from slave on its LSB register.
Master Slave
MSB LSB MSB LSB
1 0 0 1 1 0 1 1 0 1 0 0 1 1 0 0
1st Clock 0 0 1 1 0 1 1 0 1 0 0 1 1 0 0 1
2nd Clock 0 1 1 0 1 1 0 1 0 0 1 1 0 0 1 0
3rd Clock 1 1 0 1 1 0 1 0 0 1 1 0 0 1 0 0
4th Clock 1 0 1 1 0 1 0 0 1 1 0 0 1 0 0 1
5th Clock 0 1 1 0 1 0 0 1 1 0 0 1 0 0 1 1
11
Electrical Design 4
Energy Meter
6th Clock 1 1 0 1 0 0 1 1 0 0 1 0 0 1 1 0
7th Clock 1 0 1 0 0 1 1 0 0 1 0 0 1 1 0 1
8th Clock 0 1 0 0 1 1 0 0 1 0 0 1 1 0 1 1
Using the same principal for each bit, the complete data transfer between master and slave will be
done in 8 clock cycles. Once the complete transfer is done, the contents of the shift register are sent to
the buffer register. Data can now be read from the buffer register. It is very important to read the
incoming data as it will be lost in the next transfer of data. For the purpose of this design, only data
transfer from the master to slave (MCP3909 to MCU) will take place as we only need the current and
voltage values from the MCP3909.
RS-232 Communication:
The RS-232 is an asynchronous serial communications port. Data is transmitted by the RS-232 port,
one bit at a time (serially transmitted). The RS-232 contains two data lines, the TX and RX lines. The
TX is a wire which allows the transmission of data to the next device, and the RX is the wire which
receives the data from the other device. For the purpose of the design, we will only be using the TX
pin, as data will only be sent to the computer.
The logic levels of the RS-232 port is defined as follows, HIGH = -12V and LOW = 12V, while on a
microcontroller the logic levels are defined as HIGH = 5V and LOW = 0V. Therefore a level
converter is required to interface the computer to the microcontroller to obtain the correct logic level.
12
Electrical Design 4
Energy Meter
Level conversion:
To obtain the desired voltage levels (+5V high, 0V low), the MAX232 chip is used. This device
connects to the USART terminals of the microcontroller and directly to the RS232 port. The MAX232
requires 4 capacitors to ensure that it operates correctly.
Fig 12: Pin connection of MAX 232 interfaced with both the Microcontroller and DSUB9F, connector for the RS-232
cable.
In idle mode the RX pin of the RS-232 port remains low, to start transmitting data a low (12 V for
RS-232) start bit is sent on the pin the PC detects this change in state, to test if this is a start of data
transmission the PC checks the state of the pin at half the time it takes to transfer one bit, if the state is
still low, then the PC consider it as a start bit and does not neglect it as noise. Thereafter the first bit is
transmitted, after all bits have a parity bit is sent (if parity is in use), followed by one or two stop bits
(stop bits denoted by a high state, -12 V for RS-232).
13
Electrical Design 4
Energy Meter
Figure 13: Example of Data Transmission (8 bits, no parity, and 1 stop bit)
Register Select
LCD Display Read/ write
SCK Inputs (SPI)
D4
SD0 D5
D6
D7
Microcontroller
14
Electrical Design 4
Energy Meter
From this diagram it can be seen that 10 I/O pins are required. Other microcontroller features required
by this design are shown in the table below.
Feature Requirement
Timer Yes
ADC No
SPI Yes
USART Yes
Memory No
It was therefore decided that a 32 pin microcontroller would be sufficient for this project. The
ATmega 16 or the ATmega32can be used for this project. We will be using the ATmega 16. It was
chosen because it can accommodate all the inputs and outputs required and it is fairly cheap compared
to other 32 pin microcontrollers.
Main Loop:
The microcontroller waits for the data from the MCP3909 IC.
If the data received counter equals one then the microcontroller exits the main loop and
breaks to the Calculation subroutine.
Else if the data counter is not equal to one then the microcontroller code continues to loop in
the main program
15
Electrical Design 4
Energy Meter
Exit interrupt
Calculation Subroutine:
The following algorithm is designed to handle values up to 9999, but can be adapted to handle higher
values
Transmit Subroutine:
16
Electrical Design 4
Energy Meter
Clear LCD
Using standard library available in all C compilers, it is possible to convert the values we are
receiving into a character string which can be displayed on an LCD
Display string on LCD, using predefined function “lcd_putsf()”
Visual Basic interface is used to display the value of power, voltage, current and error in measured
value. The resulting values are also stored in a text document for future access. The algorithm for the
Visual Basic Pro is described below.
Main program:
Received Data:
Check Variable select (Variable select is a counter used to specify which variable is received
i.e. Voltage or current.)
If Variable select is equal to one (all values received will be stored in the voltage variable)
o Check Received Counter
o Case1: Received Counter is equal to one, multiply the received data by a thousand
and add it to the voltage variable
o Case2: received counter is equal to two, then multiply the received data by hundred
and add it to the voltage variable
o Case3: received counter is equal to three, then multiply the received data by ten and
add it to the voltage variable
o Case4: received counter is equal to four; add received data to the voltage variable.
o Clear received counter and increment the variable select
Else If variable select equals two
o Check Received Counter
17
Electrical Design 4
Energy Meter
o Case1: Received Counter is equal to one, multiply the received data by a thousand
and add it to the current variable.
o Case2: received counter is equal to two, then multiply the received data by hundred
and add it to the current variable.
o Case3: received counter is equal to three, then multiply the received data by ten and
add it to the current variable.
o Case4: received counter is equal to four; add received data to the current variable.
o Clear received counter and set variable select to one. Set complete counter to one.
Exit subroutine.
Calculate subroutine:
Multiply the current, voltage and gain (value which were used to scale voltage and current
variables) and store result in power variable.
Display voltage, current and power, using text boxes.
Store voltage, current and power in a text file
Return to main loop.
Program Overview
The main program loops continuously until the data is received via the SPI which triggers a,receive
complete interrupt. The data comes in as a 32 bit value in the interrupt service routine the value is
stored in a temporary register. The data received register indicates weather you receive the value. If
the data received is equal to one it will break away from the main program into the calculation
subroutine. In the calculate subroutine we set the higher 16bits of the temp register as voltage and the
lower 16bits as current. The power is calculated by multiplying the current, voltage and gain. Once
current and voltage values have been obtained, we use our ASCII subroutine to convert the values into
equivalent ASCII values. Transmit the resultant current and voltage values via RS232. Display the
calculated power value on the LCD. In order to do this we need to convert the power value into a
string, then only can we display the values using predefined functions” lcd_putsf ()”.
18
Electrical Design 4
Energy Meter
After the data is sent the visual basic program stores the received voltage and current values as
described in procedures above. The power value is calculated. Display the voltage, current and power
values using the graphical user interface shown below, and also stores these values in a text file.
The basic software functions that are required from Visual Basic can be seen in figure16. Once the
user opens the form on visual basic, the software starts working automatically. This means that the
serial port is opened and then waits until data is received. When data is received, the ASCII values are
automatically converted by visual basic. These decimal number and stored in a temporary variables.
These decimal numbers are then added up to form a decimal number of two decimal points which is
then ready to be sent to Microsoft Access. An example of how the numbers are received and
converted is illustrated in the following table.
19
Electrical Design 4
Energy Meter
Firstly, variables needed to be defined as strings, integers or bytes respectively. We then needed to
create a data base in Microsoft Access and then export it visual basic using DataGridView. Two
buttons were created on visual basic to allow the user to perform various functions. An exit button
was created to allow user to exit from interface and a refresh button was used to receive current,
voltage and power values and are displayed with the aid of textboxes. The received values from
serialport1 were ASCII values which were transmitted from the microcontroller and we used the
ReadChar() function to handle this. When programing this button, the serial port 1 was set up, time
and date, using DateTimePicker1.Text as well as logging data into the database created using
Microsoft access.
Error handling:
It was necessary to create an error handler in the textboxes which only allowed numeric values to be
displayed in textboxes, such as “0123456789”.
20
Electrical Design 4
Energy Meter
21
Electrical Design 4
Energy Meter
22
Electrical Design 4
Energy Meter
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
End Sub
End Sub
23
Electrical Design 4
Energy Meter
TextBox4.Text = power
TextBox3.Text = voltage
TextBox2.Text = current
End Sub
End Sub
End Sub
End Sub
24
Electrical Design 4
Energy Meter
Now that all the paper design is complete the actual building and testing of both the hardware and
software designs had to be implemented.
After testing this circuit, there was an error that occurred. At first there was no communication
taking place between the PC and the microcontroller. The first step in debugging was to look at the
device that allows communication to take place. This was the MAX232. After looking at all the
connections for the MAX232 it was found that no errors were present. This left the microcontroller.
After researching it was noted that the microcontroller runs of an internal clock of 1MHz by default if
not changed. This frequency is too low for serial communication to work. Now that the problem was
recognized the solution was to change the internal clock of the microcontroller to 4 MHz by fusing
the bits using CodeVisionAVR C Compiler. After this was done the circuit worked and all that was left
to do was to design a PCB (printed circuit board) for the final prototype. This PCB was designed using
Ultiboard from the Electronics Workbench software package ( as seen in section 3 hardware design).
The actual PCB’s as well as the final system overview is illustrated below :-
25
Electrical Design 4
Energy Meter
26
Electrical Design 4
Energy Meter
The step down transformer steps down the high mains voltage from 230 V AC to 8 V AC. We then
created a power supply using simple analogue circuitry. The 8 V AC going into the supply is
converted to DC voltage using a full wave bridge rectifier & making use of a smoothing capacitor to
give a smooth DC output with very little ripple. The inexpensive Lm7805 voltage regulator is used to
give a constant 5V DC output at 1 Amp.
The 5V DC is sent to the main PCB circuit to power the MCP3909, the Atmega 16 and the LCD. A
function generator is used to provide inputs to pins 5, 6, 7 & 8 of the MCP3909.
27
Electrical Design 4
Energy Meter
7. Management
Current Status
At the time of writing, the following progress has been made:
Final specifications
Complete Design layout
Complete software structure
Timeline
The following figure shows a projected timeline.
28
Electrical Design 4
Energy Meter
8. Conclusion
The aim of this design was to develop an Energy Meter which allowed a user to monitor data from the
mains and also log these values into a data base. This takes out the possibility of human error. The
background theory was properly researched and a time schedule was drawn up to make this design a
success within the time period given. Problems presented themselves during this design but research
and innovative thinking made this project possible. The full paper design of this project was presented
in this report, describing the features and functionality of the energy meter. The hardware schematics
were designed and simulated using Simulink and Ultiboard. The software described in this report is
implemented and simulated and then integrated with the hardware for testing.
29
Electrical Design 4
Energy Meter
References
30
Electrical Design 4
Energy Meter
Appendix
Microcontroller code:
/*****************************************************
http://www.hpinfotech.com
Project :
Version :
Date : 10/25/2010
Company :
Comments:
*****************************************************/
#include <mega16.h>
31
Electrical Design 4
Energy Meter
#include <stdio.h>
#include <stdlib.h>
#include <delay.h>
#include <string.h>
#include <math.h>
#asm
#endasm
#include <lcd.h>
#include <stdio.h>
// SPI functions
#include <spi.h>
int temp ;
// Port A initialization
32
Electrical Design 4
Energy Meter
PORTA=0x00;
DDRA=0x00;
// Port B initialization
PORTB=0x00;
DDRB=0xB0;
// Port C initialization
PORTC=0x00;
DDRC=0x00;
// Port D initialization
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
33
Electrical Design 4
Energy Meter
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
34
Electrical Design 4
Energy Meter
// Timer/Counter 2 initialization
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
TIMSK=0x00;
// USART initialization
// USART Transmitter: On
UCSRA=0x00;
35
Electrical Design 4
Energy Meter
UCSRB=0x08;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x19;
ACSR=0x80;
SFIOR=0x00;
// SPI initialization
SPCR=0x50;
SPSR=0x00;
lcd_init(16);
while (1)
ftoa(Temp,2,Display_LCD);
36
Electrical Design 4
Energy Meter
sprintf(LCD_txt,"Power = %s",Display_LCD);
lcd_gotoxy(0,0);
lcd_puts(LCD_txt);
delay_ms(1000);
lcd_clear();
};
void SPI_MasterInit(void)
37
Electrical Design 4
Energy Meter
DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK); //Set MOSI and SCK output, all others input
/* Start transmission */
SPDR = cData;
void SPI_SlaveInit(void)
DDR_SPI = (1<<DD_MISO);
/* Enable SPI */
SPCR = (1<<SPE);
char SPI_SlaveReceive(void)
return SPDR;
38
Electrical Design 4
Energy Meter
39