User Tools

Site Tools


courses:2010.12.01.intro_prototyping

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
courses:2010.12.01.intro_prototyping [2010/12/01 14:58]
camille typo
courses:2010.12.01.intro_prototyping [2011/08/10 16:21] (current)
24.201.55.104 new image url
Line 1: Line 1:
 +==== Sensors, serial communication and data logger ====
 +
 +  * Most analog sensors have 2 or 3 pins/​connectors
 +  * If 3 pins, straightforward:​ Ground - , Power +3.3/5V , Signal/Data
 +  * If 2 pins, have to build a small pull-up/​pull-down circuit to avoid short-circuit and adjust the sensor range.
 +  * {{http://​interactive.usc.edu/​membersmedia/​npashenkov/​flexsensor.jpg}} \\ image from [[http://​interactive.usc.edu/​blog/?​p=4762|USC]]
 +  * See [[http://​interactive.usc.edu/​blog/?​p=4762|this page]] for a longish explanation of the voltage divider for analog sensors.
 +
 +== Processing Sketch to visualize data ==
 +Code directly copied from ITP's wiki at [[http://​itp.nyu.edu/​physcomp/​Labs/​SerialOut|]]
 +<code java>
 +/*
 + ​Sensor Graphing Sketch
 + This sketch takes raw bytes from the serial port at 9600 baud and graphs them.
 + 
 + ​Created 20 April 2005
 + ​Updated 5 August 2008
 + by Tom Igoe
 + */
 +
 +import processing.serial.*;​
 +
 +Serial myPort; ​       // The serial port
 +int graphXPos = 1;    // the horizontal position of the graph:  ​
 +
 +void setup () {
 +  size(400, 300);        // window size
 +
 +  // List all the available serial ports
 +  println(Serial.list());​
 +  // I know that the fisrt port in the serial list on my mac
 +  // is usually my Arduino module, so I open Serial.list()[0].
 +  // Open whatever port is the one you're using.
 +  myPort = new Serial(this,​ Serial.list()[0],​ 9600);
 +
 +  // set inital background:
 +  background(48,​31,​65);​
 +}
 +void draw () {
 +  // nothing happens in draw.  It all happens in SerialEvent()
 +}
 +
 +void serialEvent (Serial myPort) {
 +  // get the byte:
 +  int inByte = myPort.read(); ​
 +  // print it:
 +  println(inByte);​
 +  // set the drawing color. Pick a pretty color:
 +  stroke(123,​128,​158);​
 +  // draw the line:
 +  line(graphXPos,​ height, graphXPos, height - inByte);
 +
 +  // at the edge of the screen, go back to the beginning:
 +  if (graphXPos >= width) {
 +    graphXPos = 0;
 +    // clear the screen:
 +    background(48,​31,​65);​
 +  } 
 +  else {
 +    // increment the horizontal position for the next reading:
 +    graphXPos++;​
 +  }
 +}
 +</​code>​
 +
 +== Arduino Sketch to send data to Processing ==
 +<code c>
 +int analogPin = 0;
 +int analogValue = 0;
 +
 +void setup(){
 +  // start serial port at 9600 bps:
 +  Serial.begin(9600);​
 +}
 +
 +void loop(){
 +  // read analog input, divide by 4 to make the range 0-255:
 +  analogValue = analogRead(analogPin); ​
 +  analogValue = analogValue / 4;
 +  Serial.print(analogValue,​ BYTE);
 +  // pause for 10 milliseconds:​
 +  delay(10);
 +}
 +</​code>​
 +
 +Here is a Pure-Data patch that uses the Pduino library.
 +It does roughly the same thing as this Processing sketch.
 +{{:​arduino:​arduino-analogread_pd.zip|}}
 +
 +== Processing Sketch receiving sensor data from serial and saving it to a file ==
 +In Processing, we can use the [[http://​processing.org/​reference/​PrintWriter.html|PrintWriter]] object to write data to a file.
 +<code java>
 +/*
 +Sensor Graphing Sketch
 + 
 + This sketch takes raw bytes from the serial port at 9600 baud and graphs them.
 + 
 + ​Created 20 April 2005
 + ​Updated 5 August 2008
 + by Tom Igoe
 + */
 +
 +PrintWriter output;
 +
 +import processing.serial.*;​
 +
 +Serial myPort; ​       // The serial port
 +int graphXPos = 1;    // the horizontal position of the graph:  ​
 +
 +void setup () {
 +  size(400, 300);        // window size
 +
 +  // List all the available serial ports
 +  println(Serial.list());​
 +  // I know that the fisrt port in the serial list on my mac
 +  // is usually my Arduino module, so I open Serial.list()[0].
 +  // Open whatever port is the one you're using.
 +  myPort = new Serial(this,​ Serial.list()[0],​ 9600);
 +
 +  // set inital background:
 +  background(48,​31,​65);​
 +
 +  // prepare data file
 +  String filename = "​data_"​ + year() + month() + day() + "​_"​ + hour() + minute() + second() + "​.txt";​
 +  output = createWriter(filename);​
 +}
 +void draw () {
 +  // nothing happens in draw.  It all happens in SerialEvent()
 +}
 +
 +void serialEvent (Serial myPort) {
 +  // get the byte:
 +  int inByte = myPort.read(); ​
 +  // print it:
 +  println(inByte);​
 +  // set the drawing color. Pick a pretty color:
 +  stroke(123,​128,​158);​
 +
 +  // draw the line:
 +  line(graphXPos,​ height, graphXPos, height - inByte);
 +
 +  // save data to file
 +  output.println(inByte);​
 +
 +  // at the edge of the screen, go back to the beginning:
 +  if (graphXPos >= width) {
 +    graphXPos = 0;
 +    // clear the screen:
 +    background(48,​31,​65);​
 +  } 
 +  else {
 +    // increment the horizontal position for the next reading:
 +    graphXPos++;​
 +  }
 +}
 +
 +void stop() {
 +  output.flush();​ // Writes the remaining data to the file
 +  output.close();​ // Finishes the file
 +  exit(); // Stops the program
 +}
 +</​code>​
 +==== More than one analog sensors ==== 
 +Check this article from ITP [[http://​itp.nyu.edu/​physcomp/​Labs/​SerialDuplex|]]
 +
 +==== Data Logging using special hardware ==== 
 +The Interaction Workshop as recently acquired a few data loggers:
 +  * [[http://​www.sparkfun.com/​products/​8627|Logomatic v2 Serial SD Datalogger]],​ fancy, many features, documentation lacking
 +  * [[http://​www.sparkfun.com/​products/​9530|OpenLog data logger]], relatively cheap and simple, save serial data on micro SD cards
 +  * [[http://​www.sparkfun.com/​products/​99|Real Time Clock module]], give a timestamp for the next 9 years, need a coin cell battery, I2C interface, check this [[http://​combustory.com/​wiki/​index.php/​RTC1307_-_Real_Time_Clock|tutorial]]
 +
 +To connect to the OpenLog data logger, we need an additional serial port. We can't use the one directly available with the Arduino board (pins 0 and 1) as it is connected to the USB + micro-controller. It is very common to use a software library to create additional serial ports. The library [[http://​arduiniana.org/​libraries/​newsoftserial/​|NewSoftSerial]] works pretty well. 
 +
 +<code c>
 +#include <​NewSoftSerial.h>​
 +
 +int analogPin = 0;
 +int analogValue = 0;
 +
 +// setup datalogger port
 +NewSoftSerial logger(4,​3);​ // rx, tx
 +
 +void setup()
 +{
 +  // start serial port at 9600 bps:
 +  Serial.begin(9600);​
 +
 +  // set pin mode and speed for software serial
 +  pinMode(4, INPUT);
 +  pinMode(3, OUTPUT);
 +  logger.begin(9600);​
 +
 +}
 +
 +void loop()
 +{
 +  // read analog input, divide by 4 to make the range 0-255:
 +  analogValue = analogRead(analogPin); ​
 +  analogValue = analogValue / 4;
 +
 +  // send value over Serial for Processing or debug
 +  Serial.print(analogValue,​ BYTE);
 +
 +  // send value to data logger
 +  logger.println(analogValue,​ BYTE);
 +
 +  // pause for 100 milliseconds:​
 +  delay(100); ​                
 +}
 +</​code>​
  
courses/2010.12.01.intro_prototyping.txt · Last modified: 2011/08/10 16:21 by 24.201.55.104