Good morning,
I try to acquire data from an accelerometer MPU6050 and a GPS Ublox Neo 6m with an Arduino Uno.
The connection between the devices are:
-Accelerometer and GPS Vcc are connected to 3.3V
-GPS TX connected to digital pin 2, GPS RX connected to digital pin 3
-MPU6050 SCL connected to analog input 5, MPU6050 SDA connected to input 4
I can load my code but the board doesn't work; in particular it seems that there are some incompatibilities with I2C and Serial protocol because the excecution stops in the setup section when the I2C communication is initialized.
Here there is my code.
#include<TinyGPS++.h>
#include<Wire.h>
#include<SoftwareSerial.h>
const int8_t ACC=0x68;
const int8_t POWER=0x6B;
const int8_t ACC_CONFIG=0x1C;
const int8_t ACC_LIMIT=0x00;
const int8_t ACC_X_MSB=0x3B;
const int8_t PIN_RX=2;
const int8_t PIN_TX=3;
const int8_t BAUD=9600;
double quad=2;
float ms_to_s=1./1000;
SoftwareSerial gpsSerial(PIN_RX,PIN_TX);
TinyGPSPlus gps;
double conv=1./16384;
double g=9.81;
void setup() {
Wire.beginTransmission(ACC);
Wire.write(POWER);
Wire.write(0);
Wire.endTransmission();
Wire.beginTransmission(ACC);
Wire.write(ACC_CONFIG);
Wire.write(ACC_LIMIT);
Wire.endTransmission();
Serial.begin(BAUD);
gpsSerial.begin(BAUD);
}
void loop() {
while (gpsSerial.available() > 0){
gps.encode(gpsSerial.read());
}
double LAT=gps.location.lat();
double LONG=gps.location.lng();
double SPEED=gps.speed.kmph();
unsigned long currtime=millis();
float time1=currtime*ms_to_s;
Wire.beginTransmission(ACC);
Wire.write(ACC_X_MSB);
Wire.endTransmission();
Wire.requestFrom(ACC,6);
////accelerazione x MSB
int16_t acc_x=Wire.read()<<8|Wire.read();
////accelerazione y MSB
int16_t acc_y=Wire.read()<<8|Wire.read();
////accelerazione z MSB
int16_t acc_z=Wire.read()<<8|Wire.read();
/// conversione in m/s^2
double acc_x_real=acc_x*conv*g;
double acc_y_real=acc_y*conv*g;
double acc_z_real=acc_z*conv*g;
double gravity=square(acc_x_real)+square(acc_y_real)+square(acc_z_real);
double gravity1=sqrt(gravity);
//// scrittura sul monitor
Serial.print(String(acc_x_real));
Serial.print("\t");
Serial.print(String(acc_y_real));
Serial.print("\t");
Serial.print(String(acc_z_real));
Serial.print("\t");
Serial.print(String(LAT));
Serial.print("\t");
Serial.print(String(LONG));
Serial.print("\t");
Serial.print(String(SPEED));
Serial.println("done ");
Serial.print(String(gravity1));
Serial.print("\t");
Serial.print(String(square(acc_x_real)));
Serial.print("\t");
Serial.print(String(square(acc_y_real)));
Serial.print("\t");
Serial.print(String(square(acc_z_real)));
Serial.print("\t");
Serial.print(String(gravity));
Serial.println(" ");
}
If I try to acquire GPS and accelerometer data separately (only one device connected to Arduino Uno) the board works very well.
I have also noticed that if the GPS or the accelerometer are acquired separately the Arduino UNO's TX led blinks regurarly but if I load the code shown before this led never blinks.
Thank you in advance if someone can give me some suggestions.