Arduino I2C Temperature Project
Arduino I2C Temperature Project
Are you interested in transmitting real-time temperature data between two Arduino boards? The solution lies in Arduino I2C communication. This tutorial provides a clear, step-by-step guide to building a master-slave temperature monitoring system using the I2C protocol. You'll gain practical knowledge about wiring, coding, and controlling an LED based on temperature readings. This guide provides all the necessary information, regardless of whether you are new to Arduino or aiming to enhance your existing skills.
Why Use I2C for Arduino Temperature Projects?
I2C (Inter-Integrated Circuit) is an excellent choice for Arduino projects due to its use of just two wires: SDA (data) and SCL (clock). It also allows one Arduino (the master) to communicate with one or more other devices (slaves). As a result, you conserve pins and maintain a clean setup.
I2C is ideal for temperature monitoring. It supports multiple sensors, functions effectively over short distances, and manages real-time data efficiently. Furthermore, the Arduino Wire library simplifies the coding process. This allows you to concentrate on building rather than debugging.
Key Benefits of Arduino I2C Communication
I2C communication offers several advantages. It requires only two pins—A4 (SDA) and A5 (SCL) on most Arduino Uno boards. It allows you to connect numerous devices to the same bus. It maintains neat wiring, reducing potential errors. It includes built-in error checking to ensure data reliability.
Parts You'll Need for This I2C Arduino Project
Gather the following components before starting the coding process. Fortunately, most are commonly found in starter kits.
- 2 Arduino Uno boards (or compatible boards like ELEGOO Uno R3)
- 1 analog temperature sensor (LM35 or TMP36)
- 1 LED
- 1 resistor (220Ω)
- Breadboard and jumper wires
- 2 USB cables
- Optional: 4.7kΩ pull-up resistors for SDA and SCL (if needed)
Note: ELEGOO boards are Arduino-compatible but not official Arduino products. They work with the Arduino IDE and are typically less expensive. Always verify your sensor type—LM35 and TMP36 use slightly different conversion formulas.
Wiring the Two Arduinos for I2C Communication
Correctly connecting your master and slave Arduinos is the next step. Following these steps will ensure your system operates correctly.
Step-by-Step I2C Wiring Instructions
First, establish the I2C bus connection between both boards. Connect Master A4 to Slave A4 (SDA). Then, connect Master A5 to Slave A5 (SCL). Also, connect the GND pins of both Arduinos together. This shared ground is crucial for stable communication.
Next, connect the temperature sensor to the master Arduino. Attach the sensor's signal pin to A1, VCC to 5V, and GND to the Arduino's GND.
After that, wire the LED on the slave Arduino. Connect the LED's long leg (anode) to pin 13 through a 220Ω resistor. Then, connect the short leg (cathode) to GND.
Finally, connect both Arduinos to your computer using USB cables. Power is supplied via USB, so an external power supply is not necessary for this demonstration.
💡 Pro Tip: If your I2C data appears unstable or glitchy, consider adding 4.7kΩ pull-up resistors from SDA to 5V and SCL to 5V. Many modern boards have these resistors built-in, but older ones might not.
Arduino I2C Code for Temperature Data Transfer
With the wiring complete, it's time to upload the code. Below are two sketches: one for the master (responsible for sending temperature data) and one for the slave (responsible for receiving data and controlling the LED).
Master Arduino Code (Temperature Sender)
#include <Wire.h>
#define SENSOR_PIN A1
#define SLAVE_ADDRESS 8
void setup() {
Wire.begin(); // Start I2C as master
Serial.begin(9600); // For debugging
}
void loop() {
int sensorValue = analogRead(SENSOR_PIN);
float voltage = sensorValue * (5.0 / 1023.0);
int temperature = (voltage - 0.5) * 100; // LM35 formula (°C)
Wire.beginTransmission(SLAVE_ADDRESS);
Wire.write(temperature >> 8); // Send high byte
Wire.write(temperature & 0xFF); // Send low byte
Wire.endTransmission();
Serial.print("Temperature: ");
Serial.println(temperature);
delay(1000); // Wait 1 second
}
Slave Arduino Code (LED Controller)
#include <Wire.h>
#define LED_PIN 13
#define SLAVE_ADDRESS 8
#define TEMP_THRESHOLD 30 // °C
void setup() {
Wire.begin(SLAVE_ADDRESS); // Start I2C as slave
Wire.onReceive(receiveEvent); // Call function when data arrives
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
}
void receiveEvent(int bytes) {
// Rebuild 16-bit integer from two bytes
int temperature = (Wire.read() << 8) | Wire.read();
Serial.print("Received Temperature: ");
Serial.println(temperature);
// Turn LED on if temp > 30°C
digitalWrite(LED_PIN, temperature > TEMP_THRESHOLD ? HIGH : LOW);
}
void loop() {
delay(100); // Keep loop alive
}
How the Arduino I2C Code Works
The master code reads the analog sensor value, converts it to voltage, and then calculates the temperature in Celsius. Since I2C transmits data one byte at a time, the 16-bit integer temperature value is split into two bytes: high and low. Both bytes are then transmitted.
The slave code listens for incoming data. When it receives two bytes, it reconstructs the full temperature value. It then checks if this value exceeds 30°C. If it does, the LED is turned on; otherwise, it is turned off. Meanwhile, both boards output data to the Serial Monitor for debugging purposes.
Testing Your I2C Arduino Temperature System
After completing the wiring and uploading the code, test the system using the following steps to ensure it functions correctly.
Step-by-Step Testing Guide
First, upload the master code to one Arduino and the slave code to the other. Ensure you select the correct board and port in the Arduino IDE for each.
Next, open two Serial Monitor windows—one for each Arduino. Set the baud rate to 9600 for both. You should observe temperature values being printed every second on the master side.
Then, observe the slave's Serial Monitor. It should display "Received Temperature: [value]" each time data is received.
Now, warm the sensor by touching it gently. Within a few seconds, the temperature reading should rise above 30°C. At this point, the LED connected to the slave Arduino should illuminate. If it does not, carefully recheck your wiring and code.
💡 Troubleshooting Tip: If no data appears, verify the I2C address (we used 8 in the example). Also, ensure SDA and SCL are not swapped. Finally, confirm that both Arduinos share a common GND connection.
Real-World Uses for This Arduino I2C Project
This simple setup can be the foundation for numerous smart applications. For example, it can be expanded into a home automation system. Imagine placing temperature sensors in different rooms and having a central Arduino collect all the data via I2C to trigger fans or send alerts.
Similarly, you could add more slave devices—such as one for humidity and another for motion detection. Because I2C supports multiple addresses, each slave only responds to its designated data. This keeps your system organized and allows for scalability.
Another idea is to log temperature data to an SD card connected to the master Arduino. Alternatively, you could send alerts to your phone using a Wi-Fi module on the slave side. Mastering I2C basics significantly expands the possibilities.
Common Mistakes to Avoid in Arduino I2C Projects
Even experienced makers can encounter issues. However, most problems can be avoided using these tips.
First, never overlook the importance of a shared GND. Without it, voltage levels differ, leading to communication failure. Second, avoid swapping SDA and SCL pins. They may look similar but have distinct functions. Third, ensure the I2C address used in the master code matches the one specified in the slave code.
Additionally, analog sensors like the LM35 require clean power. Noise from motors or other components on the same board can distort sensor readings. If possible, power them separately. Lastly, remember that I2C has limitations regarding speed and distance—keeping wires under 30 cm generally yields the best results.
Final Thoughts on Mastering Arduino I2C Communication
In summary, this Arduino I2C temperature project teaches essential skills: wiring, inter-board communication, and real-time data handling. Since I2C is widely used in sensors and displays, learning it now prepares you for more advanced builds.
Start with simple projects, test frequently, and expand gradually. Before long, you'll be designing your own smart home systems or industrial monitors. So, get your Arduinos, follow this guide, and unlock the potential of I2C communication today!
Have questions? Share them in the comments. And if this guide was helpful, share it with a fellow maker!
Embedded Electronics — Home
Build an Arduino Calculator with LCD Keypad
Arduino Calculator Project
Smart Parking System with Arduino
5 Simple Arduino Projects for Beginners
Arduino Multiplexer LED Projects
Arduino Traffic Light System
Arduino 7-Segment Display Project
Arduino Interrupts: Button + LED Control
0 Comments