In this video lesson we will show you how to incorporate accelerometers into your Arduino projects. Your Sunfounder kit includes the GY-87 IMU module. This module contains a BMP180 pressure sensor, which we have already used in earlier lessons, and an MPU6050 6 axis IMU. The MPU6050 includes 3 accelerometers and 3 gyros. In today’s lesson, we learn how to use the MPU6050 accelerometers. I will explain how these MEMS bases accelerometers work, and how we can use them in our project.
This is the schematic we use in todays lesson.
For your convenience, the code developed in the lesson is presented below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <Adafruit_MPU6050.h> #include <Adafruit_Sensor.h> #include <Wire.h> float Ax; float Ay; Adafruit_MPU6050 mpu; void setup() { // put your setup code here, to run once: Serial.begin(115200); mpu.begin(); Serial.println("MPU6050 Started!"); mpu.setAccelerometerRange(MPU6050_RANGE_2_G); mpu.setFilterBandwidth(MPU6050_BAND_21_HZ); } void loop() { // put your main code here, to run repeatedly: sensors_event_t a, g, temp; mpu.getEvent(&a, &g, &temp); Ax=a.acceleration.x/9.81; Ay=a.acceleration.y/9.81; Serial.print(Ax); Serial.print(','); Serial.println(Ay); delay(50); } |