当前位置: 首页 > ds >正文

MPU6050 六轴姿态 Arduino ESP32 Test

在物联网和嵌入式系统开发领域,Arduino 是一款广受欢迎的开源电子原型平台,它具有简单易用、成本低廉等优点。而 MPU6050 是一款集成了三轴加速度计和三轴陀螺仪的传感器,能够测量物体的运动和姿态信息。本文将详细介绍如何使用 Arduino 与 MPU6050 传感器进行通信,实现传感器数据的采集与处理,同时还会实现一个 LED 定时闪烁的功能,以展示 Arduino 的多任务处理能力。

硬件准备
  • Arduino 开发板(如 Arduino Uno)
  • MPU6050 传感器模块
  • 超声波测距模块(可选,代码中使用了超声波测距相关引脚定义)
  • LED 灯
  • 杜邦线若干

https://github.com/fishros/MPU6050_lighthttps://github.com/fishros/MPU6050_light
FishBot主控板原理图开源分享! | 鱼香ROS

FishBot主控板原理图开源分享! | 鱼香ROShttps://fishros.org.cn/forum/topic/894/fishbot%E4%B8%BB%E6%8E%A7%E6%9D%BF%E5%8E%9F%E7%90%86%E5%9B%BE%E5%BC%80%E6%BA%90%E5%88%86%E4%BA%AB

#include <Arduino.h>
#include <Wire.h>
#include <MPU6050_light.h>MPU6050 mpu(Wire);
// put function declarations here:
int myFunction(int, int);#define TRIG 27
#define ECHO 21
#define LED_BUILTIN 2unsigned long previousMillis = 0; // 用于记录上一次电平切换的时间
const long interval = 500; // 定时器间隔,单位为毫秒void setup() {// put your setup code here, to run once:int result = myFunction(2, 3);Serial.begin(115200);pinMode(TRIG, OUTPUT);pinMode(ECHO, INPUT);pinMode(LED_BUILTIN, OUTPUT);Wire.begin(18,19); // SDA, SCLbyte status = mpu.begin();Serial.print(F("MPU6050 status: "));Serial.println(status);while(status != 0) {// stop everything if could not connect to MPU6050}mpu.calcOffsets(true, true); // gyro and acceleroSerial.println("Done!\n");}unsigned long timer = 0;
void loop() {// put your main code here, to run repeatedly:digitalWrite(TRIG, HIGH);delayMicroseconds(10);digitalWrite(TRIG, LOW);double duration = pulseIn(ECHO, HIGH);double distance = duration * 0.034 / 2;//Serial.printf("Distance: %lf cm\n", distance);mpu.update();if((millis()-timer)>1000){// 温度Serial.printf("Temp: %lf\n", mpu.getTemp());// 加速度计数据Serial.printf("Accel: %lf %lf %lf\n", mpu.getAccX(), mpu.getAccY(), mpu.getAccZ());// 加速度计角度Serial.printf("Angle: %lf %lf %lf\n", mpu.getAngleX(), mpu.getAngleY(), mpu.getAngleZ());// 陀螺仪数据Serial.printf("Gyro: %lf %lf %lf\n", mpu.getGyroX(), mpu.getGyroY(), mpu.getGyroZ());// 综合角度Serial.printf("Angle: %lf %lf %lf\n", mpu.getAngleX(), mpu.getAngleY(), mpu.getAngleZ());Serial.println("===========================\n");timer = millis();}// 获取当前时间unsigned long currentMillis = millis();// 检查是否达到定时器间隔if (currentMillis - previousMillis >= interval) {// 记录当前时间previousMillis = currentMillis;// 切换2号脚的电平digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));}
}// put function definitions here:
int myFunction(int x, int y) {return x + y;
}    

http://www.xdnf.cn/news/4486.html

相关文章:

  • 使用pyenv安装Python指南
  • C++ vector 介绍与使用
  • 【Fifty Project - D23】
  • 可视化图解算法33:判断是不是平衡二叉树
  • C++自动重连机制设计与实现指南
  • 融智学核心理论的数学化表达(之二)
  • Java中的Classpath 包含哪些目录?
  • 为什么800*800mm防静电地板“应用较少
  • 基于@ConfigurationProperties+@EnableConfigurationProperties的配置管理-笔记
  • Qt/C++面试【速通笔记七】—Qt中为什么new QWidget不需要手动调用delete?
  • 资产管理系统选型避坑:2025年核心技术趋势洞察
  • 求组合数【递推+快速幂+卢卡斯+线性筛】
  • AAAI2025论文整理-数字人驱动方向
  • spark 的流量统计案例
  • android-ndk开发(8): ndk 和 clang 版本对照表
  • 北京华锐视点邀您参与2025数字显示与元宇宙博览会【5月10-12日】
  • 浅谈Vue2 与 Vue3 的区别
  • 前端流行框架Vue3教程:13. 组件传递数据_Props
  • 学习Linux的第三天
  • 某振动分析系统的参数交叉核算
  • 解决 pnpm dev 运行报错的坎坷历程
  • 【第25节 性能指标计算】
  • 4.1框架应用
  • 系统架构师2025年论文《信息系统安全体系设计》
  • Xilinx DSP48E2 slice 一个周期能做几次float32浮点数乘法或者加法?如果是fix 32定点数呢?
  • “wsl --install -d Ubuntu-22.04”下载慢,中国地区离线安装 Ubuntu 22.04 WSL方法(亲测2025年5月6日)
  • python + whisper 读取蓝牙耳机, 转为文字
  • JavaScript 到命令和控制 (C2) 服务器恶意软件分析及防御
  • 三生原理是如何与狄利克雷定理兼容的?
  • 使用docker配置Mysql