- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Mastering I2C Communication Between Two Arduinos for Temperature Data Transmission
Introduction
The Arduino ecosystem offers incredible flexibility for creating smart, interconnected devices. In this project, we demonstrate how to use two Arduino boards to communicate temperature data via the I2C protocol. This system is ideal for applications like temperature monitoring or automation where real-time communication between devices is essential.
System Overview
schematic:
connections of master:
connections of slave:
1. Master Arduino
- Function: Reads temperature data from a sensor and sends it to the Slave Arduino.
- Components:
- Analog temperature sensor (connected to analog pin A1).
- Code to convert raw sensor data to Celsius.
- I2C communication setup to transmit the temperature in two-byte format.
2. Slave Arduino
- Function: Receives the temperature data, displays it via the serial monitor, and controls an LED based on the threshold.
- Components:
- Serial debugging for temperature visualization.
- LED connected to digital pin 13 for alert indication.
- Logic to act based on the temperature threshold.
Understanding I2C Communication
What is I2C?
I2C (Inter-Integrated Circuit) is a synchronous, multi-master, multi-slave communication protocol that allows multiple devices to share data using just two wires: SDA (Serial Data Line) and SCL (Serial Clock Line).
How It Works in This Project
- Master Role: The Master Arduino initiates communication by sending temperature data.
- Slave Role: The Slave Arduino listens for incoming data and performs actions like serial logging and LED control.
Code Explanation
Master Arduino Code
The Master Arduino reads temperature data from the sensor and sends it to the Slave via I2C.
#include <Wire.h>
#define sensorPin A1
void setup() {
Wire.begin();
}
void loop() {
float reading = analogRead(A1); // Read the analog sensor value
float mv = (reading)*(5/ 1024.0) * 100;
int voltage = mv; // Convert to voltage
int temperatureC = voltage; // Convert voltage to Celsius
Wire.beginTransmission(8); // Transmit to device #8
Wire.write(highByte((int)temperatureC));
delay(100);
Wire.write(lowByte((int)temperatureC));
delay(200); // Send high byte
Wire.endTransmission(); // Stop transmitting
delay(1000); // Send data every second
}
Key Features:
- Reads temperature using an analog temperature sensor.
- Converts sensor output to Celsius.
- Sends the temperature as two bytes using I2C.
Slave Arduino Code
The Slave Arduino receives and processes temperature data, controlling an LED if the temperature exceeds the threshold.
#include <Wire.h>
int LED = 13; // Pin for LED
const int THRESHOLD = 30; // Temperature threshold
void setup() {
pinMode(LED, OUTPUT);
Wire.begin(8); // Join I2C bus with address #8
Wire.onReceive(receiveEvent); // Register event for receiving data
Serial.begin(9600); // Initialize serial for debugging
}
void loop() {
delay(100); // Nothing to do in the main loop
Serial.begin(9600);
}
void receiveEvent(int howMany) {
if (Wire.available()) { // Check if data is available
int x1 = Wire.read(); //x1 hilds upper byte of received a
delay(200);
int x2 = Wire.read(); //x2 holds lower byte of received a
int y1 = (int)x1 << 8 | (int)x2;
Serial.print("Received Temperature: ");
Serial.println(y1);
// Turn on LED if temperature exceeds threshold
if (y1 > THRESHOLD) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}
}
Key Features:
- Receives temperature data and combines bytes into a full integer.
- Logs the temperature to the serial monitor.
- Activates LED based on a temperature threshold.
Applications
1. Home Automation
- Temperature monitoring for HVAC systems.
- Alerts for abnormal temperature ranges.
2. Industrial Monitoring
- Data collection for remote temperature sensors in factories.
- Automated responses to temperature thresholds.
3. Educational Projects
- Learning about I2C communication.
- Combining sensor data processing with real-world actions.
Frequently Asked Questions (FAQs)
1. What is I2C and how does it work with Arduino?
I2C is a communication protocol that enables data transfer between devices using just two wires. It’s perfect for connecting multiple sensors or devices to Arduino.
2. Why use two bytes for temperature data?
Using two bytes allows for more precise and larger data values, ensuring accurate temperature representation.
3. Can I use this setup for other sensors?
Yes, you can modify the code to integrate other analog or digital sensors.
4. What is the range of the I2C protocol?
I2C typically works over short distances, up to about 1 meter, but this can vary depending on the hardware.
5. How can I expand this system?
You can add more sensors or devices by assigning them unique I2C addresses.
6. What is the role of the LED in this project?
The LED acts as a visual indicator when the temperature exceeds a predefined threshold.
- Get link
- X
- Other Apps
Comments
Post a Comment