Jun 15, 2010
Measure pendulum with accelerometer
Acceleration sensor can be used to measure pitch, yaw and roll. The same method can be used to find amplitude. It’s all about tilt.
To measure pendulum, you need to import Math.h library so as to use arc arcsine. Arduino Software should have it by default. You can find further information in the following link. Outcome of pitch should be float data type, thus keep variables for accelerations also float.
http://tom.pycke.be/mav/69/accelerometer-to-attitude
Components:
MEMSIC2125 Accelerometer (2 axis)
Arduino
Breadboard
Jumpber wires
Velleman DC Controlled Dimmer (K8064)
Arduino code:
#include <Math.h>
// 2010 mlab.taik.fi/paja
// Pendulum using MEMSIC2125 Accelerometer//pin to read x axis value
int memsicXPin = 8;
//pin to read y axis value
int memsicYPin = 9;
//pin to + pin of Analog In pins in the DC controlled dimmer.
//- pin goes to GND.
int pinLamp = 11;void setup()
{
Serial.begin(9600);
}void loop(){
int pulseY; // raw data of y axis from accelerometer
float accelerationY; // Y axis gravity
float tiltY;
float analogVol;//value = pulseIn(pin, state);
//Pin: Pin number (0 – 13, 14 to 19 are in analog input pins)
//State: Specifies whether the puse to be mesured is low(0) or high(1)
//value: the measured pulse duration will be stored.
pulseY = pulseIn(memsicYPin, HIGH);
// calculating the pulse to 1/1000 g
accelerationY = ((pulseY / 10) – 500) * 8;// Calculate Pitch
// pitch = asin(y axis acceleration / gravity)
// 1.57 –> 90 degrees, 0 –> 0 degree
tiltY = asin(accelerationY/1000);tiltY = tiltY * 100;
// map angle to analog output value
analogVol = map(tiltY, 80, 157, 0, 255);
// controll the DC controlled dimmer
analogWrite(pinLamp, analogVol);// Print values
Serial.print(“Y: “);
Serial.print(accelerationY);
Serial.print(“\t”);
Serial.print(“Tilt: “);
Serial.print(tiltY);
Serial.print(“\t”);
Serial.print(“Voltage: “);
Serial.print(analogVol);
Serial.println();// delay(100);
}