010 - MakeCode MicroBit: Brickcell Gyro MPU6050
Table of Contents
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 the 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.
- Connect the MPU6050 GND pin to micro:bit GND pin.
- Connect the MPU6050 VCC pin to micro:bit VCC pin.
- Connect the MPU6050 serial data pin (SDA) to micro:bit pin 20.
- Connect the MPU6050 serial clock pin (SCL) to micro:bit pin 19.
Software Instruction
Now, let's start programming the micro:bit to utilize the MPU6050 gyroscope and accelerometer sensor.
- Login to https://makecode.microbit.org/ using your Microsoft account.
- Create a new project by clicking the "New Project" button. You may name it anything you want, such as "gyro-mpu6050-test".
- Click the "Extensions" block just under the "Math" block.
- Type https://github.com/gbantique/brickcell-gyro-mpu6050/ in the search bar.
- Select the "brickcell-gyro-mpu6050" from the search results. The "gyro mpu6050" block should appear under the "Brickcell" block.
- Copy the code provided below.
// Initialize sensor for usage
Brickcell.initMPU6050()
while (true) {
// Output gyroscope values
serial.writeLine("X Gyroscope: " + Brickcell.gyroscope(axisXYZ.x, gyroSen.range_250_dps) + " rad/s");
serial.writeLine("Y Gyroscope: " + Brickcell.gyroscope(axisXYZ.y, gyroSen.range_250_dps) + " rad/s");
serial.writeLine("Z Gyroscope: " + Brickcell.gyroscope(axisXYZ.z, gyroSen.range_250_dps) + " rad/s");
serial.writeLine("-----------------------------------------------------------------------------");
// Output angle values
serial.writeLine("X Angle: " + Brickcell.axisRotation(axisXYZ.x, accelSen.range_2_g) + " Degree");
serial.writeLine("Y Angle: " + Brickcell.axisRotation(axisXYZ.y, accelSen.range_2_g) + " Degree");
serial.writeLine("Z Angle: " + Brickcell.axisRotation(axisXYZ.z, accelSen.range_2_g) + " Degree");
serial.writeLine("-----------------------------------------------------------------------------");
// Output acceleration values
serial.writeLine("X Acceleration: " + Brickcell.axisAcceleration(axisXYZ.x, accelSen.range_2_g) + " g");
serial.writeLine("Y Acceleration: " + Brickcell.axisAcceleration(axisXYZ.y, accelSen.range_2_g) + " g");
serial.writeLine("Z Acceleration: " + Brickcell.axisAcceleration(axisXYZ.z, accelSen.range_2_g) + " g");
serial.writeLine("-----------------------------------------------------------------------------");
// Output temperature value
serial.writeLine("Temperature: " + Brickcell.readTemperature() + " C");
serial.writeLine("-----------------------------------------------------------------------------");
pause(2000)
}
- Open a Serial Monitor such as the Termite terminal app. Set the baud rate to 115200 bps.
Or you can 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 instructions above, you should be able to view the measured gyroscope, angle, and acceleration values on the serial terminal every 2000 milliseconds.