Files
escape_gallery_obsidian_vault/Projects/Electronics/Building an I2C Temperature Controller.md
2026-07-11 16:59:05 -07:00

92 lines
2.9 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
active: false
tags: project
created: 2026-05-28 15:23
---
## Summary
Building an I2C temperature controller involves reading temperature from an I2C sensor (like an MCP9808 or DHT12) and using an Arduino to trigger a relay (for heaters/coolers) or adjust a fan. Use the standard `<Wire.h>` library for communication and control relays based on your temperature thresholds. 
Here is a straightforward guide to get your project running.
🛠️ Hardware Requirements
- **Microcontroller:** Arduino Uno or Nano
- **I2C Temperature Sensor:** MCP9808 (High precision) or DHT12
- **I2C Display (Optional):** 16x2 LCD with I2C backpack (PCF8574)
- **Control Element:** 5V Relay module (to switch a fan or heater)
- **Misc:** Breadboard, jumper wires, and 4.7kΩ pull-up resistors (if not built into your sensor modules) 
🔌 Wiring the Components
Connect your I2C devices to the Arduino's I2C bus. You will share these lines between the sensor and the display.
- **VCC**  Arduino 
- **GND**  Arduino 
- **SDA** Arduino Analog Pin (or the dedicated pin on newer boards)
- **SCL** Arduino Analog Pin (or the dedicated pin on newer boards)
- **Relay Signal Pin** Arduino Digital Pin 
Before running this code, ensure you have installed the **Adafruit MCP9808 Library** and the **LiquidCrystal_I2C Library** via the Arduino IDE Library Manager (`Sketch > Include Library > Manage Libraries`).
```
#include <Wire.h>
#include <Adafruit_MCP9808.h>
#include <LiquidCrystal_I2C.h>
// Create sensor and display objects
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
const int RELAY_PIN = 7;
const float TARGET_TEMP = 25.0; // Target temperature in Celsius
const float HYSTERESIS = 0.5; // Prevents relay rapid switching (chattering)
void setup() {
Serial.begin(9600);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Default to OFF (Relays are often active LOW)
// Initialize I2C LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("I2C Temp Ctrl");
// Initialize sensor
if (!tempsensor.begin(0x18)) { // Default I2C address for MCP9808
Serial.println("Couldn't find MCP9808!");
while (1);
}
tempsensor.setResolution(3); // Set resolution to 0.0625°C
}
void loop() {
tempsensor.wake(); // Wake up sensor
float tempC = tempsensor.readTempC();
tempsensor.shutdown(); // Shutdown to save power/reduce self-heating
// Update Display
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(tempC);
lcd.print("C");
// Temperature Control Logic
if (tempC > (TARGET_TEMP + HYSTERESIS)) {
digitalWrite(RELAY_PIN, LOW); // Turn ON cooling/heating
}
else if (tempC < (TARGET_TEMP - HYSTERESIS)) {
digitalWrite(RELAY_PIN, HIGH); // Turn OFF cooling/heating
}
delay(1000); // Read temperature every second
}
```
## Tasks
## References