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.
- Connect the MPU6050 GND pin to microbit GND pin.
- Connect the MPU6050 VCC pin to microbit VCC pin.
- Connect the MPU6050 serial data pin (SDA) to microbit pin 20.
- 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.
- 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, I suggest to name it with descriptive name such as "gyro-mpu6050-test".
- Click the "Extensions" block just under the "Math" block.
- Type https://github.com/gbantique/brickcell-gyro-mpu6050/ on 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.
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}
- 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
- 016 - Brickcell 8x16 Dot Matrix Display | MakeCode Microbit
- 015 - Brickcell 8x8 Dot Matrix Display | MakeCode Microbit
- 014 - Brickcell CO2 SGP30 | MakeCode Microbit
- 013 - Brickcell Gesture APDS9960 | MakeCode Microbit
- 012 - Brickcell Rotary Encoder | MakeCode Microbit
- 011 - Brickcell RTC DS3231 | MakeCode Microbit
- 009 - Brickcell TOF VL53L0X | MakeCode Microbit
- 008 - Brickcell Color TCS34725 | MakeCode Microbit
- 007 - Brickcell Pressure BMP280 | MakeCode Microbit
- 006 - Brickcell Weight HX711 | MakeCode Microbit
- 005 - Brickcell Fine Dust | MakeCode Microbit
- 004 - Brickcell Ultrasonic HCSR04 | MakeCode Microbit
- 003 - Brickcell NTC Temperature | MakeCode Microbit
- 002 - Brickcell DHT11 | MakeCode Microbit
- 001 - Brickcell LCD I2C | MakeCode Microbit
No comments yet!