i'm dealing with analog sensors. I have an Arduino Lilypad Simple Board with only 4 analog input. Now, i need two more sensors and i've found on the net this schematics: enter image description here

But i don't understand if it could add delays on my project because i'm controlling audio-video stuffs and any kind of delay has to be avoided.

Then, on the link i read this:

It is possible to calculate the actual resistance from the reading but unfortunately, variations in the IDE and arduino board will make it inconsistant. Be aware of that if you change IDE versions of OS's, or use a 3.3V arduino instead of 5V, or change from a 16mhz Arduino to a 8Mhz one (like a lilypad) there may be differences due to how long it takes to read the value of a pin. Usually that isn't a big deal but it can make your project hard to debug if you aren't expecting it!

I'm not a big expert of arduino and i don't understand what do i have to change for lilypad.

Any advice could be appreciated!


2 Answers 2


This will most certainly add delays as you are polling the pin in a blocking loop

while (digitalRead(RCpin) == LOW) { // count how long it takes to rise up to HIGH
reading++;      // increment to keep track of time 

if (reading == 30000) {
  // if we got this far, the resistance is so high
  // its likely that nothing is connected! 
  break;           // leave the loop

Assuming that your compiler can optimize the code extremely efficiently this loop would take something like 4 lines of code to execute since you have to read the pin, then compare it to a value, then branch based on the outcome (I would be very impressed if you could get this few instructions). Further assume that each one of those instructions takes only 1 clock cycle to execute (this is also probably going to take more, but it helps to bound the problem). This routine could take at most:

\$MaxRoutineTime = LoopIterations \times \frac{Instructions}{LoopIteration} \times \frac{Seconds}{Instructions}\$

\$MaxRoutineTime = 30,000 \space Iterations\times \frac{4 \space Instructions} {LoopIteration} \times \frac{Seconds}{8,000,000 \space Instructions}\$

\$MaxRoutineTime = 15 \space mS\$

but I assume it will take a little more than that because of the aforementioned allowances.

The reason it does not add delays when using an ADC is because the peripheral can be setup to generate interrupts and you will only be notified when the ADC reading is complete. The time it takes the ADC to complete a measurement is a finite number of clock cycles, so the app note you're referencing is pointing out that if you slow your clock speed, though the ADC will still take the same number of clock cycles to complete a measurement, your measurement will take longer because the clock is slower.


At first glance from your picture, combined with the fact that you mentioned audio, I thought you were measuring a microphone input. However, it appears that you're just using a Force Sensitive Resistor (FSR) which is just a pressure sensor. If you don't need to know the amount of pressure, only that it was pushed, you don't have to go through all the trouble of finding the exact reading. You can simply use any interrupt-generating, digital input if you pick the correct resistor value (in place of the capacitor). You will simply set a digital pin to generate interrupts on rising edges and pick a resistor that will give you a state change (low/high) with the desired amount of force for your touch. Then you'll know each time the FSR was pushed and can handle it in an un-blocking fashioner, introducing the least latency possible.

  • 1
    \$\begingroup\$ "at least" or "at most"? At most ( 30000 × n ) / 8Mhz, where n is the number of CPU cycles spent in the loop (I think the order of n=4 cycles. Lowering the cap speeds conversion, but lowers accuracy. \$\endgroup\$
    – jippie
    Commented Jul 24, 2012 at 7:28
  • \$\begingroup\$ @jippie - Good catch! A subtle, but important difference. Edited accordingly. \$\endgroup\$
    – Joel B
    Commented Jul 24, 2012 at 14:01

I never worked with Arduino, but most microcontrollers have interrupt pins, so Arduino must have too. If you use a interrupt pin to detect that the capacitor has charged up to to the threshold level, then you will not have delays and your program could run normally while the measurement takes place. You just need to use a pin that allows interrupt.

  1. Assert the pin low
  2. Make sure the pin is low enough time to discharge the capacitor
  3. Enable interrupt and reset a timer
  4. Make the pin an input (the capacitor will start charging)
  5. Execute your code
  6. When the interrupt occurs read the timer value (this is your measured value) and assert the pin low
  • 1
    \$\begingroup\$ +1: that's the way to measure RC times using a digital input. I like the pints particularly. \$\endgroup\$
    – stevenvh
    Commented Jul 23, 2012 at 13:10
  • \$\begingroup\$ @stevenvh Corrected. Make an error, copy and paste and that's how it's done :-) \$\endgroup\$ Commented Jul 23, 2012 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.