环境检测

姜智浩 Lv4

开发板

Arduino UNO R3

接线

BMP180

VCC → 3.3V 或 5V
GND → GND
SCL → A5
SDA → A4

OLED (SSD1306 I2C)

VCC → 3.3V 或 5V
GND → GND
SCL → A5
SDA → A4

报错信息

这两个的SDA和SCL都接A4和A5 可以共用的

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// OLED 设置
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// BMP180 传感器对象
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

void setup() {
Serial.begin(9600);

// 初始化 OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED ERROR");
while (1);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);

// 初始化 BMP180
if (!bmp.begin()) {
Serial.println("can't find BMP180");
while (1);
}
}

void loop() {
sensors_event_t event;
bmp.getEvent(&event);

display.clearDisplay();
display.setCursor(0, 0);
display.println("Environment");

if (event.pressure) {
display.setCursor(0, 20);
display.print("ATM: ");
display.print(event.pressure);
display.println(" hPa");

float temperature;
bmp.getTemperature(&temperature);

display.setCursor(0, 40);
display.print("temperature: ");
display.print(temperature);
display.println(" C");
} else {
display.setCursor(0, 20);
display.println("ERROR");
}

display.display();
delay(1000); // 每秒刷新一次
}
  • Title: 环境检测
  • Author: 姜智浩
  • Created at : 2025-04-21 11:45:14
  • Updated at : 2025-04-21 10:42:41
  • Link: https://super-213.github.io/zhihaojiang.github.io/2025/04/21/20250421环境检测/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
环境检测