Gas station without pumps

2011 June 29

Data Acquisition

Filed under: Accelerometer,Data acquisition,Robotics,Software — gasstationwithoutpumps @ 19:28
Tags: , , ,

One of the science-teacher blogs that I read has recently discussed using an Arduino for data acquisition: The DAQ-ness Monster « Science Learnification.

I’ve not played with the Arduino much that way, but I do have a 3-axis accelerometer, the ADXL335 from Analog Devices, on a breakout board from Adafruit Industries.  This accelerometer has analog readout, so I connected the three X, Y, and Z pins to analog pins 0,1, and 2 of the Arduino, and powered the breakout board from the Arduino 3.3 volt supply.  I also sent the 3.3 volt input to the AREF pin of the Arduino so that the I could use “ratiometric” measurements.  The ADXL335 is designed so that 0g acceleration is mid-scale, so using the same voltage for powering the chip and for the Arduino AREF input means that 512 is 0g and the scaling is approximately 1g for each difference of 100.  It turns out that the calibration is not perfect (no surprise there), so you can improve the measurements by tweaking the offset and scaling for each axis separately.  I did this manually for my code, and the results are only good to about 1%.

I should be able to do better if I write a calibration program on my laptop that gathers results from the Arduino and tries to optimize the magnitudes of the reported acceleration vectors to be one.  If the accelerometer is held steady in several different orientations, this should auto-calibrate.

I wrote a little program that measured the X, Y, and Z values 1000 times, averaged them, and sent the average over the USB line to my laptop.  I’ve put the code on-line at https://gist.github.com/1054649  Each of the measurements takes about 3.3 msec (about 1msec per DAC reading)

Limitations of the Arduino as a data-acquisition device:

  • Only 6 analog inputs
  • Only 10-bit resolution
  • All 6 channels on same scale, and input must be between 0 and a single reference voltage (3.3v for the setup I used).
  • No faster than about 1msec/sample (so don’t try to collect audio or video data)

UPDATE 8 October 2011.  I’ve just found out how to put code into the blog itself, so here it is:

// Accelerometer (ADXL335) test
// Kevin Karplus
// 29 June 2011

void setup()
{
  analogReference(EXTERNAL);   // essential if you are going to connect to AREF
  Serial.begin(115200);
  Serial.println("setup");
}

// I used wires to connect the breakout board to 
// three analong inputs, 3.3v, and ground.
// I also connected the AREF input of the Arduino to 3.3v

const int Xpin=2;
const int Ypin=1;
const int Zpin=0;

const int num_to_sum=1000;
const float xscale = 0.0098/num_to_sum;
const float yscale = 0.0097/num_to_sum;
const float zscale = 0.0099/num_to_sum;

const int zero_x = 500;
const int zero_y = 506;
const int zero_z = 511;

long xsum,ysum,zsum;

void loop()
{
  unsigned long startTime=millis();
  xsum = ysum = zsum = 0;
  for (int i=num_to_sum; i>0; i--)
  {    
     xsum += analogRead(Xpin) -zero_x;
     ysum += analogRead(Ypin) -zero_y;
     zsum += analogRead(Zpin) -zero_z;
  }
  Serial.print(millis()-startTime);
  float x=xscale*xsum;
  float y=yscale*ysum;
  float z=zscale*zsum;
  Serial.print(" X=");
  Serial.print(x);
  Serial.print(" Y=");
  Serial.print(y);
  Serial.print(" Z=");
  Serial.print(z);
  
  Serial.print(" total=");
  Serial.println(sqrt(x*x+y*y+z*z));
}

4 Comments »

  1. Thanks for the pingback. I’m really interested to see if I can actually make some use of an Arduino as a legit DAQ system for one of my upper-year labs.

    Comment by Joss Ives — 2011 August 20 @ 21:31 | Reply

  2. I think that you should be able to, as long as you don’t need high sampling rates or high precision (10 bits at 1kHz seems reasonable).

    Have you looked at the data logger shield (https://www.adafruit.com/products/243)? The built-in time-of-day timer and SD card writing makes recording longer experiments much easier, as you don’t have to keep transferring data up to a computer. I considered getting one, but I don’t currently have an application that needs one—the robotics projects the club is looking at don’t require untethered data logging.

    You can run the whole experiment on a stand-alone Arduino, then just plug the SD card into the computer to transfer data. With battery power from an 9v battery, the entire Arduino, data logger, and sensor can be put on cart or other untethered experiment. You would need a case for the system for a lot of experiments, though, to prevent damage.

    The biggest problem will probably be careless students shorting out power or connecting the Arduino up to a voltage source that burns them out—there is no protection for the boards and students can be awfully creative in finding damaging mistakes to make.

    Comment by karplus — 2011 August 20 @ 23:59 | Reply

  3. I haven’t played around with any shields yet, but the data logging one looks very handy.

    Comment by Joss Ives — 2011 August 21 @ 22:18 | Reply

  4. […] used accelerometers before, both the analog output ADXL335 and the I2C MQA8452Q. The ADXL335 breakout board was from Adafruit Industries, the MQA8452Q from […]

    Pingback by Accelerometer for circuits course? « Gas station without pumps — 2012 August 7 @ 11:02 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.