Hey, I am using a single axis accelerometer. I need to know how this accelerometer can be used to trace angles and control the rotation of a motor. I need the rotation from 0 to 45 degrees. Can anybody help me out with this?
an accelerometer always read the G acceleration. So if you put it orizontal you read 1G, vertical 0G, and in other angole you'll read G*sin(angle) (probably formula is wrong AND change depending your reference, take a look at trigonometry)
vasu26:
...trace angles and control the rotation of a motor...
Please tell us more how you plan to do this. An accelerometer does not measure the rotation of a motor. I can measure how much something is tilted.
I have a motor shaft that is supposed to lift a link. this accelerometer is kept in parallel to the motor shaft and rotates as and when the motor rotates. Now I need a code that will make sure that the motor rotates from 0 to 45 degrees and then after a delay of 2 secs, it comes back from 45 to 0 degrees. And how do I reverse the polarity of voltage going to the motor to make the 45 to 0 degree movement. Btw, it is a 3 axis accelerometer.
If one axis is parallel to the motor shaft and it reads full gravity (1 g) at zero degrees, it will read .707 g when tilted at 45 degrees. As long as you start with the accelerometer reading full gravity at zero degrees, the general formula for any other angle is:
value = g* cos(angle), where g = earth gravity and angle is in radians. (1 degree = 0.01745 radians).
By the way, have you actually tried to do any of this? If so, what have you done? Have you looked into using servos?
what about this code?
#define ADC_ref 2.56
#define zero_x 1.569
#define zero_y 1.569
#define zero_z 1.569
#define sensitivity_x 0.3
#define sensitivity_y 0.3
#define sensitivity_z 0.3
unsigned int value_x;
unsigned int value_y;
unsigned int value_z;
float xv;
float yv;
float zv;
float angle_x;
float angle_y;
float angle_z;
void setup() {
analogReference(INTERNAL2V56);
Serial.begin(256000);
}
void loop() {
value_x = analogRead(0);
value_y = analogRead(1);
value_z = analogRead(2);
xv=(value_x/1024.0*ADC_ref-zero_x)/sensitivity_x;
Serial.print ("x= ");
Serial.print (xv);
Serial.print(" g ");
yv=(value_y/1024.0*ADC_ref-zero_y)/sensitivity_y;
Serial.print ("y= ");
Serial.print (yv);
Serial.print(" g ");
zv=(value_z/1024.0*ADC_ref-zero_z)/sensitivity_z;
Serial.print ("z= ");
Serial.print (zv);
Serial.print(" g ");
Serial.print("\n");
Serial.print("Rotation ");
Serial.print("x= ");
angle_x =atan2(-yv,-zv)*57.2957795+180;
Serial.print(angle_x);
Serial.print(" deg");
Serial.print(" ");
Serial.print("y= ");
angle_y =atan2(-xv,-zv)*57.2957795+180;
Serial.print(angle_y);
Serial.print(" deg");
Serial.print(" ");
Serial.print("z= ");
angle_z =atan2(-yv,-xv)*57.2957795+180;
Serial.print(angle_z);
Serial.print(" deg");
Serial.print("\n");
delay(1000);
delay(1000);
}