User Tools

Site Tools


courses:2010.12.01.intro_prototyping

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.
  • interactive.usc.edu_membersmedia_npashenkov_flexsensor.jpg
    image from USC
  • See 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

/*
 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++;
  }
}
Arduino Sketch to send data to Processing
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);
}

Here is a Pure-Data patch that uses the Pduino library. It does roughly the same thing as this Processing sketch. arduino-analogread_pd.zip

Processing Sketch receiving sensor data from serial and saving it to a file

In Processing, we can use the PrintWriter object to write data to a file.

/*
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
}

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:

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 NewSoftSerial works pretty well.

#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);                 
}
courses/2010.12.01.intro_prototyping.txt · Last modified: 2011/08/10 16:21 by 24.201.55.104