Hi Guys,
The issue I'm having is (I'm pretty sure) some sort of conflict between the U8g2 library and the MPU6050_tokn library which can be found here: https://github.com/tockn/MPU6050_tockn
I'm using an UNO, connected via i2c to an MPU6050 (AD0 to GND, so address 0x68), and a cheapo 128x64 OLED. Using U8g2 to control the OLED.
Arduino IDE 1.8.16
U8g2 version 2.31.2
I have two sketches basically doing the same thing, which is to get the Y angle from the MPU6050 and display it on a 128x64 OLED (SSD1306 driver).
I was playing with the U8g2 library to sort of compare between the frame buffer and page buffer.
The frame buffer code:
#include <MPU6050_tockn.h>
#include <Wire.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
int y = 0;
int oldy = 90;
unsigned long justDisplayed = 0;
unsigned long displayInterval = 200;
//MPU6050 mpu6050(Wire, 0.02, 0.98);
MPU6050 mpu6050(Wire);
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
void draw1(void) {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setCursor(40, 60);
u8g2.print(y);
u8g2.sendBuffer(); // transfer internal memory to the display
}
void draw2(void) {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setCursor(20, 60);
u8g2.print(y);
u8g2.sendBuffer(); // transfer internal memory to the display
}
void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
// mpu6050.calcGyroOffsets(true); // If uncommented the values for Y from the MPU are incorrect
mpu6050.setGyroOffsets(2.5, 7.48, 1.16); // If uncommented all is fine
u8g2.begin();
u8g2.setFont(u8g2_font_inb53_mn);
}
void loop() {
mpu6050.update();
y = abs(mpu6050.getAngleY());
Serial.println(y);
if (y != oldy && (millis() - justDisplayed) >= displayInterval) {
oldy = y;
justDisplayed = millis();
if (y <= 9) {
draw1();
}
if (y >= 10) {
draw2();
}
}
}
The problem is stemming from these lines:
// mpu6050.calcGyroOffsets(true); // If uncommented the values for Y from the MPU are incorrect
mpu6050.setGyroOffsets(2.5, 7.48, 1.16); // If uncommented all is fine
If I uncomment the "calcGyroOffsets(true)" line and comment the "setGyroOffsets" line then the Y variable outputs mostly at zero, with the odd jump to a random number. However, it does run through the loop. The strange Y output is shown on both the display and the serial monitor.
If I then reverse the above, as shown in the full sketch above, then all works fine. I get the correct numbers on the serial monitor and on the display.
Also, the order of the U8g2 constructor call seems to be important. If before the MPU6050 call then the program just hangs during calibration and never gets to the loop and keeps resetting. But, again, if I change to the setGyroOffsets then all is good. Strange.
On the flipside. The page buffer sketch works fine no matter the order of the constructor call or the calibration lines.
Here's the page buffer sketch:
#include <MPU6050_tockn.h>
#include <Wire.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
int y = 0;
int oldy = 90;
unsigned long justDisplayed = 0;
unsigned long displayInterval = 200;
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
MPU6050 mpu6050(Wire);
void draw1(void) {
u8g2.setCursor(40, 60);
u8g2.print(y);
}
void draw2(void) {
u8g2.setCursor(20, 60);
u8g2.print(y);
}
void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
// mpu6050.setGyroOffsets(2.5, 7.48, 1.16);
u8g2.begin();
u8g2.setFont(u8g2_font_inb53_mn);
}
void loop() {
mpu6050.update();
y = abs(mpu6050.getAngleY());
Serial.println(y);
if (y != oldy && millis() - justDisplayed >= displayInterval) {
oldy = y;
justDisplayed = millis();
if (y <= 9) {
u8g2.firstPage();
do {
draw1();
} while ( u8g2.nextPage() );
}
if (y >= 10) {
u8g2.firstPage();
do {
draw2();
} while ( u8g2.nextPage() );
}
}
}
Any clues or help as to why this is happening?
Or any suggestions on what to change?
It's not really a huge problem but I guess that I'm doing something wrong and I've spent quite some time trying to figure it out, so time to seek advice.
Thanks in advance of any assistance, and if you need more advice please ask.