Introduction: Microbit Capacitance Meter

What is a capacitor?

A device that stores electrical charge which in its basic form consists of two conducting plates separated by a dielectric.

In addition to the basic parallel plate several other forms of construction exist an example being that the plates in the form of a metal foil allow the capacitor to be formed into a roll reducing its size.

These basic types tend to be of low capacitance, polystyrene film being an example with capacitance of <=10nF and single layer ceramic up to ~1uF.

Variants of these forms exist using different dielectrics and construction for a variety of different uses.

Beyond the basic forms are the polarised (Electrolytic), types which employ a gel or liquid electrolyte with capacitance up to ~1F, used in coupling, decoupling, supply and timing applications.

This brings us on to the subject of timing which is a property which we will be making use of in this project.

Supplies

Microbit (V1 or V2)

Microbit Breakout board or equivalent

BS270 NFET or equivalent

10K resistor

56k resistor

1k resistor - Qty 2

Stripboard (24 Holes x 12 Holes)

PCB Screw Terminal Block 5mm pitch

SIL pin header

*** There is no affiliation to any of the sites linked from this list and it simply presents possible sources. ***

Step 1: Theory

If we connect a capacitor across a a low voltage source (3V), as used by the Microbit (voltages exceeding the maximum specified on the capacitor will result in it's damage as will applying the wrong polarity), in series with a resistor.

Whilst, at the same time measuring the voltage across the capacitor, relative large values of capacitance will allow you to see the increasing voltage using a DMM or an oscilloscope.

If for example we choose of capacitor of 47uF and a resistor of 10K we can determine the charging characteristics.

If we assume that the capacitor is fully discharged prior to applying the voltage at time = 0 and current = 0.

At switch on the initial voltage across the capacitor is zero therefore the current is at its maximum I=V/R = 3V/10K = 300uA.

The capacitor charges exponentially (voltage increasing whilst the current is decreasing), up to the maximum voltage.

The time that it takes to reach 63% of the supply voltage is called the time constant (1T), meanwhile the capacitor continues to charge until at 5T its effectively fully charged and I=0.

The Time constant is calculated from C*R.

In this case 47uF * 10K = 470mS (1T), meaning it will take 0.47 seconds to reach 3V*0.63 = 1.89V.

But why is 1T, 63% of the applied voltage.

We can calculate the voltage at any instant of time with the following equation:

Vc = Vs*(1-e(-t/CR))

Vc = Capacitor Voltage

Vs = Supply Voltage

e = Euler Number (~2.78)

t = elapsed since charge initiation for the capacitor in question.

CR = the time constant for the circuit.

Taking our example earlier for C=47uF, R=10K, Vs=3V and CR=470mS measuring the voltage at t=470mS, gives the following:

Vc = 3*(1-(e(-470mS/470mS)) = 3*(1-e(-1)) = 3*(1-0.367) = 3*0.6321 = 1.896V = 63% of Vs

Taking this equation and plotting a chart for the values from 0T up to and beyond 5T we can see the charging process.

Step 2: Circuit

The circuit consists of a charging element a 10K resistor connected to P0, configured as a digital output this is set to logic 1 (3V), to begin charging the unknown capacitor.

Connected to P1 via a 1k resistor is the voltage monitoring pin set as an analogue input.

The remaining pin P2 is connected to the input of the discharge circuit which consists of a bias resistor network, 1k in parallel with a 56K, with the junction of these connected to the gate of the N channel FET. This is used to ensure the capacitor is fully discharged prior to initiating charging.

Therefore with this circuit charging, monitoring and discharging can be accomplished with the control of the microcontroller and also serves as protection against possible excessive conditions by isolating it from an unknown pre charged capacitor.

The resistors limiting current and the NFET providing reverse polarity protection.

*** However, even so precautions should be taken to ensure that charged capacitors with voltages in excess of 3V should not be connected to reduce the likelihood of damage to the microcontroller. If in doubt discharge via a suitable resistor capable of withstanding the discharge condition in a controlled manner ***

The circuit is built using strip board 20 holes long by 10 holes wide and mounted to the breakout board via SIL pins and sockets.

The charging resistor of 10k means 100uF takes 1 second and a 1000uF would take 10 seconds.

The charge time can be reduced by using a smaller resistor, say 1K for capacitances 100uF to 1000uF and for smaller values a larger resistor of 100K for values of 0.1uF to 1uF.

With these changes, charging times can be kept to 1 second maximum.

Step 3: Assembly

For this particular assembly two strip boards are used.

The larger board to which the components are mounted in addition to a SIL pin strip.

A slimmer board 20 holes by 3 holes wide with SIL sockets which are mounted inverted to allow the sockets to connect to the SIL pins.

A screw terminal block is mounted on the board to enable the unknown capacitor to be directly connected for measurement.

Depending on the capacitor terminal configuration, leads with crocodile clips or other suitable fixturing may be connected to the terminals.

All the components are directly connected to the board with the exception of the NFET which is mounted on terminal pins making for easy removal should it prove necessary.

Step 4: Coding

The code is broken down into the following functions.

Start

This is were the required variables to be used in the code are initialised and the digital pins are set low to ensure the external circuit is switched off.

Defaults

These variables Vmax, Voff and Coff affect the calculations and results.

For the MicroBit Vmax is nominally 3V but may actually differ slightly for improve accuracy measure the value with a DMM.

Voff is the zero voltage offset error

Coff is the zero capacitance error.

Discharge

This function applies a logic high (1), to the input to the NFET switching it on and discharging the capacitor connected to the input.

A delay in the form of a pause allows sufficient time for the capacitor to discharge.

After the delay the NFET is switched off by applying a logic low (0).

On button A

This initiate the measurement phase.

Displaying the "?" during the measurement process, setting zero = 0, to differentiate between measuring the capacitor to tested or the open circuit offsets.

Calling the Measure function.

Measure.

Sets P0 (the charge voltage), to 0V and P2 (discharge circuit input) to 0V, ensuring its turned off.

Timeout flag, 0=reset, 1=measurement, -1=measurement error

Tstart= Timer start time, Tstop= Timer stop time.

Charge time measurement is performed in microseconds whilst the process loop is performed in milliseconds.

T1= Process start time (In the event that Tstart and Tstop are not captured the process will stop after a predefined time period to prevent an infinite wait time).

This then calls Capture (This captures and further process the capacitor voltage with time)

If Timeout =1 and zero =0 then display the capacitance.

If Timeout = 1 and zero =1 then display results of offset measurement.

If Timeout <= 0 then display error message.

Ensure the charging voltage is 0V

Capture.

Set V1 = 0, used to store the zero offset value.

Calls Discharge to discharge the capacitor.

Sets a do while Timeout=0 and process elapsed time is < 3 seconds (subject to R and maximum value of capacitance to be measured).

If Tstart=0 which it will be on the first pass.

Measure the capacitor voltage which should be zero at this time if not and error is returned.

Store V1, start the timer and set P0 to logic to charge the capacitor.

If Tstart>0 which it should be if the capacitor is discharged to start with.

Measure the capacitor voltage until the voltage is >= 1.89V (63% of Vs), stop the timer and set P0=0V to disable charging.

If it never reaches >=1.89V the process will eventually timeout returning an error.

Tstop - Tstart = time constant (1T).

As T=CR and given we know T and R=10k the formula can be rearrange to C=T/R

If assuming a nominal 10K resistor without measuring its value first error compensation against a known valued capacitor can be applied.

Therefore ((Tstop-Tstart)/10K)/1.11127

If the result is <=0 then Timeout is set to -1 to indicate an error condition otherwise Timeout =1.

On Button B

**** Performed with the test terminals open circuit. ***

Sets zero = 1

Calls the function Measure and returns the offset values.

On Button A+B

Displays Voff and Coff resets them back to default values.

Step 5: Operation

Connect the test board and the Microbit together.

Power up the Microbit which will first display the application title (Xcap), followed by the menu.

You may wish to measure the Microbit 3V pin voltage and the 10K resistor and program these values in the code.

Repowering the Microbit.

Without a device connected to the terminals press Button B to apply the offsets measures in real time if this step is not carried out the default valued coded will be applied.

Attach the unknown capacitor to the test board either by the screw terminals or suitable socket of leads attached to the screw terminals dependant upon the device connections.

Press Button A to make the measurement.

The "?" will be displayed while measurement is in progress.

If a valid measurement is carried out the results will be returned in uF (Micro Farads)

The charge and discharge cycles are shown in the accompanying waveform.

Charging from 0V to 1.89V is 475mS equating to 47.5uF

Discharging from 1.89V to 0V after the measurement has been taken.

An open circuit will return the zero offset and a short circuit will display "E" for an error.

Be aware that the result returned will affected by the supply which will affect the 63% threshold the capacitance measurement offset, voltage offsets, timing variation and charging resistor value.

Although, at the end of the day this is not intended to be a precision piece of equipment but more of a diagnostic and experimental tool.

However, capacitors in particularly Electrolytic types have tolerances over the range of -20% to +75% and best case -5% to +10% subject to type.

Step 6: Finally

Hope you found it of interest.

Automation Contest

Participated in the
Automation Contest