Raspberry Pi Pico - Force Sensor

This guide shows you how to use a force sensor with a Raspberry Pi Pico. We will learn:

Raspberry Pi Pico and force sensor

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Force Sensor
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Force Sensor

Force sensor pinout

The force sensor, also known as a force sensing resistor, force sensitive resistor, or just FSR, changes its resistance when pressure is applied.

  • Low cost and easy to use.
  • Good at detecting physical pressure or compression.
  • Not good for measuring weight in pounds.

The force sensor is found in electronic drums, mobile phones, handheld gaming devices, and various other small electronic devices.

Pinout

A force sensor comes with two pins. Since it functions as a resistor, it is not necessary to distinguish between these pins. They are the same.

How It Works

The force sensor works like a resistor that adjusts its resistance depending on how strongly it is pressed. If you press harder, the resistance between the two ends decreases.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico Force

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-force-sensor """ from machine import ADC, Pin import time # Define the pin connected to the FSR force sensor FORCE_SENSOR_PIN = 28 # The Raspberry Pi Pico pin GP28 (ADC2) connected to the force sensor # Initialize ADC on the specified pin force_sensor = ADC(Pin(FORCE_SENSOR_PIN)) # Main loop while True: analog_reading = force_sensor.read_u16() # Read the raw analog value (0-65535) analog_reading = analog_reading // 64 # Scale it down to 0-1023 range to match Arduino print("Force sensor reading = ", analog_reading) # Print the raw analog reading if analog_reading < 6553: # from 0 to 6552 print(" -> no pressure") elif analog_reading < 13107: # from 6553 to 13106 print(" -> light touch") elif analog_reading < 32767: # from 13107 to 32766 print(" -> light squeeze") elif analog_reading < 52428: # from 32767 to 52427 print(" -> medium squeeze") else: # from 52428 to 65535 print(" -> big squeeze") time.sleep(1) # Delay for 1000 milliseconds

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the force sensor to the Raspberry Pi Pico according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Press on the force sensor.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Force sensor reading = 0 -> no pressure Force sensor reading = 0 -> no pressure Force sensor reading = 8461 -> light touch Force sensor reading = 9420 -> light touch Force sensor reading = 25236 -> light squeeze Force sensor reading = 26970 -> light squeeze Force sensor reading = 38873 -> medium squeeze Force sensor reading = 50677 -> medium squeeze Force sensor reading = 59065 -> big squeeze Force sensor reading = 63259 -> big squeeze Force sensor reading = 0 -> no pressure Force sensor reading = 0 -> no pressure
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

Please note that the value from the sensor may vary. It is recommended to calibrate for each force level.

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!