# Portable Acceleration Sensor for High School Physics Labs

Hi all, new to the forums and Arduino world in general. I am a physics teacher and have an idea for a sensor that would prove to be very useful if it can be done. My students have built K'Nex roller coasters and I would like them to be able to make direct acceleration measurements of the cart (sensor attached to cart) as it travels around the path so can't be connected to computer. In my head it seems feasible but haven't been able to get far. This is what I have for code:

``````const int groundpin = 18;             // analog input pin 4 -- ground
const int powerpin = 14;              // analog input pin 0 -- voltage
const int xpin = A1; // x-axis
const int ypin = A2; // y-axis
const int zpin = A3; // z-axis
void setup()
{Serial.begin(9600);

pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
}

void loop()
{
int x = analogRead(xpin); //read from xpin
int y = analogRead(ypin); //read from ypin
int z = analogRead(zpin); //read from zpin

float zero_G = 512.0; //ADC is 0~1023 the zero g output equal to Vs/2
//ADXL335 power supply by Vs 3.3V
float scale = 102.3; //ADXL335330 Sensitivity is 330mv/g
//330 * 1024/3.3/1000

Serial.print("\t");
Serial.print(((float)x-340)/65*9.8); //print y value on serial monitor
Serial.print("\t");
Serial.print(((float)y-346)/68.5*9.8); //print y value on serial monitor
Serial.print("\t");
Serial.print(((float)z-414)/68*9.8); //print z value on serial monitor
Serial.print("\t");
Serial.print(millis());
Serial.println ();
delay(1000); //wait for 1 second while testing to slow data through the serial monitor but will be 100
}
``````

Some more details and problems:

1. Accelerometer (ADXL 335) data is hard to work with. The above code that I found/combined/edited from a number of helpful places (can't remember now) can convert the values from the raw data to be essentially zero when sitting flat. As per accelerometers, once rotated, the data departs from zero even if still. Is it possible to ever get just dynamic data? Essentially reads zero if still regardless of orientation. I read that I might need a gyro but not sure.

2. To be portable, I intend to have an SD card breakout to write a file to that could then be transferred to a computer easily. I figure a .txt file with a few tabbed columns would suffice for easy import into excel but no experience working with this at all from an arduino coding end. Help!? :o

3. Battery wise, haven't gotten that far but I would get a LiPo with an appropriate charger board.

4. Currently developing with a UNO R3 but smaller would be nice if there are any suggestions. The custom K'nex roller coaster carts that I have 3d printed have only about a 5cm square platform area.

If you need any other information or details, let me know. ANY help you can provide is greatly appreciated.

The Arduino 10 bit ADC may be inappropriate for such a project. If you can't get stable values in stall, a Kalman filter may be a considerable improvement. So a 6DOF IMU (MPU6050) may be a better hardware for your project. For that chip much code can be found in the web, like for quadcopter stabilization (ardupilot).

I prefer the ADXL345 instead. It does the digital conversion for you.

There are a number of algorithms out there to subtract gravity from the measurement. However I would not bother. Just calculate the total magnitude of acceleration from the 3 axes and report that.

Bluetooth to a computer can give a wireless continuous readout of acceleration and then you can plot that with the Arduino Serial Plotter.

Sparkfun and Adafruit both have simple solutions for adding a LiPo battery with USB charging.

The Micro is my favourite Arduino for this kind of project. USB serial plus hardware Serial. The Teensy series is good too. The Teensy 3.6, while large, also includes an SD card socket.

Data from the MPU6050 can be streamed in real-time via WiFi to a PC. This example sends orientation (roll, pitch, yaw) but raw acceleration values could be sent instead.

Something similar could be done for any sensor and with a different network protocol. OSC is convenient here because it is well supported in Processsing. mqtt or plain TCP are other options.

I forgot to mention the ESP MPU teapot example is based on this article which gives more background on the orientation visualization.

the raw data to be essentially zero when sitting flat. As per accelerometers, once rotated, the data departs from zero even if still.

The accelerometer measures the acceleration due to gravity as well as accelerations due to other forces. So, unless the accelerometer is in free fall in a vacuum, the "data" are never zero.

The best you can do in a simple 2D or 1D physics experiment is to arrange the accelerometer so that one axis is always vertical, and ignore the acceleration (1 g) along that axis.

Is it possible to ever get just dynamic data?

Not easily, and not very accurately. If you know the absolute orientation of the accelerometer, accurate to about 0.1 degree, then in principle you can subtract the gravity vector from the total acceleration vector with less than 2% overall error.

In practice, obtaining this level of accuracy is nearly impossible with consumer grade accelerometers.