Lab 4: Sensing: Force Sensors and Photocells

Chai Raghupathi. Chris Myers. Tangible User Interfaces, Fall 2022

Description

Part One — Photocells (Figure 1): We build a circuit with a photocell and 10k resistor as shown in the following diagram:

Figure 1.1 Photocell Controls One LED Brightness

Part Two — Force Sensitive Resistors (Figure 2): We build a circuit with a force sensitive resistor and 10k resistor just like the photocell circuit. The only difference is we replace the photocell with an FSR.

Part Three — Introducing Processing (Figure 3): Processing is a “flexible software sketchbook” that combines visual art with technology. We run some basic programs such as “Brightness” which takes input through mouse interaction and shows output with color.

Part Four — Arduino X Processing (Figure 4): Building on parts one, two, and three, we begin integrating the Processing software with our Arduino. To do this, we make use of the Firmata library. Firmata runs the serial communication between Arduino and Processing. [Digital output] Circle gets bigger when more force is applied to FSR. [Physical output] The LED also gets brighter with more force application.

Components Used

  • Breadboard
  • LEDs
  • 220Ω and 10kΩ Resistors
  • Photocell sensor
  • Force sensitive resistor sensor
  • Laptop
  • Jumper wires

Code

/* Photocell simple testing sketch. 

Connect one end of the photocell 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 more information see http://learn.adafruit.com/photocells */

int photocellPin = A0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int LEDbrightness; //
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}

void loop(void) {
photocellReading = analogRead(photocellPin);

Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading

// LED gets brighter the darker it is at the sensor
// that means we have to -invert- the reading from 0-1023 back to 1023-0
photocellReading = 1023 - photocellReading;
//now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
analogWrite(LEDpin, LEDbrightness);

delay(100);
}

Figure 2.1 Force Sensitive Resistor (FSR) Controls One LED Brightness

/* FSR testing sketch. 

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 more information see www.ladyada.net/learn/sensors/fsr.html */

int fsrAnalogPin = A0; // FSR is connected to analog 0
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int fsrReading; // the analog reading from the FSR resistor divider
int LEDbrightness;

void setup(void) {
Serial.begin(9600); // We'll send debugging information via the Serial monitor
pinMode(LEDpin, OUTPUT);
}

void loop(void) {
fsrReading = analogRead(fsrAnalogPin);
Serial.print("Analog reading = ");
Serial.println(fsrReading);

// we'll need to change the range from the analog reading (0-1023) down to the range
// used by analogWrite (0-255) with map!
LEDbrightness = map(fsrReading, 0, 1023, 0, 255);
// LED gets brighter the harder you press
analogWrite(LEDpin, LEDbrightness);

delay(100);
}

Figure 3.1 Brightness Program in Processing

/**
* Brightness
* by Rusty Robison.
*
* Brightness is the relative lightness or darkness of a color.
* Move the cursor vertically over each bar to alter its brightness.
*/

int barWidth = 20;
int lastBar = -1;
void setup() {
size(640, 360);
colorMode(HSB, width, 100, height);
noStroke();
background(0);
}
void draw() {
int whichBar = mouseX / barWidth;
if (whichBar != lastBar) {
int barX = whichBar * barWidth;
fill(barX, 100, mouseY);
rect(barX, 0, barWidth, height);
lastBar = whichBar;
}
}

Figure 4.2 Processing Sketch to Work with Arduino through Firmata Library. Processing Code. Digital output of circle that gets bigger with more force applied to FSR. In Arduino, I ran the StandardFirmata code.

/*
arduino_input

Demonstrates the reading of digital and analog pins of an Arduino board
running the StandardFirmata firmware.

To use:
* Using the Arduino software, upload the StandardFirmata example (located
in Examples > Firmata > StandardFirmata) to your Arduino board.
* Run this sketch and look at the list of serial ports printed in the
message area below. Note the index of the port corresponding to your
Arduino board (the numbering starts at 0). (Unless your Arduino board
happens to be at index 0 in the list, the sketch probably won't work.
Stop it and proceed with the instructions.)
* Modify the "arduino = new Arduino(...)" line below, changing the number
in Arduino.list()[0] to the number corresponding to the serial port of
your Arduino board. Alternatively, you can replace Arduino.list()[0]
with the name of the serial port, in double quotes, e.g. "COM5" on Windows
or "/dev/tty.usbmodem621" on Mac.
* Run this sketch. The squares show the values of the digital inputs (HIGH
pins are filled, LOW pins are not). The circles show the values of the
analog inputs (the bigger the circle, the higher the reading on the
corresponding analog input pin). The pins are laid out as if the Arduino
were held with the logo upright (i.e. pin 13 is at the upper left). Note
that the readings from unconnected pins will fluctuate randomly.

For more information, see: http://playground.arduino.cc/Interfacing/Processing
*/

import processing.serial.*;

import cc.arduino.*;

Arduino arduino;

// Change this pin number to match where you actually have an LED in your
// circuit
int ledPin = 9;

// Change this pin number to match where you actually have a pot in your
// circuit. Note that pin 0 is the same as pin A0, pin 1 is same as A1, etc.
int potPin = 0;

void setup() {
size(500, 500);

// Prints out the available serial ports.
println(Arduino.list());

// Modify this line, by changing the "0" to the index of the serial
// port corresponding to your Arduino board (as it appears in the list
// printed by the line above).
arduino = new Arduino(this, Arduino.list()[0], 57600);

// Alternatively, use the name of the serial port corresponding to your
// Arduino (in double-quotes), as in the following line.
//arduino = new Arduino(this, "/dev/tty.usbmodem1411", 57600);

arduino.pinMode(ledPin, Arduino.OUTPUT);
}

void draw() {
// We can control the Arduino from Processing
int potVal = arduino.analogRead(potPin);
int ledBrightness = int(map(potVal, 0, 1023, 0, 255));
arduino.analogWrite(ledPin, ledBrightness);

// We can also draw to the Processing window
// setting the background to black (red = 0, green = 0, blue = 0)
background(0,0,0);
// pick up the white paintbrush (red = 255, green = 255, blue = 255)
// the next shape we draw will have white color fill
fill(255, 255, 255);
// Draw a circle at the center of the window, whose size changes with the potVal
ellipse(width/2, height/2, potVal/3, potVal/3);
}

Videos

Figure 2.2 Force Sensitive Resistor (FSR) Controls One LED Brightness

Figure 3.2 Digital Output From Moving Mouse in a Ovular Pattern

Figure 4.2 Processing Sketch to Work with Arduino through Firmata Library. [Digital output] Circle gets bigger when more force is applied to FSR. [Physical output] The LED also gets brighter with more force application.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store