esp32引脚及其对应的gpio口如图,本文采用esp32采集压力传感器的数值,

连接如图,传感器的一个引脚连接至gpio12,另一个引脚连接至3v3供电引脚

代码如下:

const int sensorPin = 34; // 模拟传感器接到 GPIO34(ADC1 通道6)
int sensorValue = 0;      // 用于存储读取的值

void setup() {
  Serial.begin(115200);  // 初始化串口通信,用于调试
  pinMode(sensorPin, INPUT); // 设置传感器引脚为输入模式
  Serial.println("Sensor reading initialized...");
}

void loop() {
  // 读取模拟值
  sensorValue = analogRead(sensorPin);

  // 输出读取的原始值到串口
  Serial.print("Raw ADC Value: ");
  Serial.println(sensorValue);

  delay(1000); // 每秒读取一次
}

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐