Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h> // Include the I2C library
- #define TEMP_SENSOR_ADDR 0x48 // Temperature sensor I2C address
- #define HUMID_SENSOR_ADDR 0x40 // Humidity sensor I2C address
- void setup() {
- Wire.begin(); // Join I2C bus
- Serial.begin(9600); // Start serial communication
- }
- void loop() {
- // Read temperature
- Wire.beginTransmission(TEMP_SENSOR_ADDR);
- Wire.write(0x00); // Command to read temperature
- Wire.endTransmission();
- Wire.requestFrom(TEMP_SENSOR_ADDR, 2); // Request 2 bytes from temp sensor
- int temp = Wire.read() << 8 | Wire.read(); // Combine bytes
- Serial.print("Temperature: ");
- Serial.println(temp);
- // Read humidity
- Wire.beginTransmission(HUMID_SENSOR_ADDR);
- Wire.write(0x00); // Command to read humidity
- Wire.endTransmission();
- Wire.requestFrom(HUMID_SENSOR_ADDR, 2); // Request 2 bytes from humidity sensor
- int humid = Wire.read() << 8 | Wire.read(); // Combine bytes
- Serial.print("Humidity: ");
- Serial.println(humid);
- delay(1000); // Wait for 1 second before next reading
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement