// 首先包含自己的库
// 再包含c++的库
// 再包含C的库
// 再包含外部的第三方库
// 再包含统同一模块中其他模块的头文件
// -------------- 头文件包含 ------------
#include <MPU6050.h>
#include <Wire.h>
#include "HX711.h"
// ----------- 全局变量定义 -------------
// 1. HC-SR04引脚设置
const int TRIG_PIN = 32;
const int ECHO_PIN = 34;
// 2. HX711 引脚设置
const int LOADCELL_DOUT_PIN = 4;
const int LOADCELL_SCK_PIN = 0;
HX711 scale;
// 3. MPU设置
MPU6050 mpu;
// 4. FSR402引脚
const int FSR_PIN = 25;
// ---------- 工具函数 -----------
// 距离测量函数
long measureDistance() {
// 发送10微秒的高脉冲到Trig引脚
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// 读取Echo引脚的高电平持续时间
long duration = pulseIn(ECHO_PIN, HIGH);
// 计算距离(声速约为340米/秒, 即每微妙0.034厘米)
// 距离 = (持续时间 * 声速) / 2 (因为声音需要往返)
return duration * 0.034 / 2;
}
// ----------- 函数初始化 ------------
// 1. HC-SR04 初始化
void InitializeHCSR04() {
// 设置 TRIG 为输出模式
pinMode(TRIG_PIN, OUTPUT);
// 设置 ECHO 为输入模式
pinMode(ECHO_PIN, INPUT);
Serial.println("HC-SR04 超声波传感器测试!");
Serial.println("-------------");
delay(1000);
}
// 2. HX7711初始化函数
void InitializeHX711(HX711 &scale) {
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
// 定义 校准因子(scale factor),用于把 HX711 输出的原始 ADC 值转换为实际重量(单位 kg)
float calibration_factor = 418.16;
// 设置 校准系数,用来把 HX711 的原始读数换算成 kg
scale.set_scale(calibration_factor);
// 清零,去皮
scale.tare();
Serial.println("HX711称重传感器初始化完成");
}
// 3. MPU初始化函数
void InitializeMPU() {
// MPU初始化设置
// 给MPU 指定SDA和SCL端口,因为其默认端口为21,22
Wire.begin(2, 15);
Serial.println("ESP32成功连接");
// 初始化 MPU6050
mpu.initialize();
Serial.println("MPU初始化成功~");
// MPU的状态检查
if (mpu.testConnection()) {
Serial.println("MPU6050 连接成功!");
} else {
Serial.println("fail! Please check the Line And I2C Address!");
while (1);
}
/*
为什么要设置量程:
给 MPU 设置量程,是为了在测量范围与灵敏度之间取得合适平衡,同时确保输出数据能正确换算为实际物理值。
+ 量程越小,灵敏度越高(分辨率更细,但更容易溢出)
+ 量程越大,灵敏度越低(可以测更剧烈的运动,但精度下降)
*/
// 给陀螺仪设置量程,这是可测的最大角速度范围,正负250度/s,这里是正负2000度/s
mpu.setFullScaleGyroRange(MPU6050_GYRO_FS_2000);
// 给加速度计设置量程,这个是正负2g,还有正负4g, 正负8g,正负16g
mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
Serial.println("MPU6050 初始化成功");
}
// 4. FSR402初始化
void InitializeFSR402() {
// 将 25 口设置为 INPUT模式
pinMode(FSR_PIN, INPUT);
Serial.println("FSR402压力传感器测试开始!");
}
// ---------- 执行函数 ---------------
void ExecHCSR04() {
// 测量距离
long distance = measureDistance();
// 打印距离信息
//Serial.printf("距离:%ld cm", distance);
Serial.print("距离:");
Serial.print(distance);
Serial.println(" cm");
// 根据距离显示不同提示
if (distance < 5) {
Serial.println(" - 非常近!");
} else if (distance < 20) {
Serial.println(" - 近距离");
} else if (distance < 100) {
Serial.println(" - 中距离");
} else if (distance < 400) {
Serial.println(" - 远距离");
} else {
Serial.println(" - 超出测量范围");
}
delay(500); // this speeds up the simulation
}
void ExecHX711() {
// 从 HX711 读取 10 次原始 ADC 值,取平均值提高稳定性,然后除以 calibration_factor 得到单位 kg 的重量
float weight = scale.get_units(10);
Serial.printf("重量:%.2f kg\n", weight);
delay(500); // this speeds up the simulation
}
void ExecMPU() {
// 三轴的加速度
int16_t ax, ay, az;
// 三轴的角速度
int16_t gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// 上述的六个值,拿到的都是数值,2g的数值是16384 LSB/g 单位是 LSB(Least Significant Bit),即纯数字,所以下面要统一单位进行一个换算
float accelX = ax / 16384.0;
float accelY = ay / 16384.0;
float accelZ = az / 16384.0;
// 陀螺仪:根据设置的量程 +-2000 dps, 灵敏度为 16.4 LSB/(度/s)
float gyroX = gx / 16.4;
float gyroY = gy / 16.4;
float gyroZ = gz / 16.4;
// 输出结果
Serial.printf("加速度 X:%.2f Y:%.2f Z:%.2f g\n", accelX, accelY, accelZ);
Serial.printf("陀螺仪 X:%.2f Y:%.2f Z:%.2f °/s\n", gyroX, gyroY, gyroZ);
Serial.println(("-----------------"));
delay(500); // this speeds up the simulation
}
void ExecFSR402() {
int rawValue = analogRead(FSR_PIN);
// 因为满电压为4095,板子供电为3.3v,所以要看adc值所占的比例来确定电压值
float voltage = rawValue * (3.3 / 4095.0);
Serial.printf("ADC值:%d | 电压值:%.2fV | 压力:\n", rawValue, voltage);
if (rawValue < 100) Serial.println("无压力");
else if (rawValue < 500) Serial.println("轻压");
else if (rawValue < 800) Serial.println("中压");
else Serial.println("重压");
delay(200); // this speeds up the simulation
}
// ------------ 全局初始化函数入口 ---------------
void setup() {
// 这是波特率为115200,这个数值需要发送和接收一样
Serial.begin(115200);
// 初始化 HC-SR04
InitializeHCSR04();
// 初始化HX711
InitializeHX711(scale);
// 初始化 MPU6050
InitializeMPU();
// 初始化FSR402
InitializeFSR402();
}
// ------------ 循环执行的函数入口 ---------------
void loop() {
// put your main code here, to run repeatedly:
// 1. 执行HC-SR04
ExecHCSR04();
// 2. 执行HX711
ExecHX711();
// 3. 执行MPU
ExecMPU();
// 4. 执行FSR402
ExecFSR402();
delay(10); // this speeds up the simulation
}