Mastering Arduino I2C Communication: Temperature Data Tutorial
Published on January 14, 2025 | By ArduinoUnoProjex
Unlock the power of Arduino I2C communication with this master-slave temperature monitoring project! Using two Arduino boards, a temperature sensor, and the I2C protocol, you’ll transmit real-time temperature data to control an LED alert. Perfect for beginners and advanced makers, this step-by-step guide covers wiring, code, and applications like home automation. Dive into I2C Arduino projects with our recommended products and expert tips!
Note: Amazon product links are included for convenience (not ours, just recommendations). ELEGOO boards are not Arduino but are compatible with Arduino IDE and much more affordable.
Why Use I2C for Arduino Projects?
What is I2C communication in Arduino? I2C (Inter-Integrated Circuit) is a two-wire protocol (SDA and SCL) that enables efficient data transfer between multiple devices, ideal for Arduino temperature monitoring and automation projects.
Benefits: Uses minimal pins, supports multiple devices, and simplifies complex setups like master-slave communication.
Components Needed
To build this Arduino I2C temperature project, gather these components:
- ✅2 Arduino Boards (e.g., Arduino Uno or ELEGOO Uno R3)
- ✅Temperature Sensor (e.g., LM35 or TMP36, analog)
- ✅LED (with 220Ω resistor)
- ✅Breadboard & Jumper Wires
- ✅USB Cables (for both Arduinos)
Optional: USB-to-Serial module (e.g., CH340) for debugging.
Wiring the I2C Temperature System
How do I connect two Arduinos for I2C communication? Follow these schematics and instructions to wire the master and slave Arduinos.
Master Arduino Connections
Slave Arduino Connections
Connection Table
Component | Master Arduino Pin | Slave Arduino Pin |
---|---|---|
SDA | A4 | A4 |
SCL | A5 | A5 |
GND | GND | GND |
Temperature Sensor | A1 (Signal), 5V, GND | - |
LED | - | 13 (via 220Ω resistor) |
Wiring Instructions
Connect I2C Bus: Link Master A4 (SDA) to Slave A4, and Master A5 (SCL) to Slave A5. Connect GNDs of both Arduinos.
Wire Master: Connect the temperature sensor’s signal pin to A1, VCC to 5V, and GND to Arduino GND.
Wire Slave: Connect an LED anode to pin 13 via a 220Ω resistor and cathode to GND.
Power Arduinos: Use USB cables to connect both Arduinos to your computer.
💡Tip: Use pull-up resistors (4.7kΩ) on SDA and SCL lines if communication is unstable.
Arduino Code for I2C Temperature Transmission
What code enables I2C communication between two Arduinos? Below are the master and slave codes for temperature data transmission.
Master Arduino Code
#include
#define SENSOR_PIN A1
#define SLAVE_ADDRESS 8
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(SENSOR_PIN);
float voltage = sensorValue * (5.0 / 1023.0);
int temperature = (voltage - 0.5) * 100; // For LM35
Wire.beginTransmission(SLAVE_ADDRESS);
Wire.write(temperature >> 8); // High byte
Wire.write(temperature & 0xFF); // Low byte
Wire.endTransmission();
Serial.print("Temperature: ");
Serial.println(temperature);
delay(1000);
}
Slave Arduino Code
#include
#define LED_PIN 13
#define SLAVE_ADDRESS 8
#define TEMP_THRESHOLD 30
void setup() {
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
}
void receiveEvent(int bytes) {
int temperature = (Wire.read() << 8) | Wire.read();
Serial.print("Received Temperature: ");
Serial.println(temperature);
digitalWrite(LED_PIN, temperature > TEMP_THRESHOLD ? HIGH : LOW);
}
void loop() {
delay(100);
}
Code Breakdown
- Master: Reads LM35 sensor data, converts to Celsius, splits into two bytes, and sends via I2C.
- Slave: Receives bytes, reconstructs temperature, logs to Serial Monitor, and toggles LED if temperature exceeds 30°C.
- Wire Library: Handles I2C communication with
Wire.begin()
,Wire.write()
, andWire.read()
.
Keywords: Arduino I2C communication code, master-slave Arduino project, temperature sensor I2C Arduino.
Testing the I2C Temperature System
How do I test Arduino I2C communication? Follow these steps to verify the system.
Upload Code: Upload the master code to one Arduino and the slave code to the other via Arduino IDE.
Check Wiring: Ensure SDA, SCL, and GND connections are secure.
Monitor Output: Open Serial Monitors for both Arduinos (9600 baud). The master should display temperature, and the slave should show received values.
Test LED: Heat the sensor (e.g., with fingers). The LED on the slave should light if the temperature exceeds 30°C.
Expected Result: Master sends temperature every second; slave logs it and controls the LED based on the threshold.
💡Tip: If data isn’t received, check I2C address (8) and pull-up resistors.
Recommended Products
Start your Arduino I2C project with these high-quality components from Amazon (affiliate links). ELEGOO boards are Arduino IDE-compatible and budget-friendly.
Troubleshooting Tips
- ⚠️No Data Received: Verify SDA/SCL connections, I2C address (8), and add 4.7kΩ pull-up resistors.
- ⚠️Incorrect Temperature: Check sensor calibration (e.g., LM35 outputs 10mV/°C) and conversion formula.
- ⚠️LED Not Toggling: Ensure pin 13 is correctly wired and the threshold (30°C) is appropriate.
- ⚠️Serial Monitor Issues: Confirm both Arduinos are set to 9600 baud.
Advanced Project Ideas
- 🚀Add a display (e.g., OLED) to the slave for real-time temperature visuals.
- 🚀Integrate multiple sensors (e.g., humidity) via I2C for a multi-sensor hub.
- 🚀Connect to Wi-Fi (e.g., ESP8266) to log data to a cloud platform.
- 🚀Implement a fan control system based on temperature thresholds.
Frequently Asked Questions
How do I use I2C communication with Arduino?
Connect SDA (A4) and SCL (A5) between master and slave Arduinos, share GND, use the Wire library, and assign a unique I2C address to the slave.
Why is my slave Arduino not receiving data?
Check SDA/SCL connections, ensure the slave address matches (e.g., 8), and add 4.7kΩ pull-up resistors to SDA and SCL lines.
Can I use ELEGOO boards for I2C projects?
Yes, ELEGOO boards like Uno R3 or MEGA R3 are fully compatible with Arduino IDE and support I2C communication.
Conclusion
This Arduino I2C temperature monitoring project unlocks the potential of master-slave communication for real-time data transfer. You’ve learned to wire two Arduinos, transmit temperature data, and control an LED alert. Use our recommended products to start building, and explore our Arduino tutorials for more inspiration. Share your I2C creations in the comments and join the maker community! 🌡️
0 Comments