2. FSR with led
FSR with led
Circuit:
- FSR Setup:
- Connect one end of the FSR to 5V.
- Connect the other end of the FSR to an analog pin (e.g., A0) and a 10kΩ resistor.
- Connect the other side of the resistor to GND.
- LED Setup:
- Connect the positive (anode) of the LED to a digital pin (e.g., LED1 to pin 13, as per your setup).
- Connect the negative (cathode) of the LED to a current-limiting resistor (e.g., 220Ω) and then to GND.
Code:
The code below reads the FSR value and turns on the LED if the force exceeds a defined threshold.
C++ Code
int fsrPin = A0; // Analog pin connected to FSR
int fsrReading; // Variable to store the FSR reading
int threshold = 600; // Threshold value to turn on the LED
int ledPin = 13; // Pin connected to LED1
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
Serial.begin(9600); // Start serial communication at 9600 baud
}
void loop() {
fsrReading = analogRead(fsrPin); // Read the value from FSR
Serial.print("FSR Reading: ");
Serial.println(fsrReading); // Print the FSR reading
if (fsrReading > threshold) {
digitalWrite(ledPin, HIGH); // Turn on the LED if reading exceeds the threshold
} else {
digitalWrite(ledPin, LOW); // Turn off the LED if reading is below the threshold
}
delay(500); // Add delay for readability
}
Explanation:
Threshold Setting:
- The
threshold
variable defines the FSR reading value needed to turn on the LED. You can adjust this threshold based on your requirements and the weight/pressure you want to detect.
- The
Reading and LED Control:
analogRead(fsrPin)
reads the FSR value (0-1023).- If
fsrReading
exceeds thethreshold
,digitalWrite(ledPin, HIGH)
turns on the LED. Otherwise,digitalWrite(ledPin, LOW)
turns it off.
Adjusting the Threshold:
- You can test the circuit with different pressures and note the corresponding
fsrReading
values in the Serial Monitor. Adjustthreshold
accordingly to achieve the desired LED behavior.
- You can test the circuit with different pressures and note the corresponding
Comments
Post a Comment