Digital DC watt meter using pic microcontroller

In this guide, we will design a digital dc watt meter using pic microcontroller which is used to measure dc power of dc circuits. Digital dc watt meter is very simple to use and basic idea of digital dc watt meter is given in this project.

This project involves the use of voltage and current sensors which are interfaced with the microcontroller. A voltage sensor is used to measure voltage across the circuit or load whereas a current sensor is used to measure current passing through the load. In this project, a shunt resistor is used as a current sensor. It is used to convert current into voltage form because a microcontroller does not understand current whereas it can read voltage directly. Moreover the circuit consists of a voltage divider because it used to measure the high voltage because microcontroller can not read high voltage or voltage greater than 5 volt. Additionally, we will use a Liquid crystal display to show the measured value of dc power.

Digital DC Watt meter Prerequisites

To understand this project completely you should know how to measure dc voltage using pic microcontroller, how to measure dc current using pic microcontroller and how to interface LCD with pic microcontrollers. If you don’t know about it, then follow the recommended readings given below first, so that you can get better understanding of digital dc watt meter project.

Digital dc watt meter circuit diagram

The circuit diagram of a digital dc watt meter is given below.

Digital Dc watt meter using pic microcontroller

0.47 resistor is used as a shunt resistor and we have already explained its purpose in digital ammeter project article in detail. Resistor R1 and R4 is used as a voltage divider. This is already covered previously in the article: digital voltmeter project. A 16×2 LCD displays measured DC power by fetching its value from PIC16F877A microcontroller. 8MHz crystal is used to operate PIC16F877A microcontroller. The DC ammeter shown above is just for simulation purposes. You don’t need to connect it while designing dc watt meter circuit.

Digital DC Watt meter Working

This project consists of three main components. Detail of these three components is given below:

  • Digital Ammeter: The digital Ammeter is designed using shunt resistor. R6 0.47 shunt resistor is used to measure current flowing through a load. Shunt resistor converts the current passing through it into voltage. This voltage is measured with the help of analog channel AN1 of PIC16F877A microcontroller. The measured voltage is converted back into current using ohm’s law formula I = V/R. This is because the value of the shunt resistor and measured voltage is known.
  • Digital voltmeter: The digital Voltmeter is used to measure voltage across the load. A voltage divider is used to step down voltage less than 5 volt. Voltage across R4 resistor of voltage divider is measured with the help of analog channel  AN0 of PIC16F877A microcontroller. Measured voltage is converted back into actual voltage by multiplying it with opposite of voltage divider formula.
  • PIC16F877A microcontroller: All mathematical calculations are done through programming of PIC16F877A microcontroller. It reads current and voltage through analog to digital converter. As you know dc power is just a product of voltage and current.

DC Power = Voltage × Current

So, by using the above formula, DC power can be easily calculated by writing a simple multiplication instruction in the program.

Digital DC watt meter with pic microcontroller Code

The code for DC watt meter measurement is written using Mikro pro C compiler. Its program is given below:

Make sure to tick the following system libraries found in the Library Manager before compiling the code:

  • ADC
  • Conversions
  • Lcd
  • Lcd_Constants
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

float current;
float voltage;
float power;
char Pwr[4];

void main() {

     PORTA = 0xFF;
     TRISA = 0XFF;
     PORTB = 0;
     TRISB = 0;
     Lcd_Init();
     ADC_Init();
     Lcd_Cmd(_LCD_CURSOR_OFF);
     Lcd_Cmd(_LCD_CLEAR);
     Lcd_Out(1,1,"Digital wattmeter");
     delay_ms(1000);
     while (1)
     {
        voltage = ADC_Read(0);
        voltage = (voltage * 5 * 10)/1024;
        
        current = ADC_Read(1);
        current = (current * 0.00489)/(0.47);
        
        power = voltage * current;
        inttostr(power,Pwr);

        Lcd_Out(2,1,"Power = ");
        Lcd_Out(2,8,Ltrim(Pwr));
        Lcd_Out(2,11,"W");
        
     }
 }

How the Code Works?

We will start off by defining the pic microcontroller’s PORTB connections with the LCD. PORTB is used to send data to LCD. These lines tell the microcontroller that LCD is connected to PORTB pins and these lines also specified the pin numbers as shown below:

sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

Next we will specify the directions of the pins:

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

Create three float variables called ‘current,’ ‘voltage’ and ‘power’ to store their respective readings.

float current;
float voltage;
float power;

Create a char array ‘Pwr’ of size 4.

char Pwr[4];

Inside the main() function, we always initialize variables, define GPIO pins settings and peripherals initialization.

     PORTA = 0xFF;
     TRISA = 0XFF;
     PORTB = 0;
     TRISB = 0;

Initialize the LCD module using LCD_Init() funtion and the ADC module using  ADC_Init().

     Lcd_Init();
     ADC_Init();

Next, we will send commands to the LCD to turn the cursor off and clear the screen.

 Lcd_Cmd(_LCD_CURSOR_OFF);
 Lcd_Cmd(_LCD_CLEAR);

Using Lcd_Out(), we will print the text ‘Digital wattmeter’ at starting position row=1 and starting position column=1.

Lcd_Out(1,1,"Digital wattmeter");

Voltage, Current and Power Measurements

Next, we will find the ADC value at channel 0 using ADC_Read(0) and save it in the variable ‘voltage.’ This reading is multiplied by 50/1024.

voltage = ADC_Read(0) : This function reads the analog value of voltage and converts it into binary value of voltage.

voltage = (voltage * 5 * 10)/ (1024) : It converts the binary value back into actual voltage by multiplying it with ADC resolution factor and voltage divider inverse. You can get its more detail from voltage measurement article.

        voltage = ADC_Read(0);
        voltage = (voltage * 5 * 10)/1024;

Next, we will find the ADC value at channel 1 using ADC_Read(1) and save it in the variable ‘current.’ This is the voltage across the shunt resistor. To find the dc current, we will use the ohm’s law to convert the voltage reading to current.

current = ADC_Read(1) : This function reads binary value of voltage across shunt resistor.

current = (current * 0.00489 )/ (0.47) : This function converts binary value of voltage across shunt resistor into current.

        current = ADC_Read(1);
        current = (current * 0.00489)/(0.47);

Finally, to find the power we multiply the voltage reading with the current reading, Lastly, we convert this value to a string in order to print it on the LCD.

 inttostr(power,Pwr) : It converts the dc current value into string to display it on the LCD.

        power = voltage * current;
        inttostr(power,Pwr);

Print the value of power with its unit on the LCD screen using Lcd_Out() and specifying the starting row, starting column and text as parameters inside it.

        Lcd_Out(2,1,"Power = ");
        Lcd_Out(2,8,Ltrim(Pwr));
        Lcd_Out(2,11,"W");

Video lecture on dc power measurement

Watch this video lecture on DC power measurement using pic microcontroller, if you want complete step by step guide

you may also like to read the following articles:

9 thoughts on “Digital DC watt meter using pic microcontroller”

  1. How connect the wattmeter for open circuit test of transformer ?the meter have 1to 10 slot for insert the wires and nothing have any other terminal .send mi the connection diagram on this wattsapp no 7507536671

    Reply
  2. is i possible to add set point for the time to turn on and off an LED on two different times. (For example i want to turn on LED at 6pm and turn off at 12am).

    Reply

Leave a Comment