Copy and paste the code below into a blank Arduino sketch. Then compile and upload it to your Metro.  

/*
 * Force Sensitive Resistor Test Code
 *
 * The intensity of the LED will vary with the amount of pressure on the sensor
 */
int sensePin = 2; // the pin the FSR is attached to
int ledPin = 9; // the pin the LED is attached to (use one capable of PWM)
void setup() {
 Serial.begin(9600);
 pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
 int value = analogRead(sensePin) / 4; //the voltage on the pin divded by 4 (to
 //scale from 10 bits (0-1024) to 8 (0-255)
 analogWrite(ledPin, value); //sets the LEDs intensity proportional to
 //the pressure on the sensor
 Serial.println(value); //print the value to the debug window
}

Not Working? 

LED Not Lighting Up?

LEDs will only work in one direction. Try taking it out and twisting it 180 degrees. (no need to worry, installing it backwards does no permanent harm).

Fading to Fast/Slow

This is a result of the FSR’s response to pressure not being quite linear. But do not fear it can be changed in code (check out the details in the Making it Better section)

Looking For More?

You're in luck - this guide has extra CIRCuits and a few PROJects included! Check the sidebar on the left for a full listing.

This guide was first published on Aug 18, 2017. It was last updated on Jun 12, 2017.

This page (Code) was last updated on May 12, 2023.

Text editor powered by tinymce.