Analog Comparator falling edge setup

Somehow I knew that I shouldn't have said what I'm building because there is always someone who just wants to argue instead of helping someone.
Btw, the Meter has been sold tens on thousands of times (as a kit and prebuild) and is still being sold, so the method of "indication" can't be too wrong.

daywalkerdha:
Somewhere I heard, that interrupts aren't allowed within interrupts. Is that true? If the Arduino "remembers" that there was another(different) interrupt during the timer interrupt and executes it afterwards, that would be fine ...

It wouldn't work very well if it could only handle one interrupt source at a time. Yes it will remember interrupt events. The ISR will not be entered until the current one completes, and one more instruction is executed. Then the highest priority outstanding interrupt (if any) will be serviced.

Thanks!

I have another question on timing inside an interrupt. Currently a

bitWrite(PORTD, measurementRange, 0);

is just called multiple times inside a timer interrupt to make a pulse with a width of somewhere between 4µs and 15µs(adjustable) because i read here » Pin I/O performance » JeeLabs that it takes about 1µs to execute a bitWrite(). Is there a better way for such short timings? It doesn't have to be that precise, just the same amount of delay every time once it's set.

You could call delayMicroseconds instead, but I'm not sure what the minimum delay you can get with it is.

dc42:
You could call delayMicroseconds instead, but I'm not sure what the minimum delay you can get with it is.

On the Arduino website, it says around 3µs. But can I call it inside a timer interrupt?

This function works very accurately in the range 3 microseconds and up. We cannot assure that delayMicroseconds will perform precisely for smaller delay-times.

Yes, you can call it inside an ISR. However, inside an ISR you should use it only for short delays, because you are lengthening the time it takes to complete the ISR and hence the time before other interrupts can be serviced.

dc42:
Yes, you can call it inside an ISR. However, inside an ISR you should use it only for short delays, because you are lengthening the tie it takes to complete the ISR and hence the time before other interrupts can be serviced.

Ok, thanks! The interrupt will be executed every 500µs and inside it will only be one delayMicroseconds() of around 10µs and the whole routine should take no longer than 15µs, including the delay.

You can do the whole thing with PWM. In my project of making VGA output I needed both vertical and horizontal sync pulses. It sounds similar to what you are doing. You need the pulses a certain width apart, and with a short pulse time. Project page:

Example screenshot:

In that case I had a 4 uS pulse with a 32 uS period. Those figures can be changed by the timer configuration values. This doesn't rely on ISRs at all (for the pulses) since the timer does that.

Yes, but I need to switch the pulse between three pins to change the range for my measurement. That would only be possible with software PWM, right?

daywalkerdha:

[quote author=Nick Gammon link=topic=136603.msg1029397#msg1029397 date=1355257310]
You can do the whole thing with PWM.

Yes, but I need to switch the pulse between three pins to change the range for my measurement. That would only be possible with software PWM, right?
[/quote]

You have 3 timers on an Arduino Uno, one 16-bit and two 8-bit. So if 8-bit timers are adequate to get the frequency and pulse width resolution your require, you can use timers to generate pulses on 3 pins. However, timer 0 is normally used to count microseconds. So, when you are using timer 0 to generate pulses, you will need to switch the timekeeping function to timer 2. Alternatively, use a Mega, which has more timers.

Alternatively, use a single timer with external gating.

I've now looked at the ESR meter article. The circuit does look rather complicated. It was designed in the late 1990s and is suited to the technology of the time. Essentially, it feeds pulses of a known current to the capacitor under test, discharging the capacitor more or less fully between pulses. It is assumed that the capacitor does not charge significantly, because the pulse is short and the capacitor under test is assumed to have high capacitance. It measures the pulse amplitude by amplifying it and comparing it with a ramping voltage.

I am concerned that the atmega328p datasheet provides (as far as I can tell) no data on the speed of the analog comparator, so there is no telling whether it is fast enough for this application.

An alternative would be to use the ADC to read the pulse amplitude directly. The ADC sample and hold takes nominally 1.5 ADC clock cycles. The default Arduino ADC clock is 125kHz, so this is 12us, plus an 8us uncertainty about when the sample is taken. If you increase the ADC clock frequency to 250KHz, you halve this time to 10uS total at the expense of slightly reduced accuracy. So if you write the start conversion command to the ADC and then immediately start the pulse, then if the pulse is at least 10us long you should be able to read its amplitude through the ADC.

Another option is to use a faster external A to D converter, or an external sample and hold circuit. Either way, you don't need the ramp[ generator circuit.

If you do decide to use the comparator (or an external comparator), then consider using a DtoA converter to generate the ramp instead of several transistors and resistors and a capacitor.

Here is the output from less than 10 lines of code, measuring a 100uf capacitor with a 10ohm ESR.

esr = 9969mOhm
esr = 10000mOhm
esr = 10030mOhm
esr = 10060mOhm
esr = 9755mOhm
esr = 9785mOhm
esr = 9815mOhm
esr = 9845mOhm
esr = 9876mOhm
esr = 9906mOhm
esr = 9937mOhm
esr = 10312mOhm
esr = 10344mOhm
esr = 10020mOhm
esr = 10052mOhm
esr = 10084mOhm
esr = 10116mOhm
esr = 10137mOhm
esr = 10170mOhm
esr = 10202mOhm
esr = 10588mOhm
esr = 10257mOhm
esr = 10290mOhm
esr = 10679mOhm
esr = 10345mOhm
esr = 10379mOhm
esr = 10402mOhm
esr = 10436mOhm
esr = 10470mOhm
esr = 10493mOhm
esr = 10528mOhm
esr = 10551mOhm
esr = 10586mOhm
esr = 10609mOhm
esr = 10645mOhm
esr = 10668mOhm
esr = 10324mOhm
esr = 10728mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10728mOhm
esr = 10728mOhm
esr = 10728mOhm
esr = 10728mOhm
esr = 10728mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10728mOhm
esr = 10728mOhm
esr = 10728mOhm
esr = 10728mOhm
esr = 10728mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10728mOhm
esr = 10728mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10728mOhm
esr = 10728mOhm
esr = 10728mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm
esr = 10347mOhm

Display in mOhm.

All it takes is a digital pin, an analog pin, and a 330ohm resistor (user defined in the code).

Plus a few lines of code on an arduino.

Are you going to reveal what those < 10 lines of code are?

dc42:
I am concerned that the atmega328p datasheet provides (as far as I can tell) no data on the speed of the analog comparator, so there is no telling whether it is fast enough for this application.

I was concerned about that as well, but I'll try and report back.

As far as the circuit itself is concerned, I understand how it works and will use a similar setup. I know there are other ways with less hardware but this project is not intended for sale or anything, mainly just to use Atmega328 features I haven't used before(like the comparator) and understand how they work. And to have a working ESR-meter in the end :wink:

@dhenry
I'd be interested in the code and schematic as well, but I'll implement it the other way.

When you are done, come back and we can compare notes then.

dhenry:
When you are done, come back and we can compare notes then.

You know the schematic, and the code is pretty easy

ISR(TIMER2_COMPA_vect){                        // every 500µs
  bitWrite(PORTD, 3, 0);                          // Disable discharge
  bitWrite(PORTD, measurementRange, 0); // Pulse
  delayMicroseconds(7);                           // The pulse width should be around 8µs
  bitWrite(PORTD, measurementRange, 1); // Stops the pulse
  bitWrite(PORTD, 3, 1);                         // Enable discharge
}

ISR(ANALOG_COMP_vect){ // Comparator interrupt
  compareCounter++;
}

In the lowest range, every pulse is 10m?. In the middle range 100m? and 1? in the high range.

What about your code and setup?

In the lowest range, every pulse is 10m?. In the middle range 100m? and 1? in the high range.

Great. Please let us know how that's working out for you.