开发板
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
代码
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
|
// OLED 设置
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); // 每秒刷新一次 }
|