ADC电压采集并串口输出
时间: 2023-09-19 11:05:03 浏览: 39
这是一个基本的ADC电压采集并串口输出的例程,使用Arduino UNO开发板和一片LM35温度传感器。
```
int sensorPin = A0; // 定义传感器引脚
float voltage; // 存储电压值
float temperature; // 存储温度值
void setup() {
Serial.begin(9600); // 初始化串口通信
}
void loop() {
int reading = analogRead(sensorPin); // 读取传感器值
voltage = reading * 5.0 / 1023.0; // 将传感器值转换为电压值
temperature = voltage * 100.0; // 将电压值转换为摄氏度温度值(假设传感器为LM35)
Serial.print("Voltage: "); // 输出电压值
Serial.print(voltage);
Serial.print("V, Temperature: "); // 输出温度值
Serial.print(temperature);
Serial.println("C");
delay(1000); // 延迟1秒钟
}
```
在上述程序中,首先定义了传感器引脚为A0,并声明了两个变量来存储电压值和温度值。在setup()函数中,初始化了串口通信。在loop()函数中,使用analogRead()函数读取传感器值,并将其转换为电压值和摄氏度温度值。最后,使用Serial.print()函数在串口上输出电压值和温度值,并延迟1秒钟。可以通过串口监视器来查看输出结果。