Skip to content

Conflict between accelerometer MPU-9265 and Oled SSD1306 #1778

Closed
@joseandres94

Description

@joseandres94

Hardware:

Board: WEMOS TTGO ESP-32 Wifi Bluetooth 0.96" OLED
Core Installation/update date: 20/aug/2018
IDE name: Arduino IDE
Flash Frequency: 80Mhz
Upload Speed: 921600

Description:

Hi! I'm trying to solve a problem on my project but I can't find a solution for it.

I want to get data from an accelerometer (MPU-92/65) connected to the board by means of I2C on pins 21 (SDA) and 22 (SCL). Then, I would like to show the angles data obtained by the accelerometer on the OLED display integrated on the board. The OLED also communicates with the board by I2C, and it is internally connected to pins 4 (SCL) and 5 (SDA).

The angles readings are obtained fine. On the other hand, I can show any message on the screen. My problem comes when I try to set them on the same program. When I try this, the angles readings are obtained fine, but the screen turns off. Trying to fix it, I realized that if I comment the line "Wire.begin()" on the "setup()" function, then the screen finally turns on but the program is not able to read data from the accelerometer. I'm not sure if the problem is on the function "Wire.begin()" or is further than this fact.

I've been looking for a function which turns the I2C communication off, like "Wire.end()" but what I found didn't work for me. I would like to make them work simultaneously because I'm sure that it is possible. But if I couldn't make it, I would like to read data for an instant and then turn off the communication and show the data on the screen.

The libraries used are "SSD1306.h" and "OLEDDisplayUi.h" for the OLED display and "Wire.h" standard for I2C communication on Arduino.

Could anybody help me? I will go mad soon.

Thanks.

PS: There are probably some inconsistencies in the code. I am new using Arduino and some things I have copied and pasted into my program.

Sketch:

include <Wire.h> // Librería para comunicarse con el acelerómetro por I2C.
#include "SSD1306.h" // Librería para utilizar la pantalla OLED.
#include "OLEDDisplayUi.h"

SSD1306 display(0x3c, 5, 4); // Definimos la pantalla.
OLEDDisplayUi ui ( &display );

#define OLED_ADDRESS 0x3c // Dirección hexadecimal de la pantalla.
#define OLED_RST 16 // GPIO16 Pin reset de la pantalla.

// DEFINICIÓN DE ACELERÓMETRO
#define MPU9265_ADDRESS 0x68

#define ACC_FULL_SCALE_4_G 0x08

void I2Cread(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data){
Wire.beginTransmission(Address);
Wire.write(Register);
Wire.endTransmission();
Wire.requestFrom(Address, Nbytes);
uint8_t index = 0;
while (Wire.available())
Data[index++] = Wire.read();
}

void I2CwriteByte(uint8_t Address, uint8_t Register, uint8_t Data){
Wire.beginTransmission(Address);
Wire.write(Register);
Wire.write(Data);
Wire.endTransmission();
}

void setup() {
Serial.begin(9600);
Wire.begin();

pinMode(OLED_RST,OUTPUT);
digitalWrite(OLED_RST, LOW); // low to reset OLED
delay(50);
digitalWrite(OLED_RST, HIGH); // must be high to turn on OLED

display.init(); // Inicializamos pantalla.
display.setContrast(255);
display.setFont(ArialMT_Plain_16); // Estilo.
display.setTextAlignment(TEXT_ALIGN_LEFT); // Origen en la izquierda.

I2CwriteByte(MPU9265_ADDRESS, 28, ACC_FULL_SCALE_4_G);
}

void loop() {

// LECTURAS DEL ACELERÓMETRO EN LOS EJES X,Y,Z.
uint8_t Buf[14];
I2Cread(MPU9265_ADDRESS, 0x3B, 14, Buf);

int16_t ax = -(Buf[0] << 8 | Buf[1]);
int16_t ay = -(Buf[2] << 8 | Buf[3]);
int16_t az = -(Buf[4] << 8 | Buf[5]);

// CONVERSIÓN DE LAS LECTURAS.
// Conversión a aceleración (m/s^2)
float ax_conv=-ax9.80665/8207.066225+0.027654;
float ay_conv=-ay
9.80665/8226.456689-0.170296;
float az_conv=az*9.80665/8185.788079-0.144666;
if(ax_conv<0.3){
ax_conv=round(ax_conv);
}
if(ay_conv<0.3){
ay_conv=round(ay_conv);
}
if(az_conv<0.3){
az_conv=round(az_conv);
}

// Conversión de aceleración a orientación
  float Y = RAD_TO_DEG * (atan2(-ay_conv, -az_conv));  
  float X = RAD_TO_DEG * (atan2(-ax_conv, -az_conv));  
  float Z = RAD_TO_DEG * (atan2(-ay_conv, -ax_conv));

  int a = RAD_TO_DEG * (atan2(az_conv, ax_conv));  
  int b = RAD_TO_DEG * (atan2(ay_conv, ax_conv));

// Mostrar datos por pantalla
display.clear(); // Limpiar pantalla
display.display(); // Muestra lo que haya en buffer
display.drawString(0, 5, "Alpha= " + String(a));
display.display(); // Muestra lo que haya en buffer
display.drawString(0, 25, "Beta= " + String(b));
display.display(); // Muestra lo que haya en buffer
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions