-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode
More file actions
79 lines (58 loc) · 1.84 KB
/
Copy pathCode
File metadata and controls
79 lines (58 loc) · 1.84 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <Wire.h> // For I2C communication to LCD
#include <LiquidCrystal_I2C.h> // For 16x2 I2C LCD
#include <Adafruit_BME280.h> // Adafruit BME280 library
#include <SPI.h> // Required for SPI mode BME280
// -------------------------------
// SPI PIN DEFINITIONS (Uno)
// -------------------------------
// SCK = 13
// MISO = 12
// MOSI = 11
// CS = Any digital pin (we choose 10)
#define BME_CS 10 // Chip select for BME280 on SPI
// Create BME280 object in SPI mode
Adafruit_BME280 bme(BME_CS, SPI);
// Create LCD object: 0x27 is common, yours may be 0x3F
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// put your setup code here, to run once:
// Initialize serial for debugging
Serial.begin(9600);
// Initialize LCD
lcd.init(); // Start LCD
lcd.backlight(); // Turn on LCD backlight
// -------------------------------
// Initialize BME280 in SPI mode
// -------------------------------
bool status = bme.begin();
if (!status) {
lcd.clear();
lcd.print("BME280 ERROR");
Serial.println("Could not find BME280! Check wiring.");
while (1); // Halt so user notices the issue
}
// Display boot message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("BME280 (SPI)");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(1500);
}
void loop() {
// put your main code here, to run repeatedly:
// Read temperature and pressure from sensor
float temperature = bme.readTemperature(); // Celsius
float pressure = bme.readPressure() / 100.0; // Convert Pa → hPa
// Clear and update LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature, 1); // One decimal place
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Press: ");
lcd.print(pressure, 1);
lcd.print(" hPa");
delay(1000); // Update once per second
}