Lab 5: Serial Communication with Firmata, Arduino, & Processing
- Due Oct 4, 2017 by 10:30am
- Points 10
- Submitting a file upload
- File Types pdf
In Lab Exercise
Objective
In previous labs we made Arduino talk to Processing over Serial. We wrote both Arduino and Processing code, and Arduino sent short little messages to Processing so they could coordinate.
In this lab we will learn a different way to make Arduino and Processing communicate, using Firmata. The Arduino will become a "slave" (pardon my language, this is really how they phrase things in computer science!) to Processing. So we will only write a Processing sketch. The Processing sketch will control the Arduino. Processing and Arduino are still communicating over serial, but the reading and writing of serial messages is done automatically.
Activities
PART 1 - BLINK (HELLO WORLD FIRMATA)
In Arduino, Firmata comes built in. Go to File --> Examples --> Firmata --> StandardFirmata to open the StandardFirmata sketch. Upload this sketch to your board.
In Processing, we have to install Firmata. Go to Sketch --> Import Library and in the window that appears search for Firmata and download it.
Try out the following Processing sketch:
import processing.serial.*; import cc.arduino.*; Arduino arduino; int ledPin = 13; void setup() { println(Arduino.list()); // You may need to change the 1 in the line below // This should refer to the index (counting from 0) // in the list of available USB connections // this sketch prints this list in the console below arduino = new Arduino(this, Arduino.list()[1], 57600); arduino.pinMode(ledPin, Arduino.OUTPUT); } void draw() { arduino.digitalWrite(ledPin, Arduino.HIGH); delay(1000); arduino.digitalWrite(ledPin, Arduino.LOW); delay(1000); }
You may need to change the "1" as indicated in the comments. This is similar to the previous lab with serial where we have to tell Processing which serial connection to use.
This Processing sketch should make the built in on board LED on the Arduino (connected to pin 13) blink. (Side note: If you have a very old Arduino board that does not have a built in LED, you will need to connect an LED and resistor on a breadboard on pin 13. You can do this with the same circuit setup as in previous labs with LEDs, just make sure to use pin 13 this time.)
What does the Arduino do when you stop running the Processing sketch? How is this different than when you just uploaded your own Arduino Blink sketch directly to the Arduino board?
With our current setup, the Arduino is running the Firmata sketch, and Processing is running the Blink sketch. The Firmata sketch just makes Arduino listen for commands. All the commands come from Processing.
PART 2 - SENSING, ACTUATING, & ANIMATION
In your circuit, connect a sensor of your choice (pot, photocell, FSR) to pin A0. Refer to previous labs for details on how to do this. Remember you may also need to use resistors.
In your circuit, connect an LED ("actuator") to pin 9. Refer to previous labs for details on how to do this. Remember you may also need to use resistors.
Try this Processing sketch:
import processing.serial.*; import cc.arduino.*; Arduino arduino; int ledPin = 9; int ledBrightness = 0; int sensorPin = 0; // pin A0 int sensorVal = 0; void setup() { println(Arduino.list()); // You may need to change the 1 in the line below // This should refer to the index (counting from 0) // in the list of available USB connections // this sketch prints this list in the console below arduino = new Arduino(this, Arduino.list()[1], 57600); arduino.pinMode(ledPin, Arduino.OUTPUT); arduino.pinMode(sensorPin, Arduino.INPUT); // sets the size of the canvas size(1000,1000); // sets the background color of the canvas in red, green, blue components background(40,40,40); // sets the framerate at which to update the animations on the canvas frameRate(10); } void draw() { sensorVal = arduino.analogRead(sensorPin); println(sensorVal); ledBrightness = sensorVal / 4; arduino.analogWrite(ledPin, ledBrightness); // make the canvas blank again background(40, 40, 40); // draw a white ball whose size is determined by the value from serial fill(255, 255, 255); ellipse(500,500,sensorVal,sensorVal); }
This draws a ball like last week in addition to setting the brightness of an LED based on the sensor value. Notice that we have to divide the sensorVal by 4 because the sensorVal can range between 0 and 1024 while the LED brightness can range only between 0 and 255.
Homework
Create an interesting visualization on your computer that is influenced by the input from the sensors you have (pot, photocell, FSR, or combination of them). If you want to, you can build on your ideas and Processing animation from last week, but you should use Firmata this time and so that will require re-writing or re-organizing some of the code. If you choose to use another language other than Processing for the animation, see if they have a Firmata-like library available and use that (e.g., pyFirmata Links to an external site. for Python, Johnny-Five Links to an external site. for nodejs).
Reflect on the similarities and differences between the serial communication protocol we used last week ("custom serial") and the Firmata protocol we are using this week. Custom serial and Firmata are two different ways of doing the same thing. Each makes some things easier and other things harder. What is easier with custom serial? What is harder? What is easier with Firmata? What is harder? Which do you prefer and why?