I am trying to get values from a force sensitive resistor with Raspberry Pi. I saw an example using Arduino such as this http://learn.adafruit.com/force-sensitive-resistor-fsr/using-an-fsr and I can get sensor values from a photocell using the code below. Now I am trying to use the same concept to apply to FSR.

#!/usr/bin/env python

# Example for RC timing reading for Raspberry Pi
# Must be used with GPIO 0.3.1a or later - earlier verions
# are not fast enough!

import RPi.GPIO as GPIO, time, os


def RCtime (RCpin):
    reading = 0
    GPIO.setup(RCpin, GPIO.OUT)
    GPIO.output(RCpin, GPIO.LOW)

    GPIO.setup(RCpin, GPIO.IN)
    # This takes about 1 millisecond per loop cycle
    while (GPIO.input(RCpin) == GPIO.LOW):
        reading += 1
    return reading

while True:
    print RCtime(27) # Read RC timing using pin #27

I tried this code but it gave me some weird numbers. When I press on the FSR, it gave me some values from 10k to 100k. when i continue pressing it, it will return me with 0 after showing a large value for once or twice.

How can i interpret this? Or what is the proper way of doing it?


The Raspberry Pi does not have an analog input option. For that you'll need a analog or Analog->Digital capable expansionboard. For example the Gertboard

| improve this answer | |
  • The RC timer the OP suggests is also a form of analog to digital converter. It measures a resistance by timing how long it takes for a capacitor to charge or discharge through a resistor. This is explained further down on the Adafruit page linked in the question. The timing method with counting loops will not be reliable on a multitasking OS, so a dedicated ADC is still a good suggestion. – Frepa Jan 28 '15 at 15:25

I have written up the wiring and have an example Python script at: http://acaird.github.io/computers/2015/01/07/raspberry-pi-fsr

I hope that helps.

| improve this answer | |
  • Hello and welcome! I hate to break this to you, but at StackExchange answers that are useful without following any links are highly recommended. While the answer "there" seems to be very useful, what about a broken link in a year or two? – Ghanima Jan 27 '15 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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