Hi below is code i am using to measure angular velovity of my sensor. However the gyro seems to produce a constant value when stationary but when i begin to move the sensor, the values increase by a large amount. for ex, it could stay constant at 20 degrees then when i touch the sensor it may jump to a minus value or even 100+ value. Any help of how I could remove this problem?
Also how could I integrate the velocity to produce the actual angle the gyro moves through?
Thank you.
#include <Wire.h> // Include Arduino I2C library
#include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define CTRL1 0x20
#define OUT_X_L 0x28
#define OUT_X_H 0x29
int8_t readData = 0x01;
int8_t writeData = 0x00;
int8_t address = 0xD6; // Device address of L3GD20H with SDO/ADR/SA0 pulled HIGH.
//int8_t address = 0xD4; // Device address of L3GD20H with SDO/ADR/SA0 connected to GND.
int16_t gx; // 16-bit variables to hold raw data from sensor
void setup() {
Serial.begin(9600);
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Aerospace Eng");
lcd.setCursor(0,1);
lcd.print("Research Project");
lcd.setCursor(0,2);
lcd.print("2016");
delay(3000);
lcd.clear();
lcd.setCursor(2,0);
lcd.print("Angle");
lcd.setCursor(7,1);
lcd.print("Sensor");
delay(3000);
lcd.clear();
lcd.setCursor(0,1);
lcd.print("By ");
delay(3000);
lcd.clear();
delay(3000);
Wire.begin();
writeReg(CTRL1, 0x0A); // Initialize the sensor by setting control register
delay(100);
}
void loop() {
gx = (int16_t) readReg(OUT_X_H) <<8 | readReg(OUT_X_L); // typecast as 16-bit
// lcd.begin();
lcd.setCursor(0,0);
lcd.print("Angular Velocity");
//Sensitivity from Page 9 of datasheet - use 8.75 mdps/digit (LSb) for default 245dps.
lcd.setCursor(0,2);
lcd.print(gx0.00875F, DEC); lcd.print("\t");
Serial.println();
delay(2000);
}
int8_t readReg(int8_t reg) {
int8_t buffer = 0;
Wire.beginTransmission((address | writeData) >>1);
Wire.write(reg);
Wire.endTransmission(0);
Wire.requestFrom((address | readData) >>1 , 1);
while(Wire.available() == 0);
buffer = Wire.read();
Wire.endTransmission();
return(buffer);
}
void writeReg(int8_t reg, int8_t val) {
Wire.beginTransmission((address | writeData) >>1);
Wire.write(reg);
Wire.write(val);
Wire.endTransmission();
}