1

I am building a simple resistor / capacitor in series (RC) circuit to measure resistance with my Raspberry Pi A+.

RC Design

Enter image description here

This design is similar to the one used in this tutorial as suggested in this post.

This circuit works by measuring the resistance of R1 by measuring the time required to charge C1. I do understand that Python is not a great language for precision timing, but this doesn't need to be very precise. It's mostly a project to learn circuit analysis with the Raspberry Pi. My Python script I am using for this math is the following:

# Time how long it takes for C1 to charge through R1
GPIO.setup (8, GPIO.IN);GPIO.output (10, 1)
start = time.time ()
while GPIO.input (8) == 0: False
end = time.time ()

# Divide duration by capacitance to get resistance
capacitance = 0.47 * 0.000001 # 0.47 µF
duration = end - start
resistance = duration / capacitance

resistance *= 1.85938    # <===== THE HORRIBLE MYSTERY COEFFICIENT

print ('resistance =', round (resistance), 'ohms')


Math Error - Mystery Coeficcient

The above setup outputs (mostly) accurate numbers for the value of R1. The problem is the unexplained coefficient ~1.85938. Some possible error sources I've considered include:

  • Internal resistance from the GPIO itself (with the 3.3 V, 16 mA max output per pin),
  • Problems due to inaccuracies in my components themselves (the resistors have tolerances of 5%),
  • A flaw in the universe of the kind that make 1 = 2 and never-ending chocolate possible,
  • or some combination of the above.

Why would RC circuits built with the GPIO be off by a factor of ~1.85938?

Robin
  • 171
  • 2
  • 11

2 Answers2

1

This is not really a Pi question.

What is the tolerance of the capacitor; most have very wide tolerances, particularly electrolytics (which this appears to be). They are designed for filtering, not precision measurement.

What is the trigger voltage? This will vary over a range. You can set the GPIO into Schmitt trigger mode to get a more defined trigger point.

Finally this kind of circuit is a kludge, designed to get around the lack of analogue input. It should only be relied on to give relative values.

Milliways
  • 62,573
  • 32
  • 113
  • 225
1

I'm not quite sure what you expect.

I'm not familiar with the A+.

Which gpios are you actually using? Is that photo a mirror image?

Gpios 0-8 have internal pull-ups to 3V3 of approximately 50k enabled at boot.

The other gpios on the expansion header have internal pull-downs to ground of approximately 50k enabled at boot.

See http://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf page 102.

Pins 3 and 5 have hard-wired 1k8 pull-ups to 3V3.

Anything under 1V at an input gpio may be seen as low (0). Anything over 2V at an input gpio may be seen as high (1).

See http://www.mosaic-industries.com/embedded-systems/microcontroller-projects/raspberry-pi/gpio-pin-electrical-specifications

joan
  • 71,852
  • 5
  • 76
  • 108