Although the Arduino can be powered by a battery and built into a standalone exhibit, most designs involve a direct connection to a regular desktop or laptop computer. There are two basic approaches. First, you can have a program running on the Arduino communicate with a program running on the computer (written, for example, in Processing) via a serial connection. Since the language used on the Arduino and the Processing language share a common ancestry, many of the commands are the same on both sides of the connection. An alternative is to download a special program to the Arduino called Firmata. Once this is installed, you control the Arduino from a program running on the computer, and you do not have to make any further changes to your Arduino software. As you will see, this can be very convenient. The program on the computer side could be written in many different languages, but in our class we will use Processing.
Background Preparation
In class we will be working in pairs. You should already have Processing and Arduino installed on a laptop. If you have a chance to download and install the Firmata Arduino library before class, please do so. Also be sure to bring your Arduino kits and accessories to class.
In Class Exercise
- Put a pushbutton switch on Arduino pin D2
- Put an LED on Arduino pin D3
- Connect your Arduino to the computer with USB
- Start the Arduino software on the computer and check your board and serial configuration
- Optional: try uploading the Blink sketch to your Arduino so you know that everything is set up properly
- In the Arduino IDE, choose File -> Examples -> Firmata -> StandardFirmata
- Upload it to your Arduino
- If you didn’t run into any errors, you can close the Arduino software now
- If you haven’t already done so, download and install the Firmata Arduino Library (note that if you are working on Windows you will need an unzip utility to open the file, and if you are working on Linux you have to rename the .jar file as described on the installation page)
- Once you have unzipped the library, you have to move the arduino folder to the libraries subfolder of your Processing Sketchbook (if you don’t know where this is, start Processing and choose Preferences to find out). If the libraries subfolder does not exist, create it
- Start Processing and try the following sketch
- Important: make sure that you understand how this process differs from the way that we were doing things previously! Also note that when you press the button on the Arduino, something happens both on the Arduino (an LED lights up) and on your computer (the graphic display changes)
import processing.serial.*; import cc.arduino.*; Arduino arduino; int mySwitch = 2; int myLED = 3; void setup() { arduino = new Arduino(this, Arduino.list()[0], 57600); arduino.pinMode(mySwitch, Arduino.INPUT); arduino.pinMode(myLED, Arduino.OUTPUT); } void draw() { if (arduino.digitalRead(mySwitch) == Arduino.HIGH) { arduino.digitalWrite(myLED, Arduino.HIGH); ellipse(50, 50, 80, 80); fill(0); } else { arduino.digitalWrite(myLED, Arduino.LOW); ellipse(50, 50, 80, 80); fill(255); } delay(100); }
Once you’ve gotten the basic demonstration sketch working, you can begin to modify it to suit your own project.
References