I am using an FSR in my project to detect if someone has pressed a certain area of my product, using the analogRead() function. The force applied is then graded into bands by assessing the analogue value obtained.
At the same time, I would like to have some sort of check method to assess whether the sensor is connected to the pin it is connected to, without having to press the sensor first. Almost like a verification check to ensure the system is working as it should before shipping to a customer.
The following code works but I have to press the sensor first:
const int fsrPin = 2; // The digital input pin connected to one leg of the FSR
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(fsrPin, INPUT_PULLUP); // Set the pin as input with internal pull-up resistor
}
void loop() {
int fsrState = digitalRead(fsrPin); // Read the state of the FSR
if (fsrState == LOW) {
Serial.println("FSR is connected.");
} else {
Serial.println("FSR is not connected.");
}
delay(250); // Delay before checking again
Any ideas on how to do this without pressing the sensor would be greatly appreciated. Thanks in advance!!
Yes that is what I'm seeing, when the sensor is connected and not pressed the analog read is zero, thus the resistance turns out as infinity. Then when I press the resistance decreases as the analog value increases.
However when I disconnect the sensor from the read pin, both analog and resistance values are random as the pin is now floating.
I have the connection point for the FSR and resistor on an external custom PCB. So wiring to the Arduino is 5V, GND and to the analog in pin to read the sensor.
This setup will be the same in the final product so that sensor replacement (if required) is easy and done by just removing that PCB and connector. I suppose this will mean the pin will be floating as you say when the PCB is removed.
Thanks for your reply @jim-p
The code I attached only updates when I press down on the sensor. The image attached shows a snapshot of the serial monitor- it updates from not connected to connected if I press the sensor and activate it.
This line of the code (PULLUP) expects you to connect the FRS between pin and ground.
Without resistor, or maybe a 10k resistor between pin and 5volt if needed.
1k sounds too low.
You should get A/D readings around 512 with average pressure applied.
The value of the pull up resistor used (or no resistor) should bring it within that range.
A disconnected sensor will read close to 1023.
Leo..