Input pin threshold

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi.
I'm sitting here experimenting with a tiny13 based circuit for reading a capacitive sensor. The principle is: discharge the capacitor, then start charging through a resistor and start a timer, and finally read the timer when the capacitor voltage reaches the threshold value.

Originally I used the comparator to determine when the threshold is reached. A voltage divider to one comparator input, and the capacitor to the other. Worked quite nicely. But I also want to use the PWM outputs, which happen to be connected to the same pins as the comparator inputs. So, I had a look at the input pin characteristics in the datasheet, which indicated a nice low/high threshold at about half the supply voltage. I redesigned the circuit a bit, and just used an ordinary digital input to determine the capacitor threshold (poll the pin and stop the timer when it goes high). And it seems to work just fine! Any experiences or comments to such an approach? (And yes, it's just a personal hobby project).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For a one-of your approach will work just fine.
But be aware of changes over temperature, Vcc to the Tiny etc.

Plons

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tricia, and Ulyana. You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Keep in mind that a digital input may chatter with slow moving analog inputs. Be sure to simply ignore the input for a while after the first transition from a '0' to a '1'. Also, noise from the comparator will tend to mess with the trigger point and degrade your accuracy slightly. You might try using the A/D converter and a timer. Successive passes would get you the time constant and thus the C, given a fixed R or I.

"It's easier to ask forgiveness than it is to get permission" - Admiral "Amazing" Grace Hopper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Originally I used the comparator to determine when the threshold is reached. A voltage divider to one comparator input, and the capacitor to the other. Worked quite nicely.

I would really like a little capacitance meter, can I get a copy of your source file, to see how to do it? also what language do you use?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your answers. According to the datasheet the low/high threshold is a nice linear function of the supply voltage. Which is exactly what I want, since the capacitor is charged from an output pin. The very limited number of test I have carried out look promising.

I have considered the the ADC approach, and might give it a try.

ph0rkeh: I'm working with a home brew soil moisture sensor (the dielectric constant varies quite a lot with moisture; a very well known principle), and I don't need very high accuracy. Hence a very simple, straight forward approach. I program in C (avr-gcc). I don't have the code here right now (can post the relevant section later), but the principle is very simple:

- A C and R is connected in series, with one R-pin to an output pin, the R/C junction to a comparator input, and the the other C-pin to ground.
- Be sure to keep the output low for a period of time so that the C will be discharged.
- Set the output pin high, and at the same time start the timer.
- Poll the comparator in a loop and stop the timer when the comparator flips.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here's (the very simple) code for charging and reading the cap, using the comparator. The RC is powered by PB3 which of course is an output. The comparator +ve input is connected to the C, whereas the -ve input is connected to a voltage divider. Since the voltage divider is powered from the same pin as the RC (PB3), I have inserted a couple of NOPs to avoid false comparator readings before the C starts to charge.

uint8_t sample(void) {
	TCNT0 = 0;	         // reset counter
	TCCR0B = (1<<CS01);	// Start counter
	PORTB |= (1<<PB3);	 // start charging
	asm volatile("nop");
	asm volatile("nop");
	while(!(ACSR & (1<<ACO))); // wait for comparator flip
	TCCR0B = 0;	         // stop counter
	PORTB &= ~(1<<PB3);	 // discharge 
	return(TCNT0);
}