基于arduino开发esp32的基本传感器代码
时间: 2023-11-24 16:05:46 浏览: 61
以下是基于Arduino开发ESP32的基本传感器代码示例,其中包含了DHT11温湿度传感器和光敏电阻传感器的读取:
DHT11温湿度传感器:
#include <DHT.h>
#define DHTPIN 4 // DHT11连接到ESP32的引脚
#define DHTTYPE DHT11 // 指定传感器类型
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float temp = dht.readTemperature(); // 读取温度值
float humi = dht.readHumidity(); // 读取湿度值
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print("C, Humidity: ");
Serial.print(humi);
Serial.println("%");
}
光敏电阻传感器:
int sensorPin = 34; // 光敏电阻连接到ESP32的引脚
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(sensorPin); // 读取光敏电阻的值
Serial.print("Light Sensor: ");
Serial.println(sensorValue);
delay(1000);
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)