## Digital Capacitance Meter using Arduino UNO

Programming, Tutorial, Coding, New Design, and/or any project for your Arduino and Raspberry Pi can post your topic /suggestion here.
Magdalo
Posts: 12
Joined: Fri Mar 17, 2017 2:17 pm

### Digital Capacitance Meter using Arduino UNO

This project presents an interrupt based approach by employing an NE555 timer and Arduino Uno to measure capacitance in the range of 1μF to 1mF, and provide local
indication using an LCD and data acquisition using a PC display (serial monitor of Arduino IDE).
Two methods have been described in here: first with NE555 timer configured in astable multivibrator mode and second with NE555 timer configured in monostable mode.

Here, NE555 timer (IC1) is operated with two external resistors (R1 and R2) and an unknown capacitor (Cx), whose value is to be measured. IC1 is powered with +5V from Arduino board (Board 1); thus, eliminating the need for an external DC power supply. IC1 generates a square-wave output of +5V amplitude at a specific frequency (depending on values of R1, R2 and Cx), which is available at its output pin 3. Output pin 3 of IC1 is connected to pin 2 of Arduino board. Since this pin of Arduino is a hardware interrupt pin (called Interrupt 0), source code (capacitance1.ino) uploaded to Arduino uses an interrupt handler, which is executed whenever the timer output makes a low-to-high transition. Thus, time period of the square-wave is continuously obtained by calculating the time difference between two such consecutive transitions. Time-period (T) of oscillation for the square-wave output from IC1 is given as:

T=0.693×(R1+2×R2)×Cx Thus, value of Cx is given as: Cx=1.443×T/(R1+2×R2) Measured value of Cx (in μF) is then displayed on a 16×2-character LCD and PC. NE555 timer. NE555 timer IC1 operates as an oscillator in astable multivibrator mode with free-running frequency, and duty cycle is accurately controlled by R1, R2 and Cx. Pins 4 and 8 of IC1 are connected to +5V connector of Board 1. R1 and R2 of 100-kiloohm each are connected between pins 6 and 7, and pins 7 and 8, respectively, of IC1. IC1 shares the same ground with Arduino board. The author’s designed breakout board for the timer.
Circuit diagram of Arduino based digital capacitance meter with NE555 timer in astable mode
arduino-capacitance-meter.png (39.01 KiB) Viewed 1333 times
The source code (capacitance1.ino) is written in Arduino programming language. Atmega328/Atmega328P is programmed using Arduino IDE software. Select the correct board from Tools→Board menu in Arduino IDE and burn the program (sketch) through the standard USB port in your computer. Here, code written in Arduino uses LiquidCrystal.h header file provided by Arduino library for working with the LCD. lcd.begin(16, 2) function helps configure the 16×2 character LCD. Serial.begin(9600) function initialises the serial port with a baud rate of 9600. attachInterrupt(0,cap,RISING) function calls interrupt handler ‘cap’ whenever a signal connected to interrupt 0 pin (pin 2) of Arduino makes a low-to-high, that is, rising edge transition. Serial.print(capacitance, 3) function prints the measured value of capacitance up to three decimal places using the serial port on the PC monitor. lcd.setCursor(0, 1) function sets the LCD cursor position to print from first column of second row. lcd.print(capacitance, 3) function prints the measured value of capacitance up to three decimal places on the LCD screen.
Circuit diagram of Arduino based digital capacitance meter with NE555 timer in monostable mode
capacitance-meter-circuit-diagram-b.png (31.23 KiB) Viewed 1333 times
In the second method NE555 timer (IC1) is operated in monostable multivibrator mode with external resistor R1 and an unknown capacitor (Cx), whose value is to be measured. IC1 is powered with +5V from Arduino board (Board 1); thus, eliminating the need for an external DC power supply. In this mode of operation, when trigger pin of the timer is made low (0V) by sending an active low pulse from pin 9, output of the timer (from pin 3) goes high (+5V) for a certain period of time, which is determined by the values of R1 and Cx. Time period (T) for which the timer’s output remains high is given as:
T = 1.1×R1×Cx
Output of the timer is connected to pin 2 of Arduino, which is a hardware interrupt pin (called Interrupt 0). The source code (capacitance2.ino) uploaded to Arduino uses
an interrupt-within-an-interrupt handler, that is, a two-level nested interrupt sub-routine (ISR). The first interrupt-handler gets executed whenever the timer output makes low-to-high transition, and the second ISR is called from within the first when timer output makes high-to-low transition. Thus, time for which the output of the timer remains high is obtained by calculating the time difference between two such consecutive interrupts, which is equal to T.
Thus, value of Cx is given as:
Cx = T/(1.1×R1)
Measured value of Cx (in μF) is then displayed on a 16×2-character LCD and serial monitor of Arduino IDE. NE555 timer. NE555 timer IC1 operates in monostable multivibrator mode, where time for the output goes high, after applying high-low-high pulse from pin 9 of Arduino, which is controlled by R1 and Cx connected externally to the IC. Output of timer pin 3 is connected to interrupt pin (pin 2) of Arduino. Pins 4 and 8 of the IC1 are connected to +5V connector of Board 1. R1 of 100kΩ is connected between pins 8 and 7. IC1 shares the same ground with Arduino board. The authors’ designed breakout board for the timer.

The code (capacitance2.ino) written in Arduino programming language uses LiquidCrystal.h header file provided by Arduino library for working with the LCD. attachInterrupt(0,analyze1,RISING) function calls the interrupt handler named analyze1 whenever output of IC1 connected to interrupt 0 pin (pin 2) of Arduino
makes low-to-high, that is, rising-edge transition. attachInterrupt(0,analyze2,FALLING) function calls the interrupt handler named analyze2 whenever output of IC1
connected to pin 2 of Arduino makes high-to-low, that is, falling-edge transition. The high-low-high trigger pulse applied to pin 2 of IC1 is generated by pin 9 of Arduino using the following code within void loop() function. Refer source code for the same.
void loop ( ){
digitalWrite(9, HIGH);
delay(10);
digitalWrite(9, LOW);
delay(1);
digitalWrite(9, HIGH);
while (1);
}
Note. To test a new capacitor (Cx), connect the capacitor and press Reset on Arduino Uno board.

Measurement-of-Capacitance-Using-Arduino.zip
Please share your success and if you found any mistake. Kindly comment below! enjoy

Magdalo
Posts: 12
Joined: Fri Mar 17, 2017 2:17 pm

### Re: Digital Capacitance Meter using Arduino UNO

Update:

The circuit diagram of the Capacitance Meter using Arduino is shown in below figure. Circuit is simple, a LCD is interfaced with Arduino to display the measured Capacitance of capacitor. A Square wave Generator Circuit (555 in Astable mode) is connected to Arduino, where we have connected the Capacitor whose capacitance needs to be measured. A Schmitt trigger gate (IC 74LS14) is used to ensure that only rectangular wave is fed to Arduino. For filtering the noise we have added couple of capacitors across power.

This circuit can accurately measure capacitances in range 10nF to 10uF.
Arduino-Capacitance-Meter-circuit-diagram.png (91.64 KiB) Viewed 346 times
555 Timer IC Based Square Wave Generator:
First of all we will talk about 555 Timer IC based square wave generator, or should I say 555 Astable Multivibrator. We know that the capacitance of a capacitor cannot be measured directly in a digital circuit, in other words the UNO deals with digital signals and it cannot measure capacitance directly. So we use 555 square wave generator circuit for linking the capacitor to digital world.

Simply speaking, the timer provides square wave output whose frequency directly implicates to the capacitance connected to it. So first we get the square wave signal whose frequency is the representative of the capacitance of the unknown capacitor, and feed this signal to UNO for getting the appropriate value.

General configuration 555 in Astable mode as a shown in below figure:
555-timer-astable-mode.png (46.99 KiB) Viewed 346 times
he output signal frequency depends on RA, RB resistors and capacitor C. The equation is given as,

Frequency (F) = 1/ (Time period) = 1.44/ ((RA+RB*2)*C).

Here RA and RB are resistance values and C is capacitance value. By putting the resistance and capacitance values in above equation we get the frequency of output square wave.

We are going to connect 1KΩ as RA and 10KΩ as RB. So the formula becomes,

Frequency (F) = 1/ (Time period) = 1.44/ (21000*C).

By rearranging the terms we have,

Capacitance C = 1.44/ (21000*F)

In our Program Code (see below), for getting the capacitance value accurately we have calculated the result in nF by multiplying the obtained results (in farads) with “1000000000”. Also we have used ‘20800’ instead of 21000, because the accurate resistances of RA and RB are 0.98K and 9.88K.

So if we know the frequency of the square wave we can get the capacitance value.

Schmitt Trigger Gate:
The signals generated by the timer circuit are not completely safe to be directly given to the Arduino Uno. With the sensitivity of UNO in mind, we use Schmitt trigger gate. Schmitt trigger gate is a digital logic gate.

This gate provides OUTPUT based on INPUT voltage level. A Schmitt Trigger has a THERSHOLD voltage level, when the INPUT signal applied to the gate has a voltage level higher than the THRESHOLD of the logic gate, OUTPUT goes HIGH. If the INPUT voltage signal level is lower than THRESHOLD, the OUTPUT of gate will be LOW. With that we don’t usually get Schmitt trigger separately, we always have a NOT gate following the Schmitt trigger.

We are going to use 74HC14 chip, this chip has 6 Schmitt Trigger gates in it. These SIX gates are connected internally as shown in below figure.
IC-74HC14-schmitt-trigger.jpg (18.5 KiB) Viewed 346 times
The Truth Table of Inverted Schmitt Trigger gate is show in below figure, with this we have to program the UNO for inverting the positive and negative time periods at its terminals.
Schmitt-Trigger-Truth-Table.gif (1.93 KiB) Viewed 346 times
We connect the signal generated by timer circuit to ST gate, we will have rectangular wave of inverted time periods at the output which is safe to be given to UNO.

Arduino measures the Capacitance:
The Uno has a special function pulseIn, which enables us to determine the positive state duration or negative state duration of a particular rectangular wave:
Htime=pulseIn(8,HIGH);
Ltime = pulseIn(8, LOW);
The pulseIn function measures the time for which High or Low level is present at PIN8 of Uno. The pulseIn function measures this High time (Htime) and Low Time (Ltime) in micro seconds. When we add Htime and Ltime together we will have the Cycle Duration, and by inverting it we will have the Frequency.

Once we have the frequency, we can get the capacitance by using the formula we discussed earlier.

Summary and Testing:
So in summary, we connect the unknown capacitor to the 555 timer circuit, which generates a square wave output whose frequency is directly related to capacitance of capacitor. This signal is given to UNO through ST gate. The UNO measures the frequency. With frequency known, we program the UNO to calculate the capacitance by using formula discussed earlier.

Let’s see some results I got,

When I connected 1uF Electrolytic Capacitor, the result is 1091.84 nF ~ 1uF. And the result with 0.1uF Polyester Capacitor is 107.70 nF ~ 0.1uF
Arduino-Capacitance-Meter-1uf-capacitor.jpg (9.45 KiB) Viewed 346 times
So that is how we can simply measure the Capacitance of any capacitor.

Code

Code: Select all

``````#include <LiquidCrystal.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

int32_t Htime;
int32_t Ltime;
float Ttime;
float frequency;
float capacitance;

void setup()
{
pinMode(8,INPUT);            //pin 8 as signal input
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("capacitance =");
}
void loop()
{
for (int i=0;i<5;i++)        //measure time duration five times
{
Ltime=(pulseIn(8,HIGH)+Ltime)/2;        //get average for each cycle
Htime=(pulseIn(8,LOW)+Htime)/2;
}

Ttime = Htime+Ltime;
frequency=1000000/Ttime;

capacitance = (1.44*1000000000)/(20800*frequency);   //calculating the Capacitance in nF
lcd.setCursor(0,1);
lcd.print(capacitance);
lcd.print(" nF   ");
delay(500);
}``````
I hope you like the Update! Thanks