# kmod's blog

28Jul/130

## Microcontroller Project: Digital Faradmeter

Alright, I got my \$10 soldering iron today and put together my USB->UART board, and I was finally able to program my Arduino-compatible ATmega328!  You can see what I'm messing around with here:

I got all the basic Arduino tutorials running, so I decided to try my hand at something a little tougher: a digital "faradmeter", ie capacitance-measurer.  I've never heard of a device like this so I'm not sure what it's called, so I'm just going to go with "faradmeter" to parallel "voltmeter" and "ohmmeter".  The usefulness of a device like this is a little debatable since it's only really useful for dedicated capacitors which are already going to be labeled, but let's go with it for now.

In the picture above, you can see the measured electrolytic capacitor on the top right of the breadboard.  Here's a very basic schematic of what my board looks like; I've omitted all the wiring that's due to the basic Shrimp schematic, which my board is based off; this is my first computer-made schematic, so don't be too harsh on it :)

The schematic is quite simple, but I'm learning that much of a microcontroller project's complexity is in the circuitry, and not just the code, so I'm trying to get practice at creating these schematics.

The basic idea behind this circuit is I'm measuring the capacitance of the capacitor by testing how quickly it charges.  I'm using pin A5 as a sense pin, and using both A3 and A4 as drive pins; using two separate pins lets the microcontroller control the amount of current it drives, theoretically increasing the range of capacitances that it can measure.

The basic procedure is the ATmega first fully discharges the capacitor, then charges it back up to roughly half-full and measures the amount of time that process takes, and from that time calculates the capacitance.  It starts by charging just with the 22kohm resistor, and then switches to the 1kohm resistor to speed up the process.

There are a couple ways to calculate the capacitance from the charging time; perhaps the simpler one is to essentially time-integrate the inferred current to get the charge on the capacitor, and use C=Q/V to get the capacitance.  I decided to go with a slightly more roundabout method of calculating from an exponential RC circuit model (which is derived from C=Q/V); I didn't try both ways to compare, but in theory the exponential approach should be better able to scale to small capacitances, since it can more-correctly calculate based on "large" (compared to the RC constant) time slices.

I tried the circuit on a bunch of the capacitors I have, and after dealing with a number of issues I think it's finally working.  I think the one issue that I ran into most, but might have been misattributing so I'm not sure, is that if you switch a pin from driving a logic 1 to being an input pin, with the purpose of disabling the pin, it will actually activate an 20kohm pullup resistor instead of going truly high-impedence.  This might not matter too much for digital circuits, since it won't typically change the logic level, but in this system where I'm driving sub-mA currents, it threw off the results.  Speaking of results, I'm not sure how accurate mine are since I have no way to double-check them, but they do align closely with the marked capacitances.  The schematic I have above seemed to be able to measure down to about the 10nF range, though I bet if I replaced R1 with a larger resistor + improved the code, I could get it down into the 1nF and possibly even 100pF range.

I started yet-another github repo, and you can see the arduino sketch + eagle files here.

### Update

I hooked up my "LCD Button Shield" to my faradmeter to directly see the readings, rather than having to look at the serial console.  It was actually surprisingly easy to set up, other than the fact that I had bent one of my ATmega pins when inserting it and didn't realize this until I tried using it.  The Arduino IDE comes with a "LiquidCrystal" library that is designed to drive displays like this; writing out "hello world" is just a few lines, and writing out the capacitance was just a matter of duplicating the output to both the serial and the lcd.

Also, according to Wikipedia these devices are just called "capacitance meters".