Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you. Additionally, some links direct to products from our own brand, DIYables .
About Force Sensor
The force sensor is also known as force sensing resistor, force sensitive resistor, or just FSR. The force sensor is basically a resistor that changes its resistive value depending on how much it has been pressed. The force sensor is:
Low-cost and easy-to-use.
Good at detecting physical pressure, squeeze.
Not good at finding how many pounds of weight they have on them.
The force sensor is used in electronic drums, mobile phones, handheld gaming devices and many more portable electronics.
Pinout
A force sensor has two pins. Since it is a kind of resistor, we do NOT need to distinguish these pins. They are symmetric.
How It Works
The force sensor is basically a resistor that changes its resistance depending on how much it has been pressed. The harder you press on the sensor, the lower the resistance between the two terminals will be.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
How To Program For Force Sensor
Arduino Uno's pin A0 to A5 can work as the analog input. The analog input pin converts the voltage (between 0v and VCC) into integer values (between 0 and 1023), called ADC value or analog value.
By connecting a pin of the force sensor to an analog input pin, we can read the analog value from the pin by using analogRead() function, and then we can know how much it has been pressed.
Arduino Code
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-force-sensor */#define FORCE_SENSOR_PIN A0 // the FSR and 10K pulldown are connected to A0voidsetup() {Serial.begin(9600);}voidloop() {int analogReading = analogRead(FORCE_SENSOR_PIN);Serial.print("Force sensor reading = ");Serial.print(analogReading); // print the raw analog readingif (analogReading < 10) // from 0 to 9Serial.println(" -> no pressure");elseif (analogReading < 200) // from 10 to 199Serial.println(" -> light touch");elseif (analogReading < 500) // from 200 to 499Serial.println(" -> light squeeze");elseif (analogReading < 800) // from 500 to 799Serial.println(" -> medium squeeze");else// from 800 to 1023Serial.println(" -> big squeeze");delay(1000);}
Quick Steps
Copy the above code and open with Arduino IDE
Click Upload button on Arduino IDE to upload code to Arduino
Press the force sensor
See the result on Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Force sensor reading = 0 -> no pressure
Force sensor reading = 0 -> no pressure
Force sensor reading = 132 -> light touch
Force sensor reading = 147 -> light touch
Force sensor reading = 394 -> light squeeze
Force sensor reading = 421 -> light squeeze
Force sensor reading = 607 -> medium squeeze
Force sensor reading = 791 -> medium squeeze
Force sensor reading = 921 -> big squeeze
Force sensor reading = 987 -> big squeeze
Force sensor reading = 0 -> no pressure
Force sensor reading = 0 -> no pressure
Ln 11, Col 1
Arduino Uno on COM15
2
Video Tutorial
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
The below video demo uses the below code. Note that the video shows Arduino Uno R4, but it works identically for Arduino Uno R3:
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-force-sensor */#include <Wire.h>#include <Adafruit_GFX.h>#include <Adafruit_SSD1306.h>// OLED display configuration#define SCREEN_WIDTH 128#define SCREEN_HEIGHT 64#define OLED_RESET -1Adafruit_SSD1306display( SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);// Force-sensitive resistor input#define FORCE_SENSOR_PIN A0// LED pins used to indicate force levelconstint ledPins[8] = { 2, 3, 4, 5, 6, 7, 8, 9};// Calibrated force sensor rangeconstint FORCE_MIN = 0;constint FORCE_MAX = 1000;// Exponential moving average filterfloat smoothValue = 0;constfloat alpha = 0.3;// Initialize hardware and displayvoidsetup() {Serial.begin(115200);// Configure LED outputs and turn all LEDs off.for (int i = 0; i < 8; i++) {pinMode(ledPins[i], OUTPUT);digitalWrite(ledPins[i], LOW); }// Initialize the OLED using I2C communication.if (!display.begin( SSD1306_SWITCHCAPVCC, 0x3C )) {Serial.println("OLED failed!");// Stop execution if the OLED cannot be initialized.while (1); }display.clearDisplay();display.setTextColor(SSD1306_WHITE);// Display startup screen.display.setTextSize(2);display.setCursor(20, 25);display.println("DIYables");display.display();delay(1500);// Initialize the filter with the first sensor reading// to prevent an incorrect initial value. smoothValue = analogRead(FORCE_SENSOR_PIN);}voidloop() {// Read the current FSR value.int rawValue = analogRead(FORCE_SENSOR_PIN);// Apply exponential moving average filtering// to reduce sensor noise and fluctuations. smoothValue = alpha * rawValue + (1.0 - alpha) * smoothValue;int forceValue = (int)smoothValue;// Convert the sensor value into 8 force levels.int level = map( forceValue, FORCE_MIN, FORCE_MAX, 0, 8 ); level = constrain(level, 0, 8);// Turn on LEDs progressively according to the force level.for (int i = 0; i < 8; i++) {if (i < level) {digitalWrite(ledPins[i], HIGH); }else {digitalWrite(ledPins[i], LOW); } }// Output sensor data for monitoring and calibration.Serial.print("FSR = ");Serial.print(forceValue);Serial.print(" | Level = ");Serial.println(level);// Refresh OLED display.display.clearDisplay();// Display title.display.setTextSize(2);display.setCursor(50, 0);display.println("FSR");// Configure the force indicator as a battery-style bar.int barX = 10;int barY = 25;int barWidth = 108;int barHeight = 18;// Draw the outer bar.display.drawRect( barX, barY, barWidth, barHeight, SSD1306_WHITE );// Draw the battery terminal.display.fillRect( barX + barWidth, barY + 5, 4, 8, SSD1306_WHITE );// Calculate the filled portion based on the force level.int fillWidth = map( level, 0, 8, 0, barWidth - 4 );if (fillWidth > 0) {display.fillRect( barX + 2, barY + 2, fillWidth, barHeight - 4, SSD1306_WHITE ); }// Display current force level.display.setTextSize(1);display.setCursor(5, 50);display.print("LEVEL: ");display.print(level);display.print("/8");// Display project branding.display.setCursor(78, 50);display.print("DIYables");display.display();// Short delay for stable display updates.delay(30);}
You can share the link of this tutorial anywhere. Howerver, please do not copy the content to share on other websites. We took a lot of time and effort to create the content of this tutorial, please respect our work!