hello, i need some help, i am attempting to make a G-force read out similar to an f1 style with the moving dot in the middle and then 2 rings marking 1g and 2gs. i am using an acceleromiter mpu6050 and a oled display sh110x 0x3c. i currently have the wiring set up like this
for the mpu6050
vcc-3.3v
ground - ground
scl- A5
sda- a4
and same for the sh110x oled
i currently have the g force x and y showing up in the serial moniter but it doesnt connect to the oled
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
//initionalize
Adafruit_MPU6050 mpu;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SH1106G display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Constants for g-force rings
const int RING1_RADIUS = 15;
const int RING2_RADIUS = 30;
const int RING3_RADIUS = 22;
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10); // Wait for Serial Monitor to open
}
// Initialize I2C communication and the MPU6050 sensor
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip. Check your wiring!");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.println("MPU6050 initialized and ready!");
delay(100);
display.clearDisplay();
display.display();
}
void loop() {
sensors_event_t accel;
sensors_event_t gyro;
sensors_event_t temp;
display.clearDisplay();
// Get new sensor events
mpu.getEvent(&accel, &gyro, &temp);
// Calculate G-force for X and Y axes and print them
float gForceX = accel.acceleration.x / 9.81; // Convert m/s² to G-force
float gForceY = accel.acceleration.y / 9.81; // Convert m/s² to G-force
float resultantForce = sqrt(pow(gForceX, 2) + pow(gForceY, 2));
// Display the G-force values in the Serial Monitor
Serial.print("G-Force (X): ");
Serial.print(gForceX, 2); // Display with 2 decimal places
Serial.print(" G, G-Force (Y): ");
Serial.print(gForceY, 2); // Display with 2 decimal places
Serial.println(" G");
Serial.println(resultantForce);
display.drawCircle(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, RING1_RADIUS, SH110X_WHITE);
// display.drawCircle(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, RING2_RADIUS, SH110X_WHITE);
// display.setCursor(SCREEN_WIDTH - 40, 0);
// display.setTextSize(1);
// display.setTextColor(SH110X_WHITE);
// display.print(resultantForce, 2);
// display.print(" G");
// Update the display
// display.display();
delay(500); // Delay to make the output more readable
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.