Project updated to beta 6.0.1 (released on December 20th, 2021)
In this project we're going to learn how to use an MPU6050 sensor to detect slight inclinations on the x and y axis and the LED on the higher slope will turn on. Most of the components needed to build this project is included in the Wilderness Labs Meadow F7 w/Hack Kit Pro.
MPU6050 sensors are capable of providing much more data since they're packed with a 6 axis accelerometer and gyro, which are commonly used to quadrocopter drones and RC planes. You can easily communicate with them using I2C serial communication via serial clock (SCL) and data (SDA). We'll write the logic for the sensor and LEDs using Meadow.Foundation.
Meadow.Foundationa platform for quickly and easily building connected things using.NET on Meadow. Created by Wilderness Labs, it's completely open source and maintained by the Wilderness Labs community.
If you're new working with Meadow, I suggest you go to the Getting Started w/ Meadow by Controlling the Onboard RGB LEDproject to properly set up your development environment.
Step 1 - Assemble the circuitWire your LEDs and Mpu6050 project like the following circuit diagram:
Create a new Meadow Application project in Visual Studio 2019 for Windows or macOS and name it RotationDetector.
Step 3 - Add the required NuGet packagesFor this project, search and install the following NuGet packages:
Step 4 - Write the code for RotationDetectorIn your MeadowApp class, copy the following code below:
// public class MeadowApp : App<F7Micro, MeadowApp> <- If you have a Meadow F7v1.*
public class MeadowApp : App<F7MicroV2, MeadowApp>
{
Led up;
Led down;
Led left;
Led right;
Mpu6050 mpu;
public MeadowApp()
{
Initialize();
mpu.StartUpdating(TimeSpan.FromMilliseconds(100));
}
void Initialize()
{
var onboardLed = new RgbPwmLed(
device: Device,
redPwmPin: Device.Pins.OnboardLedRed,
greenPwmPin: Device.Pins.OnboardLedGreen,
bluePwmPin: Device.Pins.OnboardLedBlue);
onboardLed.SetColor(Color.Red);
up = new Led(Device.CreateDigitalOutputPort(Device.Pins.D13));
down = new Led(Device.CreateDigitalOutputPort(Device.Pins.D10));
left = new Led(Device.CreateDigitalOutputPort(Device.Pins.D11));
right = new Led(Device.CreateDigitalOutputPort(Device.Pins.D12));
mpu = new Mpu6050(Device.CreateI2cBus());
mpu.Updated += MpuUpdated;
onboardLed.SetColor(Color.Green);
}
void MpuUpdated(object sender, IChangeResult<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, Temperature? Temperature)> e)
{
up.IsOn = e.New.Acceleration3D?.Y.CentimetersPerSecondSquared < -50;
down.IsOn = e.New.Acceleration3D?.Y.CentimetersPerSecondSquared > 100;
left.IsOn = e.New.Acceleration3D?.X.CentimetersPerSecondSquared > 50;
right.IsOn = e.New.Acceleration3D?.X.CentimetersPerSecondSquared < -100;
}
}
In
the MeadowApp's constructor we initialize all the LEDs and the MPU6050 sensor. Notice that for the LEDs we simply pass digital output port by calling Device.CreateDigitalOutputPort(IPin pin)
along with Pin each LED is connected to, and for the Mpu6050 sensor, all we need to pass is a I2C bus calling Device.CreateI2cBus()
.
Click the Run button in Visual Studio. It should look like to the following GIF:
This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Meadow.Foundation.
- It comes with a huge peripheral driver library with drivers for the most common sensors and peripherals.
- The peripheral drivers encapsulate the core logic and expose a simple, clean, modern API.
- This project is backed by a growing community that is constantly working on building cool connected things and are always excited to help new-comers and discuss new projects.
Comments