Home / Home made Projects / Microcontroller Projects / Capacitance meter: How to measure capacitance with microcontroller

Capacitance meter: How to measure capacitance with microcontroller

This unique article will is to explain the design of capacitance meter using microcontroller. The principle of measuring capacitance is explained. Further the required components and connection diagram are presented. The code needed for this project is discussed and lastly results are illustrated in Proteus simulation software.

Introduction

There are three types of loads in electrical system named as resistor, inductor and capacitor. All of them have different properties and uses. Due to variant properties, their measurement procedure is also different.

Capacitor is an electrical device which stores energy in the form of electric field. It consists of two metallic plates and a dielectric medium between them. When a DC voltage is applied across the plates, it stores charge on the plates. The ability of a capacitor to store energy in the form of electric charge is called capacitance. The higher the capacitance of capacitor, the more charge it can store. The purpose of this article is to build a homemade capacitance meter using microcontroller.

Working principle of capacitance meter

Consider a simple charging circuit of a capacitor. It consists of a capacitor and resistor connected in series with a DC battery through a switch. When the switch is closed, the charges begin to accumulate on the plates of the capacitor.

Capacitor charging circuit

The charging of capacitor follows the following equation

Where  V0 = Applied Voltage

                Vc = Voltage of capacitor

                T = time of charging

                R= resistance value

                C = Capacitance of capacitor

The charging plot of capacitor is shown in the following figure

capacitor charging plot

The principle of measuring capacitor in this article is based on the capacitor charging equation. If a capacitor is inserted in a charging circuit having input voltage and series resistance value is already known. By applying the input voltage for a fixed amount of time, certain charge will be stored. By the measuring the voltage of the capacitor, the capacitance can be determined by putting all the known quantities in the capacitor charging equation.

So last equation is used to find the capacitance of capacitor. 

So last equation is used to find the capacitance of capacitor.

Schematic

Following components are required to construct a capacitance meter

  • Two switches (relays or can be any )
  • Resistors
  • Push buttons
  • Relay driver ULN2003A
  • Microcontroller (PIC 18F452)
  • Power Source 5V & 12V
  • LCD

The above components are connected as shown in the following diagram

Capacitance meter schematic diagram

Push button is connected to Pin RB0 while capacitor voltage is measured using analogue pin RA0. LCD is interfaced with Port C and switches for charging capacitor are controlled through port D. The sequence of events is as follows

  • Push button needs be pressed to measure capacitance.
  • When push button is pressed, charging switch is closed.
  • Capacitor is connected to ground for 1 sec to discharge any charge stored on it.
  • After 1 sec, power switch is closed for 5ms to store charge on capacitor.
  • After 5ms, charge switch is opened to disconnect capacitor from battery.
  • Power switch is also opened to save battery from discharging.
  • Voltage due to stored charge on capacitor is measured by controller.
  • The known values t=5ms, Vo = 5V, R = 50kΩ, and measured capacitor voltage (Vc) are used to determine capacitance using charging equation.
  • Measured value is displayed on LCD.

All the above steps are performed by microcontroller after button is pressed by the user.

Simulation Results

The capacitance meter is tested in ISIS Proteus environment for different values of capciatance and the results are displayed.

For 50nF capacitor

capacitance meter result 1

The meter shows 47nF which is fairly correct value.

Now for 500nF capacitance value

capacitance meter result 2

The capacitance meter shows 499nF values which is very much near to the actual 500nF value.

Code

The code for microcontroller is written in C language and compiled in MIKRO C compiler.

#define charge PORTD.F1. //assigning names to pins
#define button PORTB.F0
#define power PORTD.F0
float voltage; // initializing variable for capacitor calculation
float voltagecap;
sbit LCD_RS at Rc3_bit;
sbit LCD_EN at Rc2_bit;
sbit LCD_D4 at Rc4_bit;
sbit LCD_D5 at Rc5_bit;
sbit LCD_D6 at Rc6_bit;
sbit LCD_D7 at Rc7_bit;
sbit LCD_RS_Direction at TRISc4_bit;
sbit LCD_EN_Direction at TRISc5_bit;
sbit LCD_D4_Direction at TRISc0_bit;
sbit LCD_D5_Direction at TRISc1_bit;
sbit LCD_D6_Direction at TRISc2_bit;
sbit LCD_D7_Direction at TRISc3_bit;
void main() {
char z[14]; // char to store measured value
float a,b,c,d; // arbitrary variables for calculation of capacitance
TRISC=0; // making port C as an output port
TRISD=0; // making port D as an output port
TRISB=0XFF; // making port B as in input port
TRISA=0XFF; // making port A as in input port
ADC_Init(); // Initialize ADC module with default settings
Lcd_Init(); // Initialize Lcd
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,”Welcome to”); // dsiaplay the welcome logo on the LCD screen at position ROW=1 COLUMN=1
Lcd_Out(2,1,”help2educate”);
delay_ms(1000); // keep displaying logo for 1s
while (1){
if(button==1)
{
charge =1; // close charging switch to discharge capacitor fully before measuring
delay_ms(1000); // discharge for 1sec
power=1; // close power switch to connect battery to capacitor for charging
delay_ms(5); // charge capacitor for 5ms
charge=0; // open charge switch to isolate capacitor from battery and ground
power =0; //  open power switch to prevent battery from discharging
voltage = ADC_Read(0); // reading analogue value from channel 0. 0 stands for channel number
delay_ms(1);
voltagecap=(voltage*4.88)/1000; // convert digital value into analogue
a=voltagecap/5; // first step of charging equation Vc/Vo
b=1-a; // 1-Vc/Vo
c = log(b); // ln (1-Vc/Vo) = X
d=-100/c; // t/RX & converting to nF
floattostr(d,z); // convert to char for display
}
else
{floattostr(d,z);} // if button not pressed
lcd_cmd(_lcd_clear);
Lcd_Cmd(_LCD_CURSOR_OFF);
lcd_out(1,1,z);
lcd_out(1,15,”nF”);
delay_ms(100);
}}

About Syed Noman ud din

Syed Noman ud din is an Electrical Engineer and working in Industry from last 3 years. He writes technical articles for electrical and electronic engineers. He has also published several research publications in renowned international journals.

Check Also

Digital Frequency meter for sine waves

This article describes the design and construction of microcontroller based digital frequency meter. The circuit …

Leave a Reply

Your email address will not be published. Required fields are marked *

*