# Project 2: Receiving and Processing FSR Values

This week's planning included
1. Creating a circuit with FSR and Arduino
2. Receiving FSR values on computer
3. Sending values to web interface

#### Creating a circuit with FSR and Arduino

FSRs work with changing their resistance they feel force or pressure upon them. This resistance change could be different for every type of FSR out there.  Thus, to start with, we measured the resistance of the current FSR with a multimeter.

We found that for our FSR, the resistance is infinitely high when unpressed, ~100.000 Ω with our finger unpressed on top of it, and ~200 Ω when pressed with our full strength.

We had to turn that resistance range to a voltage range that would ideally lie between 0 and 5V. Therefore we found a Voltage Divider circuit on Wikipedia, with

• R1 being the FSR (0-100.000 Ω)
• R2 being some other resistor(s) (unknown resistance)
• Vin being the incoming electrical current coming from Arduino’s  POWER pin (5V)
• Vout being the outgoing analog electrical signal leading to analog pin A0 (preferably between 0 and 5V)

Thus, to make the circuit work, we had to calculate the ideal values for R2. Luckily, there was a ready-made calculation on the page:

$V_{\mathrm {out} }={\frac {R_{2}}{R_{1}+R_{2}}}\cdot V_{\mathrm {in} }$

Thus, we found that for R2 = 50.000 Ω, the Vout would be ideal. When pressed, it would be close to 5V, and when unpressed close to 0V:

• Case 1: R1 = 200 Ω (fully pressed)
Vout = (50.000/(50.000 + 200)) * 5V = 4.98V
• Case 2: R1 = 100.000 Ω (finger unpressed on top)
Vout = (50.000/(100.000 + 50.000)) * 5V = 1.67V
• Case 3: R1 = infinitely big (unpressed)
Vout = (50.000/infinity) * 5V = approaching 0V

After a quick explanation how the breadboard worked (horizontally adding is in series, vertically in parallel), we created the circuit.

#### Receiving FSR values on computer

In the Arduino IDE, I started by telling the program that we would take analog pin 0 as an input value in this program.

Also, I learned about setting a rate for data transmission, a baudrate. I didn’t know that this was necessary, but apparently you have different rates for reading pin values to USB. If you don’t set the rate for this transmission, you can’t recover the data’s values. For now, I chose one of the fastest rates, 115200.

void setup() {
// Set baud rate.
Serial.begin(115200);

// We will have input from analog pin 0.
pinMode(0, INPUT);
}

In the loop, I wanted to save the current value of the fsr and print it. I was told that there were two improvements I could make for the future. Firstly, saving the 8 last bits (the most important bits) directly to the system with Serial.write(fsr >> 8), secondly: reading with a delay() to make it a bit easier for the computer to keep up:

void loop() {
// put your main code here, to run repeatedly:

//Reading value of analog pin 0.

Serial.println(fsr); //For debugging in console.

//Serial.write(fsr >> 8); //For writing to system, the last 8 MSB bits.
}

Now in the Serial Monitor (upper right button) of Arduino, I saw the values coming in, with a nice range between 0 and (approaching) 1023, the maximum value a 4-bit int can have.

#### Sending values to web interface

Now, I wanted to show those values not in Arduino’s console, but in the system, and then on a website. For this, I found that I needed to read the serial port using node.js.

I found the nodejs library johnny-five that seemed ideal for this. I looked through their documentation and tried Hello World, where a simple Arduino and blinking LED were controlled by a javascript file. It worked perfectly.

Then I wanted to read the FSR value again in johnny-five, and found this great example. Immediately all the FSR values appeared in the system console, right after any change.

But then my biggest challenge came: linking this nodejs script so that I could use it in a webpage. I made a simple webpage with a circle, where the size of the circle would depend on the pressure on the FSR.

However, up to date I struggled a lot with using the nodejs script in my real webpage, so that I could for example change html values in the script. Anyone has help?

Publicités
Publié dans Uncategorized