Introduction: ATTiny85 Capacitor Meter
This instructable is for a Capacitor meter based on the ATTiny85 with the following features.
- Based on ATTiny85 (DigiStamp)
- SSD1306 0.96" OLED Display
- Frequency measurement for low value capacitors 1pF - 1uF using 555 oscillator
- Charge time measurement for high value capacitors 1uF - 50000uF
- 2 separate ports used for the methods to minimise stary capacitance
- Two values of current used for Charge Time to minimise time for large capacitors
- 555 method self zeros at start up, can be rezeroed with push button
- A quick test used to select which method should be used for each cycle of measurement.
- Charge time method accuracy can be improved by support for OSCVAL clock frequency adjustment
Step 1: Schematic and Theory
The schematic shows the ATTiny driving the SSD1306 OLED display via an I2C interface. It is directly powered from a LiOn 300mAh battery and a charging point is included which can be used with a LiOn compatible external charger.
The first measurement method is based on measuring the frequency of a 555 free running oscillator. This has a base frequency determined by the resistors and a capacitor which should be high accuracy as this determines the accuracy of the measurements. I used a 820pF 1% polystyrene capacitor I had but other values around 1nF can be used. The value has to be entered into the software together with an estimate of any stray capacitance (~20pF). This gave a base frequency of around 16KHz. The output of the 555 is fed into PB2 of the ATTiny which is programmed as a hardware counter. By measuring the count over a period of about 1 second the frequency can be determined. This is done at start up to determine the base frequency. When a capacitor under test is added in paralle to the base capacitor then the frequency is lowered and when this is measured and compared to the base frequency then the value of the added capacitance can be calculated.
The nice feature of this method is that the calculated value is only dependent on the accuracy of the base capacitor. The period of the measurement does not matter. The resolution depends on the resolution of the frequency measurements which is quite high so even very small added capacitance can be measured. The limiting factor seems to be the 'frequency noise' of the 555 oscillator which for me is equivalent to about 0.3pF.
The method can be used over a decent range. To improve the range I synchronise the measurement period to detecting edges of the incoming pulses. This means that even low frequency oscillation like 12Hz (with a 1uF capacitor) are measured accurately.
For larger capacitors the circuit is arranged to use a charge timing method. In this the capacitor under test is discharge to ensure it starts at 0, then charged through a known resistance from the supply voltage. An ADC in the ATTiny85 is used to monitor the capacitor voltage and the time to go from 0% to 50% charge is measured. This may be used to calculate the capacitance. As the reference for the ADC is also the supply voltage then this does not affect the measurement. However, the absolute measure of the time taken does depend on the ATTiny85 clock frequency and variations in this do affect the result. A procedure may be used to improve the accuracy of this clock using a tuning register in the ATTiny85 and this is described later.
To discharge the capacitor to 0V a n-channel MOSFET is used together with a low value resistor to limit the discharge current. This means even large value capacitors can be discharged rapidly.
To charge the capacitor 2 values of charging resistor are used. A base value gives reasonable charging times for capacitors from 1uF up to about 50uF. A p-channel MOSFET is used to parallel in a lower resistor to allow higher value capacitors to be measured in a reasonable interval. The values chosen give a measurement time of about 1 second for capacitors up to 2200uF and proportionally longer for larger values. At the lower end of value the measurement period must be kept reasonably long to allow determining the transition through the 50% threshold to be done with enough precision. The sampling rate of the ADC is about 25uSec so a minimum period of 22mSec gives reasonable precision.
As the ATTiny has limited IO (6 pins) then allocation of this resource needs to be done carefully. 2 pins are needed for the display, 1 for the timer input, 1 for the ADC, 1 for discharge control and 1 for charge rate control. I wanted a push button control to allow for re-zeroing at any point. This is done by hi-jacking the I2C SCL line. As the I2C signals are open drain then there is no electrical conflict by allowing the button to pull this line low. The display will stop working with the button depressed but this is of no consequence as it resumes when the button is released.
Step 2: Construction
I made this up into a small 55mm x 55mm 3D printed box.designed to hold the 4 major components; the ATTiny85 DigiStamp board, the SSD1306 display, the LiOn battery, and a small bit of prototype board holding the 55 timer and charge control electronics.
Enclosure at https://www.thingiverse.com/thing:4638901
- ATTiny85 DigiStamp board. I used a version with a microUSB connector which is used for uploading firmware.
- SSD1306 I2C OLED Display
- 300mAH LiOn battery
- Small strip of prototyping board
- CMOS 555 timer chip (TLC555)
- n-Channel MOSFET AO3400
- p-Channel MOSFET AO3401
- Resistors 4R7,470R,22K,2x33K
- Capacitors 4u7,220u
- Precision Capacitor 820pF 1%
- Miniature slide switch
- 2 x 3 pin headerss for charge port and measurement ports
- Push Button
- Hook up wire
- Fine point soldering iron
First make up the 555 timer circuit and charging components on the prototype board. Add flying leads for the external connections. Affix the slide switch and charge point and measuring port into the enclosure. Affix the battery and do the main power wiring to the charge point, slide switch. Connect ground to push button. Affix the ATTiny85 in place and complete the hook up.
You can do some power saving modifications to the ATTiny board before fitting which will reduce the current a bit and extend battery life.
This is not critical as there is a power switch to turn off the meter when not in use.
Step 3: Software
Software for this Capacitor Meter can be found at
This is an Arduino based sketch. It needs libraries for the display and I2C which can be found at
These are optimised for the ATTiny to take up minimal memory. The I2C library is a high speed bit bang method which allows using any 2 pins. This is important as the I2C methods using the serial port use PB2 which is in conflict with using the timer/counter input needed to measure the 555 frequency.
The software is structured around a state machine which takes the measurement through a cycle of states. An ISR supports overflow from the timer counter to extend the 8 bit hardware. A second ISR supports the ADC operating in continuous mode. This gives the fastest response to the charging circuit crossing the threshold.
At the start of each measurement cycle a getMeasureMode function determines which is the most appropriate method to use for each measurement.
When the 555 method is used the timing of the counting only starts when the counter has changed. Likewise the timing is only stopped after the nominal measurement interval and when an edge is detected. This synchronisation allows accurate calculation of the frequency even for low frequencies.
When the software starts the first 7 measurements are 'calibration cycles' used to determine the base frequency of the 555 with no added capacitor. The last 4 cycles are averaged.
There is support for adjusting the OSCAL register for clock tuning. I suggest setting the OSCCAL_VAL to 0 initially at the top of the sketch. This means the factory calibration will be used until tuning is performed.
The value of the 555 base capacitor need to be adjusted is required. I also add on an estimated amount for stray capacitance.
If different resistors are used for the charge methods then the CHARGE_RCLOW and CHARGE_RCHIGH values in the software will also need to be changed.
To install the software use the normal digistamp method of uploading the software and connecting the usb port when prompted. Leave the power switch in the off position as power will be supplied by the USB for this operation.
Step 4: Operation and Advanced Calibration
Operation is very straightforward.
After turning on the unit and waiting for the calibration zero to finish then connect the capacitor under test to one of the two measurement ports. Use the 555 ports for low value capacitors < 1uF and the charge port for higher value capacitors. For electrolytic capacitors connect the negative terminal to the common earth point. During testing the capacitor will be charged up to about 2V.
The 555 port can be rezeroed by holding in the push button for about 1 second and releasing. Make sure nothing is connected to the 555 port for this.
The charge method relies on the absolute clock frequency of the ATTiny85 to measure time. The clock uses the internal RC oscillator arranged to give a nominal 8MHz clock. Although the stability of the oscillator is quite good for voltage and temperature variations its frequency can be out by quite a few percent even though it is factory calibrated. This calibration sets the OSCCAL register at start up. The factory calibration may be improved by checking frequency and making more optimal setting of the OSCCAL value to suit a particular ATTiny85 board.
I haven't managed to fit in a more automatic method into the firmware yet so I use the following manual procedure. Two variations are possible depending on what external measurements are available; either a frequency meter capable of measuring the frequency of the triangular waveform on the 555 port, or a square wave source of known frequency e.g. 10KHz with a 0V/3.3V levels which can be connected to the 555 port and override the waveform to force that frequency into the counter. I used the second method.
- Start up meter on its normal power with no capacitors connected.
- Connect the frequency meter or square wave generator to the 555 port.
- Restart calibration cycle by pushing button.
- At the end of the calibration cycle the display will show the frequency as determined by the counter and the current OSCCAL value. Note that repeated use of the calibration cycle will toggle between showing the measured frequency and normal no display.
- If the displayed frequency is less than what is known then it means the clock frequency is too high and vice versa. I find an OSCCAL increment adjusts the clock by about 0.05%
- Calculate a new OSCCAL value to improve the clock.
- Enter new OSCCAL value into OSCCAL_VAL at top of firmware.
- Rebuild and upload new firmware. Repeat steps 1 -5 which should show the new OSCCAL value and the new frequency measurement.
- If necessary repeat steps again until best result is achieved.
Note is important to do the measurement part of this tuning when running on normal power not USB to minimise any frequency shift due to supply voltage.
Tip 2 months ago
Is it accurate? I'd like to make one. Thank you very much!
Reply 2 months ago
Yes. I find it accurate. I tested against some 1% capacitors and it gave good results.
Calibration is necessary to improve accuracy for the measuremen method used for higher value capacitors as this does depend on the clock frequency of the cpu which can be a few % out. This is explained in the instructable section on calibration.
Reply 2 months ago
Thanks! This reassures me. I have favorited it and I'll try it in my spare time! :)