Arduino 之 MPU6050模块

 1 /*
 2 需要 Adafruit_Sensor.h & Adafruit_BMP280.h
 3 需要将 Adafruit_BMP280.h文件中37行改成 #define BMP280_ADDRESS (0x76)
 4 #BMP280 pin 转换 说明:
 5 VCC---3.3v
 6 GND---gnd
 7 SCL---13
 8 SDA---11
 9 CSB---10
10 SDO---12
11 */
12 /*-------------------------库加载--------------*/
13 #include <Wire.h>
14 #include <SPI.h>
15 //Adafruit库
16 #include <Adafruit_Sensor.h> 
17 #include <Adafruit_BMP280.h>
18 //MPU6050库
19 #include <I2Cdev.h>
20 #include <MPU6050.h>
21 //WS2812库
22 #include <FastLED.h>
23 /*-------------------------引脚配置--------------*/
24 #define BMP_SCK 13      //BMP280硬件连接引脚
25 #define BMP_MISO 12
26 #define BMP_MOSI 11 
27 #define BMP_CS 10
28 //MPU6050 SDA SCL连接于A4 A5引脚,不需要此处定义
29 //WS2812引脚
30 #define DATA_PIN 9               // Arduino输出控制信号引脚
31 /*-------------------------模块配置--------------*/
32 //BMP280连接模式配置:
33 //Adafruit_BMP280 bmp; // I2C模式
34 Adafruit_BMP280 bmp(BMP_CS); // 硬件SPI模式
35 //Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);//模拟SPI模式
36 //MPU6050模式配置:
37 MPU6050 accelgyro;
38 int16_t ax, ay, az;
39 int16_t gx, gy, gz;
40 //WS2812配置:
41 #define NUM_LEDS 8               // LED灯珠数量
42 #define LED_TYPE WS2812          // LED灯带型号
43 #define COLOR_ORDER GRB          // RGB灯珠中红色、绿色、蓝色LED的排列顺序
44 uint8_t max_bright = 200;       // LED亮度控制变量,可使用数值为 0 ~ 255, 数值越大则光带亮度越高
45 CRGB leds[NUM_LEDS];            // 建立光带leds
46 /*-------------------------初始化配置--------------*/
47 void setup() {
48   //基本初始化:
49   Serial.begin(115200);
50   Wire.begin();
51   Serial.println("初始化I2C设备...");
52   accelgyro.initialize();
53   Serial.println("测试I2C设备连接...");
54   Serial.println(accelgyro.testConnection() ? "MPU6050 连接成功!" : "MPU6050 连接失败!");
55   LEDS.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);  // 初始化光带 
56   FastLED.setBrightness(max_bright);                            // 设置光带亮度
57   ;
58 //BMP280初始化
59   Serial.println(F("BMP280 初始化..."));
60   if (!bmp.begin()) {  
61     Serial.println(F("Error:找不到BMP280/SPI/3.3V传感器"));
62     while (1);
63   }
64   Serial.println(F("BMP280初始数据:"));
65 
66 }
67 int MICPin = 1;
68 
69 void loop() {
70     float BMP28_temp = bmp.readTemperature();
71     float BMP28_kpa = bmp.readPressure() / 1000;
72     float BMP28_m = bmp.readAltitude(1013.25);
73     int mic_v = analogRead(MICPin);
74 /*    Serial.println(F("温度:"));
75     Serial.println(BMP28_temp);
76     Serial.println(F("气压:"));
77     Serial.println(BMP28_kpa);
78     Serial.println(F("海拔:"));
79     Serial.println(BMP28_m);*/
80     Serial.println(mic_v);
81     fill_gradient(leds, 0, CHSV(0,0,0) , 7, CHSV(250,250,156), SHORTEST_HUES);
82     FastLED.show();
83     delay(50);
84 }
posted @ 2019-12-13 14:38  loadbxh  阅读(426)  评论(0)    收藏  举报