Created
October 15, 2022 21:34
-
-
Save chicks/7db0b86abc74e6351d9c30ade2fe0247 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Basic OLED demo for accelerometer readings from Adafruit MPU6050 | |
// ESP32 Guide: https://RandomNerdTutorials.com/esp32-mpu-6050-accelerometer-gyroscope-arduino/ | |
// ESP8266 Guide: https://RandomNerdTutorials.com/esp8266-nodemcu-mpu-6050-accelerometer-gyroscope-arduino/ | |
// Arduino Guide: https://RandomNerdTutorials.com/arduino-mpu-6050-accelerometer-gyroscope/ | |
#include <Adafruit_MPU6050.h> | |
#include <Adafruit_SSD1306.h> | |
#include <Adafruit_Sensor.h> | |
#include <driver/adc.h> | |
#include "esp_adc_cal.h" | |
#define ADC_EN 14 //ADC_EN is the ADC detection enable port | |
#define ADC_PIN 34 | |
int vref = 1100; | |
float battery_voltage = 10; | |
float usb_voltage = 0; | |
Adafruit_MPU6050 mpu; | |
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire); | |
void showVoltage() | |
{ | |
digitalWrite(14, HIGH); | |
delay(1); | |
float measurement = (float) analogRead(34); | |
battery_voltage = (measurement / 4095.0) * 7.26; | |
digitalWrite(14, LOW); | |
float usb_measurement = (float) analogRead(14); | |
usb_voltage = (usb_measurement / 4095.0) * 7.26; | |
} | |
void setup() { | |
Serial.begin(115200); | |
// while (!Serial); | |
Serial.println("MPU6050 OLED demo"); | |
if (!mpu.begin()) { | |
Serial.println("Sensor init failed"); | |
while (1) | |
yield(); | |
} | |
Serial.println("Found a MPU-6050 sensor"); | |
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally | |
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64 | |
Serial.println(F("SSD1306 allocation failed")); | |
for (;;) | |
; // Don't proceed, loop forever | |
} | |
display.display(); | |
delay(500); // Pause for 2 seconds | |
display.setTextSize(1); | |
display.setTextColor(WHITE); | |
display.setRotation(0); | |
esp_adc_cal_characteristics_t adc_chars; | |
esp_adc_cal_value_t val_type = esp_adc_cal_characterize((adc_unit_t)ADC_UNIT_1, (adc_atten_t)ADC_ATTEN_DB_2_5, (adc_bits_width_t)ADC_WIDTH_BIT_12, 1100, &adc_chars); | |
pinMode(14, OUTPUT); | |
} | |
void loop() { | |
sensors_event_t a, g, temp; | |
mpu.getEvent(&a, &g, &temp); | |
display.clearDisplay(); | |
display.setCursor(0, 0); | |
showVoltage(); | |
display.println("Voltage: "); | |
display.print(battery_voltage); | |
display.print(" Batt. "); | |
display.print(usb_voltage); | |
display.print(" USB"); | |
display.println(""); | |
Serial.print("Accelerometer "); | |
Serial.print("X: "); | |
Serial.print(a.acceleration.x, 1); | |
Serial.print(" m/s^2, "); | |
Serial.print("Y: "); | |
Serial.print(a.acceleration.y, 1); | |
Serial.print(" m/s^2, "); | |
Serial.print("Z: "); | |
Serial.print(a.acceleration.z, 1); | |
Serial.println(" m/s^2"); | |
display.println("Accel (m/s^2): "); | |
display.print(a.acceleration.x, 1); | |
display.print(", "); | |
display.print(a.acceleration.y, 1); | |
display.print(", "); | |
display.print(a.acceleration.z, 1); | |
display.println(""); | |
Serial.print("Gyro. "); | |
Serial.print("X: "); | |
Serial.print(g.gyro.x, 1); | |
Serial.print(" rps, "); | |
Serial.print("Y: "); | |
Serial.print(g.gyro.y, 1); | |
Serial.print(" rps, "); | |
Serial.print("Z: "); | |
Serial.print(g.gyro.z, 1); | |
Serial.println(" rps"); | |
display.println("Gyro (rps): "); | |
display.print(g.gyro.x, 1); | |
display.print(", "); | |
display.print(g.gyro.y, 1); | |
display.print(", "); | |
display.print(g.gyro.z, 1); | |
display.println(""); | |
display.println("Temp: "); | |
display.print(temp.temperature); | |
display.print(" C, "); | |
display.print((temp.temperature * 9/5) + 32); | |
display.print(" F"); | |
display.display(); | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment