010 - Brickcell Gyro MPU6050 | MakeCode Microbit

Introduction

The MPU6050 is a sensor module used in electronics and robotics. It combines a 3-axis accelerometer and a 3-axis gyroscope in a single package. This combination of sensors allows it to measure both linear motion (acceleration) and rotational motion (angular velocity) in three dimensions each. It is commonly used in projects involving motion tracking, orientation sensing, and stabilization applications.

The MPU6050 communicates with microcontrollers or other devices through interfaces like I2C (Inter-Integrated Circuit) or SPI (Serial Peripheral Interface) though only I2C is enabled in Brickcell kit. It's widely used in applications such as drones, quadcopters, gaming controllers, and wearable devices to sense motion and orientation.

Hardware Instruction

Let's explore how to connect the MPU6050 sensor to a micro:bit microcontroller and utilize MakeCode for programming.

  1. Connect the MPU6050 GND pin to microbit GND pin.
  2. Connect the MPU6050 VCC pin to microbit VCC pin.
  3. Connect the MPU6050 serial data pin (SDA) to microbit pin 20.
  4. Connect the MPU6050 serial clock pin (SCL) to microbit pin 19.

Software Instruction

Now, let's start into programming the micro:bit to utilize the VL53L0X TOF distance sensor.

  1. Login to https://makecode.microbit.org/ using your Microsoft account.
  2. Create a new project by clicking the "New Project" button. You may name it anything you want, I suggest to name it with descriptive name such as "gyro-mpu6050-test".
  3. Click the "Extensions" block just under the "Math" block.
  4. Type https://github.com/gbantique/brickcell-gyro-mpu6050/ on the search bar.
  5. Select the "brickcell-gyro-mpu6050" from the search results. The "gyro mpu6050" block should appear under the "Brickcell" block.
  6. Copy the code provided below.
 1// Initialize sensor for usage
 2Brickcell.initMPU6050()
 3
 4while (true) {
 5    // Output gyroscope values
 6    serial.writeLine("X Gyroscope: " + Brickcell.gyroscope(axisXYZ.x, gyroSen.range_250_dps) + " rad/s");
 7    serial.writeLine("Y Gyroscope: " + Brickcell.gyroscope(axisXYZ.y, gyroSen.range_250_dps) + " rad/s");
 8    serial.writeLine("Z Gyroscope: " + Brickcell.gyroscope(axisXYZ.z, gyroSen.range_250_dps) + " rad/s");
 9    serial.writeLine("-----------------------------------------------------------------------------");
10
11    // Output angle values
12    serial.writeLine("X Angle: " + Brickcell.axisRotation(axisXYZ.x, accelSen.range_2_g) + " Degree");
13    serial.writeLine("Y Angle: " + Brickcell.axisRotation(axisXYZ.y, accelSen.range_2_g) + " Degree");
14    serial.writeLine("Z Angle: " + Brickcell.axisRotation(axisXYZ.z, accelSen.range_2_g) + " Degree");
15    serial.writeLine("-----------------------------------------------------------------------------");
16
17    // Output acceleration values
18    serial.writeLine("X Acceleration: " + Brickcell.axisAcceleration(axisXYZ.x, accelSen.range_2_g) + " g");
19    serial.writeLine("Y Acceleration: " + Brickcell.axisAcceleration(axisXYZ.y, accelSen.range_2_g) + " g");
20    serial.writeLine("Z Acceleration: " + Brickcell.axisAcceleration(axisXYZ.z, accelSen.range_2_g) + " g");
21    serial.writeLine("-----------------------------------------------------------------------------");
22
23    // Output temperature value
24    serial.writeLine("Temperature: " + Brickcell.readTemperature() + " C");
25    serial.writeLine("-----------------------------------------------------------------------------");
26    pause(2000)
27}
  1. Open a Serial Monitor such as Termite terminal app https://www.compuphase.com/software_termite.htm/. Set the baud rate to 115200 bps.

Or you make a copy of my created project in your MakeCode workspace.

https://makecode.microbit.org/S44876-90738-59137-62113/

Expected Result

If you carefully follow the provided instruction above, you should be able to view the measured gyroscope, angle, and acceleration on the serial terminal every 2000 milliseconds.



Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.