How to use an FSR - Force Sensitive Resistor with Arduino to fade an LED. This sensor is a variable resistor just like a photocell or flex sensor. The resistance changes by applying pressure on it.
Things Needed
- Arduino uno
- Force sensitive resistor
- LED
- 10KOhm & 220 Ohm resistors
Here is the Diagram which shows, How to connect the L.E.D and F.S.R
The Code for Arduino
Here's the code, embedded using Codebender!Try downloading the Codebender plugin and clicking on the "Run on Arduino" button to program your Arduino board with this sketch. And that's it, you've programmed your Arduino with this sketch.
How it works:
- Read analog value from flex sensor
:: value=analogRead(sensorPin);
- Map analog values 0-1023 to PWM values 0-255
:: value = map(value, 0, 1023, 0, 255);
- Send pwm value to led
:: analogWrite(ledPin, value);
code
//Constants:
const int ledPin = 3; //pin 3 has PWM funtion
const int sensorPin = A0; //pin A0 to read analog input
//Variables:
int value; //save analog value
void setup(){
//Set pin 3 as 'output'
pinMode(ledPin, OUTPUT);
//Begin serial communication
Serial.begin(9600);
}
void loop(){
value = analogRead(sensorPin); //Read and save analog value from potentiometer
Serial.println(value); //Print value
value = map(value, 0, 1023, 0, 255); //Map value 0-1023 to 0-255 (PWM)
analogWrite(ledPin, value); //Send PWM value to led
delay(100); //Small delay
}
No comments:
Post a Comment