DS3:007 – More About Sensors, Arduino + Processing



Sensing the World

When you first get into physical computing and working with sensors and electronics, it might be a bit overwhelming and it’s hard to figure out what exactly is possible. One way of finding out what is possible is just browsing for existing sensors. I would recommend looking through the following sites:

You can divide sensors roughly into three categories depending on how you need to connect them to the Arduino.

  • Variable Resistors
    • Resistors that change their values depending on some input
    • Use with the voltage divider circuit
    • photocells (light sensor), thermistors (temperature sensor), many flex and force sensors
  • Sensors With Analog Output
    • Sensors that output a varying voltage that can be read directly with the Arduino (0–5V)
    • Sharp infrared distance sensors, some accelerometers…
  • Sensors With Digital Output
    • Different protocols for communication
    • I2C (most modern sensors use I2C)
    • SPI
    • Serial
    • PWM

Arduino + Processing

In order to read sensor data into Processing or to send some information to the Arduino from Processing, you need to communicate over the USB connection. The communication happens as serial communication (just like the Serial Monitor in Arduino). But it is just data going both directions, you either have to come up with your own protocol for what that data means what, or you can use some ready-made solution. Firmata is one of these solutions.


Firmata code comes with the Arduino examples. It handles all the commands to receive or send data between the Arduino board and various other software (Processing, Pure Data etc.)

How to use it:

  1. Upload the StandardFirmata code to your Arduino. (File–>Example–>Firmata–>StandardFirmata)
  2. After you have uploaded the code, you can close the Arduino IDE
  3. In Processing, install the Arduino (Firmata) library using the Contribution Manager (Sketch–>Import Library–>Add Library). Search for Arduino and you should see it appear.)

A Basic Example

How to open the connection and turn on an LED connected to pin 9.

import processing.serial.; import cc.arduino.;
import org.firmata.*;
Arduino myArduino;
void setup(){
// Print a list all the serial ports available on your computer
// Insert the index number of your Arduino serial port
// inside the Arduino.list[X] to open the connection
myArduino = new Arduino(this, Arduino.list()[0], 57600);
void draw(){
// Turn the LED on

Reading Sensors

import processing.serial.; import cc.arduino.;
import org.firmata.*;
Arduino myArduino;
float b = 0;
float lightVal = 0;
float bgColor = 0;
void setup() {
size(500, 500);
myArduino = new Arduino(this, Arduino.list()[2], 57600);
myArduino.pinMode(9, Arduino.OUTPUT);
void draw() {
lightVal = myArduino.analogRead(0);
bgColor = map(lightVal, 200, 800, 0, 255);
b = map(mouseX, 0, width, 0, 255);
myArduino.analogWrite(9, int(b));
text(lightVal, 100, 100);
text(int(b), mouseX, mouseY);