Wednesday 9/21 – Lab 4: Sensing: Force sensors and photocells

Sensing: Force sensors and photocells

Lecture Slides

Available here


Before the Lab

You should have already completed your color mixer and come up with some way to control the LEDs with 2 or 3 pots.

In Lab Exercise


In the past lab, we explored potentiometers (or “pot” for short) which are analog sensors that measure rotation. In this week’s lab, we’ll be exploring two other analog sensors. The first sensor measures light brightness and is called a photocell (or a “phot” for short. Okay, that was a lie). The second sensor measures force and is called Force Sensitive Resistor (or “FSR” for short). Thus far, the pot, phot, and FSR sensors convert some physical action into an electrical characteristic, namely resistance.

In addition to new sensors, this lab will introduce “Processing” which is a programming language for creating more interesting UIs than the text based one’s you’ve been using.


Part 1 – Photocells

Beginning with the circuit you built last week, try replacing the pots with photocells (You should have two in your kit). Note: you will need to add a 10K resistor (brown,black, yellow) to the circuit as shown in the diagram below. Try the classic “LED_fade” or “LED_blink” with the photocell. Try photocell with what you have built for this week’s homework.

You can also see this tutorial on using a photocell with Arduino, which includes example code.

Once you plug in the photocell correctly, you can just read its value using analogRead, such as

photoCellVal = analogRead(pinNumber);

as we have been doing for the potentiometers.

Part 2 – Force Sensitive Resistors

Replace the photocells in your current circuit with the FSRs (distributed in class). This should simply involve swapping out the photocells with the FSRs (still using the 10K resistors). Again, try the classic “LED_fade” or “LED_blink” with the photocell. Try using the FSR with what you have built for this week’s homework.

You can also see the tutorial here on how to use an FSR sensor with arduino:

it includes example code. Again, once you plug in the FSR sensor correctly, you can just use


Part 3 – Processing

In this section, we begin exploring Processing

1. Download and install Processing if you have not done so yet (

2. Load “File ‐> Examples ‐> Topics ->Motion ‐> Bounce”

3. Press “Run”

Congrats! You just made a java applet.

Part 4 – More Processing

Influence a Processing program running on your computer with the input from your Arduino board example code.

Open a new sketch in Processing and copy in the sketch below. What will happen is that the Arduino sketch reads the value from your sensor and writes it over serial. The Processing sketch will listen to the same serial port and read the value, then that value will influence your Processing sketch. Like this:

sensor –> Arduino sketch –> Serial port –> Processing sketch –> cool visual output from Processing 🙂

Also, to send the value over from the Arduino to the Processing sketch, you will need to use something like this in your Arduino sketch:


. That sends the val over the Serial port to the Processing sketch, which is waiting to receive it. You probably already have a line like that somewhere in your Arduino sketch. Make sure you only send the number over serial; if you send any strings over then the Processing sketch will get confused.

 * Arduino Ball Paint
 * (Arduino Ball, modified 2008)
 * ---------------------- 
 * Draw a ball on the screen whose size is
 * determined by serial input from Arduino.
 * Created 21 September 2016
 * Noura Howell
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/cu.usbmodem1421"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

int serialVal = 0;

void setup() {
 port = new Serial(this, portname, 9600); 

void draw() {
 // erase the screen
 background(40, 40, 40);

 // draw the ball
 fill(255, 255, 255);

// called whenever serial data arrives
void serialEvent(Serial p) {
 int c =;
 if (c != lf && c != cr) {
 buf += char(c);
 if (c == lf) {
 serialVal = int(buf);
 buf = ""; 




Try it with pot, photocell, and FSR.



Create an interesting visualization on your computer that could be influenced by the input from the sensors you have (pot, photocell, FSR, or combination of them). You can use Processing (or any other language you like) in writing the program. Report on your results in the lab submission.


Create a mechanical construction for your FSR that distributes or focuses physical force that is applied. Think about everyday objects (toothpaste tube, entrance mat, paintbrush, pipette, etc.) and how you measure the pressure or force applied to them





(older, for reference, here is the older code that I think is note working: )