Arduino MicroPython Force Sensor

This guide shows you how to use a force sensor with an Arduino and MicroPython to detect the force or weight applied. You will learn:

Arduino MicroPython and force sensor

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Force Sensor
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

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

A force sensor, also known as a force-sensing resistor (FSR), changes its resistance when you apply pressure.

  • Inexpensive and easy to use
  • Good for detecting physical pressure or squeezing
  • Not meant for measuring weight in pounds

You can find force sensors in electronic drums, mobile phones, handheld gaming devices, and many other small gadgets.

Pinout

A force sensor has only two wires. Because it works like a resistor, it doesn't matter which wire goes where. They're both the same.

How It Works

Think of the force sensor like a special button that changes its resistance based on how hard you press it. The harder you push, the closer the two ends get, resulting in lower resistance.

Wiring Diagram

The wiring diagram between Arduino MicroPython Force

This image is created using Fritzing. Click to enlarge image

Arduino MicroPython Code for Force Sensor

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-force-sensor """ from machine import ADC, Pin import time # Define the pin connected to the FSR force sensor FORCE_SENSOR_PIN = 'A0' # The Arduino Giga WiFi pin A0 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() >> 4 # Read the 16-bit analog value from the force sensor and convert back to 12-bit ADC # Determine the pressure description based on the analog reading if analog_reading < 409: # from 0 to 408 pressure_description = " -> no pressure" elif analog_reading < 819: # from 409 to 818 pressure_description = " -> light touch" elif analog_reading < 2047: # from 819 to 2046 pressure_description = " -> light squeeze" elif analog_reading < 3276: # from 2047 to 3275 pressure_description = " -> medium squeeze" else: # from 3276 to 4095 pressure_description = " -> big squeeze" # Print the entire message in one line print(f"Force sensor reading = {analog_reading}{pressure_description}") time.sleep(0.5) # Delay for 500 milliseconds

Detailed Instructions

Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the force sensor to the Arduino according to the provided diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • 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 = 14 -> no pressure Force sensor reading = 16 -> no pressure Force sensor reading = 21 -> no pressure Force sensor reading = 527 -> light touch Force sensor reading = 586 -> light touch Force sensor reading = 1576 -> light squeeze Force sensor reading = 1683 -> light squeeze Force sensor reading = 2428 -> medium squeeze Force sensor reading = 3184 -> medium squeeze Force sensor reading = 3688 -> big squeeze Force sensor reading = 3951 -> big squeeze Force sensor reading = 0 -> no pressure Force sensor reading = 0 -> no pressure
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

Remember that the sensor's values can change slightly. It's a good idea to adjust the sensor's settings for each specific force level you want to measure.

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!