The objective of this project is to communicate with an I2C-based temperature sensor using Arduino and display the sensor readings on an LCD screen. This will demonstrate how to use I2C communication to gather temperature data and show it on an LCD in real time.
Fundamental Programming Concepts
- I2C Communication: Learn how to communicate with sensors and devices using the I2C protocol.
- Digital Input/Output: Handle data flow between the Arduino and the I2C temperature sensor.
- Data Types: Use appropriate data types to handle temperature readings (e.g., float for decimal precision).
- LCD Display: Display the temperature readings in a clear and readable format on an LCD.
Requirement Components
To complete this I2C temperature sensor display using Arduino project, you will need:
- Arduino Uno Board
- I2C Temperature Sensor (such as LM75 or DS18B20 with I2C module)
- I2C LCD (16×2 or 20×4)
- Breadboard
- Jumper Wires
- USB Cable (for connecting Arduino to your computer)
Circuit Diagram
Circuit Connection
Component | Arduino Pin |
I2C Temperature Sensor (SCL) | SCL (A5 on Arduino Uno) |
I2C Temperature Sensor (SDA) | SDA (A4 on Arduino Uno) |
I2C LCD (SCL) | SCL (A5) |
I2C LCD (SDA) | SDA (A4) |
Power (Both LCD and Sensor) | 5V and GND |
How to Connect the Circuit
- I2C Temperature Sensor: Connect the SCL pin to A5 and SDA to A4 on the Arduino. Ensure the sensor is powered by connecting 5V and GND.
- I2C LCD: Connect the SCL pin of the LCD to A5 and the SDA pin to A4 (shared with the sensor), and power it via 5V and GND.
- Power: Ensure that both the LCD and the sensor receive power from the Arduino.
Explanation of Circuit
- I2C Communication allows multiple devices (like the temperature sensor and LCD) to communicate with the Arduino using only two pins (SDA and SCL).
- The I2C temperature sensor sends temperature data over the I2C bus, which is read by the Arduino. The Arduino then sends this data to the LCD for display.
Programming Section for I2C-based temperature sensor
Arduino Syntax
Topic Name | Syntax | Explanation |
Wire.begin() | Wire.begin() | Initializes the I2C communication protocol. |
Wire.requestFrom() | Wire.requestFrom(address, quantity) | Requests data from the I2C sensor at a specific address. |
lcd.print() | lcd.print(data) | Displays the data on the LCD screen. |
Arduino Code:
Here’s the Arduino code to read data from the I2C temperature sensor and display it on an LCD:
#include <Wire.h> // Include I2C library
#include <LiquidCrystal_I2C.h> // Include LCD library
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set LCD I2C address and size (16x2)
const int sensorAddress = 0x48; // I2C address of temperature sensor (example: LM75)
void setup() {
// Initialize I2C communication
Wire.begin();
// Initialize the LCD
lcd.begin(16, 2);
lcd.backlight(); // Turn on the backlight
lcd.print("Temp:"); // Initial display
}
void loop() {
// Request 2 bytes of data from the sensor
Wire.requestFrom(sensorAddress, 2);
if (Wire.available() == 2) {
// Read the two bytes from the sensor
byte msb = Wire.read();
byte lsb = Wire.read();
// Combine the two bytes to create the temperature value
int temperature = (msb << 8) | lsb;
temperature = temperature >> 5; // Adjust for LM75 11-bit output
// Convert to Celsius (example calculation)
float tempC = temperature * 0.125;
// Display temperature on the LCD
lcd.setCursor(6, 0); // Move cursor to position 6 on the first line
lcd.print(tempC);
lcd.print(" C");
delay(1000); // Wait 1 second before next reading
}
}
Steps to Upload Code:
- Connect your Arduino to your computer using a USB cable.
- Open the Arduino IDE and select the correct Board and Port.
- Copy and paste the provided code into a new sketch.
- Click the Upload button to transfer the code to your Arduino.
- Observe the temperature displayed on the LCD in real-time.
Check Output
After uploading the code, the LCD will display the temperature readings in Celsius. These values are updated every second. The temperature sensor communicates via I2C, and the Arduino sends the readings to the LCD.
Troubleshooting Tips
- No temperature display? Ensure that the I2C address for both the sensor and the LCD is correct. You may need to scan for I2C addresses using an I2C scanner code.
- LCD not displaying properly? Verify that the correct I2C address is set for the LCD. You can adjust the LCD backlight using lcd.backlight() and check connections.
- Incorrect temperature readings? Make sure the temperature sensor is properly connected and functioning. Verify the temperature conversion in the code based on the sensor’s data sheet.
Further Exploration
- Display in Fahrenheit: Add a conversion to display the temperature in Fahrenheit alongside the Celsius reading.
- Multiple Sensors: Extend the project by adding more I2C sensors (like humidity sensors) to display additional environmental data.
- OLED Display: Swap the LCD for an OLED display and modify the code to output on the new display type.
Note
This project demonstrates how to communicate with an I2C-based temperature sensor and display real-time temperature data on an LCD display using Arduino. It covers basic I2C communication, data handling, and LCD output, making it ideal for beginners interested in sensor projects.
FAQ
Q1: What is I2C communication, and why use it?
I2C is a communication protocol that allows multiple devices (sensors, displays) to communicate with the Arduino using only two pins, making it efficient for connecting multiple peripherals.
Q2: How does Wire.requestFrom() work in this project?
Wire.requestFrom() sends a request to the I2C sensor at the specified address and reads a specific amount of data (e.g., 2 bytes for temperature readings).
Q3: Can I use other sensors with this code?
Yes, you can modify the code to work with other I2C temperature sensors by adjusting the sensor’s I2C address and data handling based on the sensor’s specifications.
Q4: How do I find the I2C address of my sensor or display?
You can use an I2C address scanner code to detect the addresses of all connected I2C devices.
Q5: Can I use a 20×4 LCD instead of 16×2?
Yes, the code can easily be modified for larger LCDs by adjusting the size in lcd.begin() and adjusting the cursor positions for displaying text.