Skip to content

11. Input devices

Group assignment:

  • Probe an input device(s)’s analog and digital signals

  • Document your work on the group work page and reflect on your individual page what you learned

To see our group assignment click here

Individual assignment:

  • add a sensor to a microcontroller board that you have designed and read it.

For this week’s assignment, we have chosen to use a MPU-6050 accelerometer module with the electronic board we made in Week 4 (Electronics production)

MPU-6050 accelerometer module

The MPU-6050 accelerometer module is a popular sensor used to measure linear acceleration and tilt. It combines an accelerometer and a gyroscope in a single integrated circuit, making it ideal for applications in motion control, virtual reality, orientation tracking and more. The MPU-6050 is capable of measuring accelerations of up to ±16g and angular velocities of up to ±2000 degrees per second. It usually communicates via the I2C or SPI bus with a microcontroller. This module is widely used in electronic projects and portable devices to detect movement and orientation.

The MPU-6050 accelerometer module is powered in 3.3V because we use it with an ESP32. Indeed the GPIO of the ESP32 receive 3.3V in input.

PROGRAMMING

To program our circuit board with our MPU-6050 accelerometer module, we will use an OLED screen to display the position coordinates.

      
 #include < Adafruit_MPU6050.h>
#include < Adafruit_MPU6050.h>
#include < Adafruit_SSD1306.h>
#include < Adafruit_Sensor.h>

Adafruit_MPU6050 mpu;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);

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 MPU6050 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);
}

void loop() {
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  display.clearDisplay();
  display.setCursor(0, 0);

  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("Accelerometer - 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("Gyroscope ");
  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("Gyroscope - 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.display();
  delay(100);
}

      

Link to the official code website

RESULTAT

After uploading the code on the card here is the result.

RECAP VIDEO

I invite you to watch this summary video.

WE HAVE FINISHED 👏👏 🥳🥳

THANK YOU FOR FOLLOWING THIS TUTORIAL 🤝🤝🤝

Helpful links :


Last update: July 17, 2024