Hello everyone, how are you? Could you help me? I'm conducting acceleration measurements with the MPU 6050 sensor, but I need to use a measurement delay of 1 millisecond for each measurement. However, when I start the measurement with this rate, the Arduino serial freezes after some time of use, around 10 seconds. But when I use the measurement at 40 milliseconds, it works normally, but this measurement doesn't meet my needs, it needs to be 1 millisecond. I'm connecting the Vcc 3.3v to the MPU Vcc on the Arduino, Gnd to Gnd, and SCL and SCA with pull-ups.
What should I do to make this work at the 1 millisecond rate?
// Inclusão das Bibliotecas
#include<Wire.h>
#include<MPU6050.h>
const unsigned long TEMPO = 10;
unsigned long TempoAntes =0, TempoAtual = 0;
// Endereco I2C do sensor MPU-6050
const int MPU = 0x68;
// Variaveis para armazenar valores do sensor
float AccX, AccY, AccZ;
// Variaveis para armazenar o tempo
float TempoDeCorrido = 0;
void setup() {
// Inicializa Serial
Serial.begin(115200);
delay(1000); // Aguarda 1 segundo para garantir que o Arduino estĆ” pronto
// Inicializa o MPU-6050
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
// Configura Acelerometro para fundo de escala desejado
/*
Wire.write(0b00000000); // fundo de escala em +/-2g
Wire.write(0b00001000); // fundo de escala em +/-4g
Wire.write(0b00010000); // fundo de escala em +/-8g
Wire.write(0b00011000); // fundo de escala em +/-16g
*/
Wire.beginTransmission(MPU);
Wire.write(0x1C);
Wire.write(0b00011000); // Trocar esse comando para fundo de escala desejado conforme acima
Wire.endTransmission();
//Apresentação da unidades de medidas
//Serial.println("Segundos;EixoX;EixoY;EixoZ");
//Serial.println("s;m/s2;m/s2;m/s2");
//Serial.println("LABEL,Tempo(s),Eixo X (m/s2),Eixo Y (m/s2),Eixo Z (m/s2)"); // Configura os cabeƧalhos das colunas no Excel
}
void loop() {
TempoAtual = millis();
if(TempoAtual - TempoAntes >= TEMPO){
// Comandos para iniciar transmissão de dados
Wire.beginTransmission(MPU);
Wire.write(0x3B);// onde comeƧa a receber os dados
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true); // Solicita os dados ao sensor somente de aceleração
// Armazena o valor dos sensores nas variaveis correspondentes
AccX = Wire.read() << 8 | Wire.read();
AccY = Wire.read() << 8 | Wire.read();
AccZ = Wire.read() << 8 | Wire.read();
// Imprime na Serial os valores obtidos
/* Alterar divisão conforme fundo de escala escolhido:
AcelerƓmetro
+/-2g = 16384
+/-4g = 8192
+/-8g = 4096
+/-16g = 2048
*/
// Envia os dados para o Excel usando os comandos do PLX DAQ v2
//Serial.print("DATA,"); // Comando para enviar dados para o Excel
///Serial.print(TempoDeCorrido);
//Serial.print(","); // Separador de colunas
//Serial.println((AccX / 2048) * 9.81);// Aceleração X em m/s^2
// Serial.print(" "); // Separador de colunas
// Serial.print((AccY / 2048) * 9.81);// Aceleração Y em m/s^2
//Serial.print(" "); // Separador de colunas
Serial.println((AccZ / 2048.0) * 9.81);// Aceleração Y em m/s^2
TempoDeCorrido += 0.1; // Incrementa o tempo
TempoAntes = TempoAtual;
}
}
I did some tests with the multimeter and when I measure one of the SCL or SCA ports, the data starts working again when the system is frozen. What could it be?
Have a look at the Serial Input Basics tutorial, and use those ideas to wait for a certain keyboard character to be pressed, to start or stop the measurement.
// Inclusão das Bibliotecas
#include<Wire.h>
//#include<MPU6050.h>
const unsigned long TEMPO = 1000;
unsigned long TempoAntes = 0, TempoAtual = 0;
// Endereco I2C do sensor MPU-6050
const int MPU = 0x68;
// Variaveis para armazenar valores do sensor
float AccX, AccY, AccZ;
// Variaveis para armazenar o tempo
float TempoDeCorrido = 0;
void setup() {
// Inicializa Serial
Serial.begin(115200);
delay(1000); // Aguarda 1 segundo para garantir que o Arduino estĆ” pronto
// Inicializa o MPU-6050
Wire.begin();
Wire.setClock(400000);
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
// Configura Acelerometro para fundo de escala desejado
/*
Wire.write(0b00000000); // fundo de escala em +/-2g
Wire.write(0b00001000); // fundo de escala em +/-4g
Wire.write(0b00010000); // fundo de escala em +/-8g
Wire.write(0b00011000); // fundo de escala em +/-16g
*/
Wire.beginTransmission(MPU);
Wire.write(0x1C);
Wire.write(0b00011000); // Trocar esse comando para fundo de escala desejado conforme acima
Wire.endTransmission();
//Apresentação da unidades de medidas
//Serial.println("Segundos;EixoX;EixoY;EixoZ");
//Serial.println("s;m/s2;m/s2;m/s2");
//Serial.println("LABEL,Tempo(s),Eixo X (m/s2),Eixo Y (m/s2),Eixo Z (m/s2)"); // Configura os cabeƧalhos das colunas no Excel
}
void loop() {
static int count = 0;
static unsigned long prevTime;
TempoAtual = micros();
if (TempoAtual - TempoAntes >= TEMPO) {
// Comandos para iniciar transmissão de dados
Wire.beginTransmission(MPU);
Wire.write(0x3B);// onde comeƧa a receber os dados
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true); // Solicita os dados ao sensor somente de aceleração
// Armazena o valor dos sensores nas variaveis correspondentes
AccX = Wire.read() << 8 | Wire.read();
AccY = Wire.read() << 8 | Wire.read();
AccZ = Wire.read() << 8 | Wire.read();
// Imprime na Serial os valores obtidos
/* Alterar divisão conforme fundo de escala escolhido:
AcelerƓmetro
+/-2g = 16384
+/-4g = 8192
+/-8g = 4096
+/-16g = 2048
*/
// Envia os dados para o Excel usando os comandos do PLX DAQ v2
//Serial.print("DATA,"); // Comando para enviar dados para o Excel
///Serial.print(TempoDeCorrido);
//Serial.print(","); // Separador de colunas
//Serial.println((AccX / 2048) * 9.81);// Aceleração X em m/s^2
// Serial.print(" "); // Separador de colunas
// Serial.print((AccY / 2048) * 9.81);// Aceleração Y em m/s^2
//Serial.print(" "); // Separador de colunas
Serial.println((AccZ / 2048.0) * 9.81);// Aceleração Y em m/s^2
count++;
TempoDeCorrido += 0.1; // Incrementa o tempo
TempoAntes = TempoAtual;
}
if (count >= 1000) {
unsigned long timeNow = millis();
Serial.print("Readings: ");
Serial.print(count);
Serial.print(" Time:");
Serial.print(timeNow - prevTime);
Serial.println("ms");
delay(1000);
prevTime = millis();
count = 0;
}
}
I think the MPU 6050 sensor I have is defective or faulty because my ADXL 345 works normally using the same logic I used with the MPU 6050. Thanks to everyone who helped me, I will purchase another MPU 6050 from a more reliable seller.