Force-sensitive resistors (FSR) are remarkable electrical components and are proper unsung heroes in the Makers World. Name me a better variable touch input device that has basically no moving parts, all whilst being available in such a tiny form factor. They are perfect for detecting touch, and qualitatively measuring rate and relative changes in force/pressure.

Transcript

Hey gang! Tim here at Core Electronics and I'm controlling these GlowBit LEDs with the pressure of my fingertips and it's all thanks to this Force Sensitive Resistor (FSR). Today I'm going to show you exactly how to use an FSR with a Raspberry Pi single board computer. First, I'm going to set one up to act as a simple off or on switch, then I'm going to expand on it so we can measure analog signals. So, let's crack on in!

Both Force Sensitive Resistors (FSR) are variable touch devices that last a long time, have no moving parts, and come in all different shapes and sizes. The harder you press them, the less electrical resistance they have. This Square FSR I have right here can sense weight anywhere in the range of 100 grams to 10 kilograms. So, how do they do this? Simple answer is layers. Every FSR is made up of three layers: a black conductive film layer, an adhesive layer keeping it all together, and an active area layer which is usually a flexible PCB. Zooming in close, you're going to be able to see that the two sides of the traces on the active area do not directly connect to each other. Pressing the pad pushes the traces into that conductive film which lowers the resistance between the terminals. When no pressure is applied, this particular Square FSR has over 1 million ohms of resistance. When pressed hard, the resistance will decrease to below one kilo ohm.

Importantly to note, these FSR components are not a replacement for load cells or strain gauges. FSR components are never going to be suitable for precision Force measurements due to them being highly non-linear and just not accurate enough. On the table before me is everything that you're going to need. I'm using a Raspberry Pi 4 model B with this Square FSR. We will assume that you know how to run a Raspberry Pi as a desktop computer.

We're going to be using a breadboard, a 10K resistor, and a couple of jumper wires to pull analog readings from an FSR. To show this analog response, I also have this glow bit stick. I've soldered three in a row just to make it a little bit more interesting.

To start, set up your Raspberry Pi as a desktop computer. With the Raspberry Pi unpowered, let's now connect up the FSR. Start by plugging in your FSR into the breadboard. These FSR components are quite delicate on the connector side, so be gentle when doing so. Then set up a blue, black, and red wire with a 10 kilo ohm resistor in the arrangement. The red jumper wire connects to a 5 volt pin of the Raspberry Pi, the black jumper wire connects to a ground pin, and the blue jumper wire connects to the gpio4 pin on the Raspberry Pi. This will act as our input and we'll read the digital state of this pin, which will allow us to detect pressure on top of the FSR.

Double check your connections and then power up your Raspberry Pi by plugging in that USBC power supply. Once your Raspberry Pi is booted up, click on the application menu and on the top left of the screen, hover over the programming tab and then select Thonny IDE. In the simple setup section of the article, find the example script, copy it, and paste it as a new script into Thonny IDE. Then save your script. Check the article for additional resources if you need. The links are down below.

It's simple. FSRsetup.py with that done, smash that run button. Now, whenever you press this pad, it's going to respond with "Under Pressure" written to the Shell. This open source script can be readily changed, providing you with a fantastic rugged button that can control almost anything.

I'll show you something else. I'm going to connect this LED in series right here, and now when I press the pad, the LED is going to pop off. Let's jump into the script and take a look at what's going on in the back room. It all starts by importing only the packages that we need; in this case, the GPIO and time. Then we set the GPIO mode to BCM. BCM just means that we can set pin 4 like so, and we're going to set pin 4 to be a GPIO input. This is our blue wire, and this is the wire that's sensing whether this pad gets pressed or not. When this pad gets pressed, the voltage goes high and it gets recognized by the Raspberry Pi.

We'll keep going down a little bit further. This variable right here is just to determine whether the pressure is being applied or not, and it starts a while true loop. It's gonna take a reading from the input; currently it's not being touched, so the input going into pin 4 is low. The next line states that if the last reading was low and this one is high, that means the pressure pad is being pushed. We then want the shell to print "Under Pressure", which is what happens right here. This line here is there just to update the previous input variables, so that way we can avoid spamming our shell with way too many messages. And for the same reason, we have a slight pause here.

We'll run it again. Nice! Now, on and off switching is great, but let's take this guide up a notch and unlock the true analog potential of FSR components. For this next section of the guide, we're going to need to install some extra packages and change. the SCL pin on the ADC module

Ensure I²C is enabled on your Raspberry Pi by clicking the top left menu button, going down to Preferences and clicking on Raspberry Pi Configuration. Under the Interfaces tab, make sure that I²C is enabled, then simply press OK. We now need to install some Python packages. In the article, find the Software Setup section and copy these commands one by one into a terminal. Open up a terminal by pressing the black button on the top left of the screen and enter 'y' if ever prompted.

With that complete, all the software sides of things are sorted. Turning our attention back to our Hardware, let's now wire in our Adafruit ADC. You will need to solder that ADC board to do so, use a little bit of flux. Make sure you're all set up and use some Blu-Tac to make it even easier, or just press the board with the headers directly into a breadboard and that way your components are going to be perfectly aligned.

As we are going to be plugging new pieces into our Raspberry Pi's GPIO, make sure to disconnect power to your system. For our analog system here, we do not need to alter any of the current hardware setup on the breadboard. We will connect the blue input wire coming out from the breadboard to the A0 pin on the ADC module. This module can run at 5 volts, so connect up the module to the second 5 volt pin on the Raspberry Pi using a red jumper wire. Next, connect the module to a ground pin on the Raspberry Pi using a black jumper wire.

Now for the I²C bus, connect the SDA (serial data pin) on the Raspberry Pi to the SDA pin on the ADC module. Finally, connect the SCL (serial clock pin) of the Raspberry Pi to the SCL pin on the ADC module.

The SCL pin on the ADC module, double check all your connections and once sure that all is correct, plug that USBC power supply back in and open up Thonny IDE just the same as before. Then copy and paste the next script from the full written up article into the coding area of Thonny IDE. You're going to find this next script in the "Reading Analog Data from Force Sensitive Resistor" section in the guide. You can also download all scripts utilized in this guide from a link found at the bottom of this written article. Save this as analog_fsrsetup.py and now put your system to the test by pressing the Big Green Run button. Now, whenever I press the pad it's going to respond with the raw ADC data as it measures this sense wire voltage. I do want better? Right click anywhere on the shell to enable the plotter. This is going to let you see a graph of the data stream being printed to the Shell. Maxed out drop.

Let's jump into the script and take a look at what's going on in the back room. Like before, we've imported all the necessary functionality to our script right here. We have time and we also have the library to let us run our ADS1015 ADC board. We create this variable here to represent the ADC board. This is just the standard syntax to set up this particular module in Python. Now here, I've chosen a gain of one. If you were measuring a significantly smaller voltage change, a higher gain value would be better. Then we create a loop that repeats forever. Then we want to read the values coming into the ADC from out A0 port. It's inside this while loop so each time it prints that value, it sleeps for 0.1 of a second and then it spits out the value again. Beautiful and simple. There's a lot we can do with it too. With an FSR component built into a case, you now have yourself a rugged analog input device. have a working setup

I did here is my 3D printed solution which definitely increases the durability of them whilst making it more fun and easier to press evenly down onto this pad. As you can see by the analog responses, we can get much smoother. The 3D printable files can be found at the bottom of the article.

Next, I just had to power some Hardware with my newfound analog control, so I settled on this bar of three inline globit LED sticks that increase relative to the force I put on the sensor pad. Here's the final setup with the code running: whenever I press down on the FSR, it's going to reduce light all the way up. If I only lightly tap on the FSR, we're going to get less and less and less LEDs glowing.

You're going to find the code needed to run this in the zip file found at the bottom of my full written up article. The code to make this setup is very similar to the previous scripts we've explored. I'm just going to highlight the important differences which are all focused on making this GlowBit stick function the way that we want. We have a great guide on globit link down below, so check that out if you need.

Here in the script, you can see the difference straight away. We are importing the globit libraries, then we create an LED variable. This way, the python script knows how many LEDs we have on our stick. In this variable, we can also choose the brightness. Here, I've chosen it to be 100. Then we create a graphing variable into which we enter the amount of LEDs we have hooked up and the range of values that the ADC can emit. The Min value that we can get from the ADC is zero and the max value is 247. Here, I've just written 250. We also get the option to choose a color map. Here, I've chosen rainbow because I think that's the most exciting one, but for instance, we could just remove that completely, save it, run it, and we'll have a working setup.

Get a whole different range of colors to update the graph constantly in this one dimensionality. We have this graph and values. The graph is referring back to here and values is referring to all the numbers from the ADC. So, each time this wild true loop loops, we're going to update this glue bit module and that's where the FSR comes in. All different types and sizes, but what I have created here is going to work for all of them.

I hope this has inspired some creative ideas, be it a midi pad, sleep sensors, or hidden button controls. In the end of the day, these are just such cool input devices. We are full-time makers and we're always happy to help, so until next time, stay cozy.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.