Introduction:
This tutorial will show how to detect the fullness of a trash can by using two force sensitive resistor (FSR), and a RGB LED will change its color according to the changes. Basically, FSR is a resistor which can change its resistance depending on how much pressure it receives, and it is very sensitive to pressure change.
In our project, we are going to stick two FSRs on the inner wall of a trash can, one on the half and the other on the top. If the trash can is less than half-full, the RGB will be green. If the trash can is half-full, the trashes will touch the FSR on the middle and it can capture the change of the pressure. Then the RGB will change to blue color. If the trash can is full, trashes will touch the other FSR on the top, and the RGB will change to red color.
However, for this tutorial, we are just using the Arduino board to connect all the parts together and use our fingers to touch the FSRs instead of the trashes.
Parts List:
- Arduino board
- Breadboard
- Wires
- Two Square Force Sensitive Resistors *
- One RGB LED
- Optional: 9 volts Battery (if you need it to work without connecting to the computer)
* Note: In this case, we are going to use the Square FSR from Adafruit (Product ID: 1075). There are different shapes of FSRs available on Adafruit and you can choose the shape that fits your project best.
Wiring the sensors:
Let’s connect one FSR first. Connect one end of the pin to 5 volts, and the other end to Analog 0 pin (A0). Then connect a 10k ohms resistors from A0 to ground, because we want to protect our sensor from burning down. Do the same thing to the second FSR, but change the Analog 0 to Analog 1.
Secondly, connect the RGB LED to the board. There are four pins with different length on RGB. The longest pin is common, which should be connected to ground. Based on the longest pin, the color order is RED, COMMON, GREEN and BLUE.
Connect the common pin to ground, and connect the red, green and blue pin to digital pin 2,3 and 4. Between each digital pin and RGB pin, connect a 330 ohms resistor.
Circuit Diagram :
*In the diagram, the FSRs are circle, but in our project, we use the square FSR. They are basically the same things with different shapes.
Code:
Now let’s start to run the code. Firstly, we declare some variables to identify the pins we are connecting to. Make sure that your sensors are connected to the same pins as in your code, because otherwise it won’t work. Then we declare two variables to store values from the analog pins connected to our FSRs.
In our main code, we set the speed of serial monitor and the output of the RGB pins at first. Then we read the value from FSRs and print them on the serial monitor, so that we can check if the sensors are working properly. We use three “if” statements to set three different conditions:
(1) When both of the FSRs are not being pressed, the RGB will only output the green light through the green pin.
(2) When the first FSR is being pressed and the second is not, the RGB will output the blue light through the blue pin.
(3) When both of the FSRs are being pressed, the RGB will output the red light through the red pin.
We loop the code every second. Now, if you load the code and open your serial monitor, you will able to see the value from the two FSRs every second, and the RGB color will change according to which FSR you are pressing. You can change the delay time in the delay() code, so if you want your sensor to be more responsive to pressure change you can make the number smaller.
*My Arduino software is in Chinese because it adjusts to the language in my computer. Don’t worry about it.
The complete code is included below. Have fun playing with your Arduino!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
/* Connecting two FSRs and a RGB LED. For the FSR: Connect one end of FSR to 5V, the other end to Analog 0. Then connect one end of a 10K resistor from Analog 0 to ground Connect LED from pin 11 through a resistor to ground For the RGB: An RGB LED is actually three LEDs (red, green, and blue) in one package. When you run them at different brightnesses, the red, green and blue mix to form new colors. Starting at the flattened edge of the flange on the LED, the pins are ordered RED, COMMON, GREEN, BLUE. Connect RED to a 330 Ohm resistor. Connect the other end of the resistor to Arduino digital pin 2. Connect COMMON pin to GND. Connect GREEN to a 330 Ohm resistor. Connect the other end of the resistor to Arduino digital pin 3. Connect BLUE to a 330 Ohm resistor. Connect the other end of the resistor to Arduino digital pin 4. Created 11/14/2017 By Huiling He */ const int fsrAnalogPin1 = 1; // connect FSR 1 to analog 1 const int fsrAnalogPin2 = 2; // connect FSR 2 to analog 2 const int RED_PIN = 2; // connect the red pin to pin 2 const int GREEN_PIN = 3; // connect the green pin to pin 3 const int BLUE_PIN = 4; // connect the blue pin to pin 4 int fsrReading1; // the analog reading from FSR 1 resistor divider int fsrReading2; // the analog reading from FSR 2 resistor divider void setup() { Serial.begin(9600); // set the speed of the serial monitor pinMode(RED_PIN, OUTPUT); // set the output of the red pin pinMode(GREEN_PIN, OUTPUT); // set the output of the green pin pinMode(BLUE_PIN, OUTPUT); // set the output of the blue pin } void loop() { fsrReading1 = analogRead(fsrAnalogPin1); // read the value of FSR 1 fsrReading2 = analogRead(fsrAnalogPin2); // read the value of FSR 2 Serial.print("FSR1 = "); Serial.println(fsrReading1); // print the value of FSR 1 on serial monitor Serial.print("FSR2 = "); Serial.println(fsrReading2); // print the value of FSR 2 on serial monitor if ((fsrReading1==0) & (fsrReading2==0)) // if both FSR 1 and FSR 2 are not pressed { digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, HIGH); digitalWrite(BLUE_PIN, LOW); // the RGB will turn on green light } else if ((fsrReading1>0) & (fsrReading2==0)) // if FSR 1 is pressed and FSR 2 is not { digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, LOW); digitalWrite(BLUE_PIN, HIGH); // the RGB will turn on blue light } else if ((fsrReading1>0) & (fsrReading2>0)) // if both FSR 1 and FSR 2 are pressed { digitalWrite(RED_PIN, HIGH); digitalWrite(GREEN_PIN, LOW); digitalWrite(BLUE_PIN, LOW); // the RGB will turn on red light } delay(2000); // loop the code for each 2 seconds } |