Hi, I'm trying to use the GY-511 module for getting the Elevation angle of an object.
https://create.arduino.cc/projecthub/electropeak/make-a-digital-compass-w-gy-511-accelerometer-magnetometer-df9dc1
I found this using a google search but the code I'm using is from another site.
I modified the code from the second link above and for a while it worked great from what I could remember. I believe I was using an Arduino Pro Mini at the time. However, now that I am using an ESP32 Pico D4, I changed the I2C pins to what they should be by default and I have them plugged into the board in those respective pins.
I checked my addresses via I2C scanner program and they appear to be correct.
Any ideas what is wrong?
I'm only getting one static value rather than a dynamic one in the serial monitor:
Elevation Angle: -135
Elevation Angle: -135
Elevation Angle: -135
Elevation Angle: -135
Elevation Angle: -135
Elevation Angle: -135
Elevation Angle: -135
Elevation Angle: -135
Here is the code I'm using:
//allows you to communicate with I2C
#include<Wire.h>
#define SDA 21
#define SCL 22
const int MPU_addr = 0x19;
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
//values for determining correct angle with accelerometer
int minVal = 85;
int maxVal = 402;
double x;
int ElevationAngle; //elevation angle
double z;
void setup() {
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(115200);
}
void loop() {
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr, 14, true);
AcX = Wire.read() << 8 | Wire.read();
AcY = Wire.read() << 8 | Wire.read();
AcZ = Wire.read() << 8 | Wire.read();
int xAng = map(AcX, minVal, maxVal, -180, 180);
int yAng = map(AcY, minVal, maxVal, -180, 180);
int zAng = map(AcZ, minVal, maxVal, -180, 180);
ElevationAngle = RAD_TO_DEG * (atan2(xAng, zAng));
Serial.print("Elevation Angle: ");
Serial.print(ElevationAngle);
Serial.println();
delay(2000);
}