Force Sensitive Resistor Not Resisting?

Hello,
I’m trying to use a Force Sensitive Resistor as a button. I have it connected to an Analog Pin and I’m measuring its input value in the loop(). I have an if statement testing if its input is greater than 10, indicating that someone has lightly tapped on it. Inside the if statement, it prints the value it has received from the Analog Pin.

So, in theory, it would only print values higher than 10. However, seemingly randomly and very frequently, it is bypassing the if statement with a value of 0 and then printing that number.

const int fsrPin = 0;

void setup() {
  Serial.begin(9600);
  pinMode(fsrPin, INPUT);
}

void loop() {
  int fsrRead = analogRead(fsrPin);
  if(analogRead(fsrPin) >= 10){
      Serial.println(fsrRead);
   }
}

Attached is an visualization of the circuit.

Remove the "if" and simply print the values you get from analogRead() in a continuous stream, so you have a good idea of the range of values to expect. Then change "10" to something more reasonable.

Try this:

void loop() {
      Serial.println(analogRead(fsrPin));
      delay(100); //slow it down a bit
}

If you are actually getting "0" from analogRead() then you have a bad connection somewhere.

jremington: Remove the "if" and simply print the values you get from analogRead() in a continuous stream, so you have a good idea of the range of values to expect. Then change "10" to something more reasonable.

Try this:

void loop() {
      Serial.println(analogRead(fsrPin));
      delay(100); //slow it down a bit
}

If you are actually getting "0" from analogRead() then you have a bad connection somewhere.

After adding the delay and printing the analogRead() directly, I'm now noticing that I'm not getting any zeroes until I touch the sensor. After I touch it and get an actually correct reading, I start getting the zeroes printed again, however more infrequent this time... Hope that info helps.

Isn't the analog pin A0 instead of 0?

const int fsrPin = 0;

void setup() {
  Serial.begin(9600);
  pinMode(fsrPin, INPUT);
...
...
...

I tried it with A0, now I'm getting much higher numbers for my inputs, that are actually making sense. Also, there are no more zeroes. Unfortunately now I'm having another, rather indescribable problem.

I'll try to figure this out on my own, thanks for the help everybody.

You are ofcourse using a resistor with your FSR.
What value, and how is it all connected.

const int fsrPin = 0; // is the same as
const int fsrPin = A0; // because the compiler knows you mean A0 after an analogRead call.
But less confusing if you use A0 for analogue pins.

pinMode(fsrPin, INPUT); // can be removed. Pins are input by default.

An easier way is to use the FSR between A0 and ground, and use the internal pull up resistor with pinMode.

pinMode(fsrPin, INPUT_PULLUP); // FSR between pin and ground, no resistor

Leo…

I start getting the zeroes printed again, however more infrequent this time

Bad connection. If you are using a breadboard, rewire the circuit using different contacts and/or slightly larger wires.

If you intend this circuit to actually be used, with people poking at it constantly, then you need to get rid of the breadboard and solder all connections, possibly with strain relief. The FSR should be mounted on some supporting surface so the connections aren't flexed constantly.

Tried it out myself using both 0 and A0, and the analogRead does work properly with either, but the pinMode command with INPUT_PULLUP only works properly if I use A0, with just 0 it goes to the digital pin. (had to change to 2 / A2 to test that since digital pin 0 is used by the serial port).

Wawa: You are ofcourse using a resistor with your FSR. What value, and how is it all connected.

const int fsrPin = 0; // is the same as const int fsrPin = A0; // because the compiler knows you mean A0 after an analogRead call. But less confusing if you use A0 for analogue pins.

pinMode(fsrPin, INPUT); // can be removed. Pins are input by default.

An easier way is to use the FSR between A0 and ground, and use the internal pull up resistor with pinMode.

pinMode(fsrPin, INPUT_PULLUP); // FSR between pin and ground, no resistor

Leo..

The analog pins on an Uno are also digital pins and INPUT_PULLUP is a digital mode. Analog pin 0 and A0 being the same is an Arduino quirk.

What kind of resistance do you get when you put the sensor on a meter and press? Your voltage divider might need a different resistor.

david_2018: Tried it out myself using both 0 and A0, and the analogRead does work properly with either, but the pinMode command with INPUT_PULLUP only works properly if I use A0, with just 0 it goes to the digital pin. (had to change to 2 / A2 to test that since digital pin 0 is used by the serial port).

Good point. Something to watch out for. Karma for that. Never have these problems, because I always use A0. Leo..

Wawa: const int fsrPin = 0; // is the same as const int fsrPin = A0; // because the compiler knows you mean A0 after an analogRead call.

Unbelievable. This means that digitalRead(0) and analogRead(0) are actually reading from different pins, but digitalRead(14) and analogRead(0) are reading from the same pin!

I thought Arduino was supposed to be designed to be easy for beginners...