ESP32 MicroPython Force Sensor

This tutorial shows you how to use a force sensor with an ESP32 and MicroPython to detect the force or weight applied to it. We will learn:

ESP32 MicroPython and force sensor

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Force Sensor
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32

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 called a force sensing resistor (FSR), changes its resistance when pressure is applied.

  • Affordable and simple to use.
  • Great for detecting physical pressure or compression.
  • Not suitable for measuring weight in pounds.

You can find force sensors in electronic drums, mobile phones, handheld gaming devices, and many other small electronic 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

Imagine the force sensor as a special kind of knob that changes its resistance based on how hard you push it. The harder you press, the closer the two ends of the knob get, which means lower resistance.

Wiring Diagram

  • How to connect ESP32 and force sensor using breadboard
The wiring diagram between ESP32 MicroPython Force

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and force sensor

ESP32 MicroPython Code for Force Sensor

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-force-sensor """ from machine import ADC, Pin import time # Define the pin connected to the FSR force sensor FORCE_SENSOR_PIN = 36 # The ESP32 pin GPIO36 (ADC0) connected to the force sensor # Initialize ADC on the specified pin force_sensor = ADC(Pin(FORCE_SENSOR_PIN)) # Set the ADC width (resolution) to 12 bits force_sensor.width(ADC.WIDTH_12BIT) # Set the attenuation to 11 dB, allowing input range up to ~3.3V force_sensor.atten(ADC.ATTN_11DB) # Main loop while True: analog_reading = force_sensor.read() # Read the raw analog value (0-4095) # 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 set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the force sensor to the ESP32 according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • 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 = 529 -> light touch Force sensor reading = 588 -> light touch Force sensor reading = 1577 -> light squeeze Force sensor reading = 1685 -> light squeeze Force sensor reading = 2427 -> medium squeeze Force sensor reading = 3185 -> medium squeeze Force sensor reading = 3687 -> big squeeze Force sensor reading = 3950 -> big squeeze Force sensor reading = 0 -> no pressure Force sensor reading = 0 -> no pressure
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

Keep in mind that the sensor's values aren't always exactly the same. It's a good idea to adjust the sensor's settings for each specific force level you want to measure.

※ NOTE THAT:

This tutorial demonstrates how to use the adc.read() function to read values from an ADC (Analog-to-Digital Converter) connected to a force sensor. The ESP32's ADC is suitable for projects that do not require high precision. However, if your project needs accurate measurements, keep the following in mind:

  • The ESP32 ADC is not perfectly accurate and may require calibration for precise results. Each ESP32 board may vary slightly, so calibration is necessary for each individual board.
  • Calibration can be challenging, especially for beginners, and might not always yield the exact results you desire.

For projects requiring high precision, consider using an external ADC (e.g., ADS1115) with the ESP32 or opt for an Arduino, which has a more reliable ADC. If you still wish to calibrate the ESP32 ADC, refer to the ESP32 ADC Calibration Driver.

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!