Raspberry Pi Pressure Pad : Detect Pressure on a Specific Area
In this Raspberry Pi pressure pad tutorial, I will go through the steps of connecting a pressure pad to the Pi.
There are many uses for using a pressure pad such as detecting when someone sits down or places pressure on a specific part of an object. It is a pretty great sensor to make use of in IoT projects and more.
In this instructional exercise we just experience the means of snaring the cushion, so you can identify if enough weight has been connected or not. We will likewise experience strategies to get variable qualities by utilizing a capacitor or a simple to computerized converter (ADC).
The second structure in this instructional exercise makes utilization of a capacitor simply like in the LDR instructional exercise. Utilizing this technique we can gauge the opposition of the weight cushion. In the event that there is no weight, we will get no qualities. When we apply some weight, we will get values changing from high as 100,000+ directly down to 5,000.
The third structure that I make utilization of in this instructional exercise utilizes an ADC. Much the same as the capacitor by utilizing an ADC, we will have the capacity to get variable qualities from the weight cushion. Flawless in case you’re customizing something that accomplishes something at various weight focuses.
You can locate the full instructional exercise to set up a weight cushion directly underneath.
Below is the full list of equipment that you will need to complete this Raspberry Pi pressure pad tutorial.
MCP3008 or similar
The circuit for this weight cushion is shockingly basic. Right off the bat, I will address the parts that make up the circuits and for what reason we’re making utilization of them.
Remember that you won’t require every one of the parts, it just relies upon what you plan on doing.
Pressure Pad Sensor
The weight cushion is the most imperative piece of our circuit as it won’t work without it being used. I made utilization of a FlexiForce weight cushion sensor, however, you’re ready to utilize less expensive choices.
The FlexiForce sensor opposition will differ between close endless when you’re applying practically no weight, to under 25k ohms for when you’re applying a considerable measure of weight to it.
It’s especially similar to a light needy sensor, yet rather than the encompassing light, it utilizes coordinate weight.
The resistor is required in the entirety of our circuits aside from the capacitor circuit. This resistor will go about as a voltage divider and partitions the 3v3 between the weight cushion and the resistor. At the point when weight is connected it will give enough voltage to send our stick to high.
The following is the condition you can exercise what the voltage out to our GPIO stick will be. For instance, if our weight cushion is max opposition, it will be (1,000,000/(1,000,000 + 10,000,000)) * 3.3 = 0.3 volts. This voltage isn’t sufficient to send the stick to high.
On the other side, in case we’re applying a high measure of weight so the weight cushion opposition is just 50,000 then our condition will be (1,000,000/(1,000,000 + 50,000)) * 3.3 = 3.14 volts. This voltage is sufficient to send the stick to high.
To take in more about voltage dividers I prescribe looking at our guide on them.
Since the Raspberry Pi doesn’t have simple pins it will just ever be high or low. We can work around this by either utilizing a capacitor or simple to the advanced converter, see beneath.
A capacitor will enable us to get variable outcomes without the utilization of a simple to the advanced converter. The outcomes aren’t as precise yet at the same time staggeringly convenient as should be an obvious fluctuating measure of weight being connected to the weight cushion.
The capacitor will go about as a battery energizing while at the same time getting power and releasing when it’s full or when it quits accepting a charge. By utilizing this in arrangement with our weight cushion we can work out the obstruction of the weight cushion. Higher the opposition the less weight is being connected.
Analog to Digital Converter
An ADC enables us to associate simple sensors and gadgets to the Raspberry Pi. It includes a lot additionally wiring, however, will enable us to get a variable esteem as opposed to a straightforward on or off.
A variable esteem enables us to accomplish all the more, for example, checking how much weight is being connected and playing out an alternate capacity relying upon the outcome. Incredible on the off chance that you have to watch out for weight in particular regions.
I won’t go much into the ADC in this instructional exercise so on the off chance that you need to take in more at that point make sure to look at my instructional exercise on utilizing an ADC with the Raspberry Pi.
Circuit Diagram using a Resistor
The steps to setting up this circuit are straightforward and shouldn’t take you long at all. This circuit will give you a simple on or off result from the pressure pad.
- Connect one end of the pressure pad to 3v3.
- Place a resistor onto the breadboard and have one end go to GND and the other to pin 7.
- Lastly, have the resistor end that goes to pin 7 also go to the other end of the pressure pad.
The diagram below shows you how it should look.
Circuit Diagram using a capacitor
We can use a capacitor to measure the resistance of the pressure pad. This circuit is ideal if you need to know roughly how much pressure is being applied to the sensor.
- Connect one end of the pressure pad to 3v3.
- Place a capacitor onto the breadboard and have the negative end go to GND and the other to pin 7 and the pressure pad.
If you need more information, then please refer to the diagram below.
Circuit using an Analog to Digital Converter
The last circuit we’re looking at makes use of an analog to digital converter. This will allow you to get semi-accurate values from the pressure pad. We also make use of the resistor and use it as a voltage divider.
- VDD (Pin 16) wire this to 3.3V
- VREF (Pin 15) wire this to 3.3V
- AGND (Pin 14) wire this to ground
- CLK (Pin 13) wire this to GPIO11 (Pin 23/SCLK)
- DOUT (Pin 12) wire this to GPIO9 (Pin 21/MISO)
- DIN (Pin 11) wire this to GPIO10 (Pin 19/MOSI)
- CS (Pin 10) wire this to GPIO8 (Pin 24/CE0)
- DGND (Pin 9) wire this to GROUND
- Wire the pressure pad to 3v3
- Wire a resistor from GROUND to CH0
- Also wire CH0 to the pressure pad
If you require clarification on the steps below observe the diagram below.
Coding the Pressure Pad
Each circuit setup requires its own unique code. The simplest is using the resistor while the most complicated is to use an analog to digital converter.
Code using a Resistor
Much like the circuit, the code for the resistor is also easy since we’re only looking for when the pin goes to high.
In the code, we import our required packages, GPIO for interaction with the GPIO pins and time for pausing the script.
import RPi.GPIO as GPIO import time
Now we first set up the GPIO so that it runs in BCM mode and setup GPIO pin 4 to act as an input.
Next, we enter an infinite loop and read the GPIO pin. If it is high and the previous reading was low, then we print a message. We then pause the script slightly before repeating.
while True: #take a reading input = GPIO.input(4) #if the last reading was low and this one high, alert us if ((not prev_input) and input): print("Under Pressure") #update previous input prev_input = input #slight pause time.sleep(0.10)
You can get the full code for using a resistor right below.
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(4,GPIO.IN) #initialise a previous input variable to 0 (Assume no pressure applied) prev_input = 0 try: while True: #take a reading input = GPIO.input(4) #if the last reading was low and this one high, alert us if ((not prev_input) and input): print("Under Pressure") #update previous input prev_input = input #slight pause time.sleep(0.10) except KeyboardInterrupt: pass finally: GPIO.cleanup()
Code using a Capacitor
Much like resistor code this isn’t overly complex and is based on our LDR code that we made use of in a previous tutorial.
Firstly, we must insert all the packages we require for this to work correctly, time and GPIO.
import RPi.GPIO as GPIO import time
The rest of our code is pretty simple, we have a function called rc_time, and this takes one parameter. In this function, we wait until the pin goes to high and then return the count where “count” is the time it took to go high.
Once we have returned the count we have the pin to act as an output and set it to low, we then wait 100ms before switching it back to an input. It will then count until the pin goes high.
You can use the count value to determine how much pressure somebody is applying to the pressure pad. If the value is really high such as 50k+, then you’re applying little to no pressure. Lower than 10k means there is quite a bit of pressure on the pad.
The full code is right below, or you can find it over on our GitHub.
#!/usr/local/bin/python import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) #define the pin that goes to the circuit pin_to_circuit = 4 def rc_time (pin_to_circuit): count = 0 #Output on the pin for GPIO.setup(pin_to_circuit, GPIO.OUT) GPIO.output(pin_to_circuit, GPIO.LOW) time.sleep(0.1) #Change the pin back to input GPIO.setup(pin_to_circuit, GPIO.IN) #Count until the pin goes high while (GPIO.input(pin_to_circuit) == GPIO.LOW): count += 1 return count #Catch when script is interrupted, cleanup correctly try: # Main loop while True: print(rc_time(pin_to_circuit)) except KeyboardInterrupt: pass finally: GPIO.cleanup()
Code using an Analog to Digital Converter (ADC)
The analog to digital converter code is pretty straightforward, the tricky part is receiving the value from the MCP3008 chip. If you want to know more about this code, check out on our analog to digital converter tutorial.
Below is the full code for using an MCP3008, you can also get it off our GitHub.
#!/usr/bin/python import spidev import time #Define Variables delay = 0.5 pad_channel = 0 #Create SPI spi = spidev.SpiDev() spi.open(0, 0) #spi.max_speed_hz=1000000 def readadc(adcnum): # read SPI data from the MCP3008, 8 channels in total if adcnum > 7 or adcnum < 0: return -1 r = spi.xfer2([1, 8 + adcnum << 4, 0]) data = ((r & 3) << 8) + r return data try: while True: pad_value = readadc(pad_channel) print("---------------------------------------") print("Pressure Pad Value: %d" % pad_value) time.sleep(delay) except KeyboardInterrupt: pass
You can also make use of something like Raspberry Pi Cayenne if you don’t wish to do any coding. Check out my tutorial on using that if you want to learn more.