Lesson 5: I2C Communication - Microcontroller Talking to Sensors
đ Learning Objectivesâ
- Understand what I2C protocol is
- Learn about I2C hardware connections
- Learn to enable I2C sensors
- Read temperature sensor data
đ¯ What is I2C?â
I2C (pronounced I-squared-C) = Inter-Integrated Circuit
A Useful Analogyâ
Imagine a bus route with many stops:
- Bus = The shared communication lines
- Devices = The various stops
- Address = Each stop's unique number
All devices share two wires, distinguished by their addresses!
đ I2C Hardware Connectionsâ
Only 2 wires needed + power and ground:
| Wire Name | Function |
|---|---|
| SCL | Clock line |
| SDA | Data line |
| VCC | Power (3.3V or 5V) |
| GND | Ground |
Connection Diagramâ
Microcontroller Temperature Sensor OLED Screen
â â â
ââ SCL âââââââââââŧâ SCL ââââââââââââ¤
ââ SDA âââââââââââŧâ SDA ââââââââââââ¤
ââ VCC âââââââââââŧâ VCC ââââââââââââ¤
ââ GND âââââââââââ´â GND ââââââââââââ
đŽ How I2C Worksâ
1. Each Device Has an Addressâ
| Sensor | I2C Address |
|---|---|
| BMP280 | 0x76 |
| ADS1115 | 0x48 |
| TCS34725 | 0x29 |
| OLED | 0x3C |
2. Communication Processâ
- Microcontroller sends address
- Corresponding device responds
- Send or receive data
- End communication
đ§ I2C Instructions in JoyKitâ
| Instruction | Function |
|---|---|
0xC0 | Enable peripheral |
0xC3 | Enable temperature/humidity sensor (BMP280/BME280) |
0xC7 | Enable WS2812B |
đŦ Classroom Demo Project: Temperature Monitoringâ
Project Goalâ
Read and display temperature sensor data.
Hardware Requirementsâ
- Onboard BMP280/BME280 sensor
- Screen (optional)
Reference Programâ
; Temperature monitoring project
000 C3 0001 0000 ; Enable temperature/humidity sensor
001 02 03E8 0000 ; Wait for sensor to stabilize
002 0E 0000 0000 ; loop (data automatically displayed on screen)
Advanced: Control LED with Temperatureâ
; Change LED color based on temperature
000 C3 0001 0000 ; Enable sensor
001 C7 0001 0000 ; Enable LED
002 02 03E8 0000 ; delay
003 06 000A 001E ; If temperature > 30°C, jump
004 E4 00FF 0000 ; Green (normal)
005 0E 0002 0000 ; jump back
006 E4 0000 00FF ; Red (too hot warning)
007 0E 0002 0000 ; jump back
đĄ Advantages of I2Câ
| Feature | Description |
|---|---|
| Wire efficient | Only needs 2 wires |
| Multiple devices | Can connect multiple sensors at once |
| Simple | Protocol is relatively simple |
đ¯ Comparison with SPIâ
| I2C | SPI |
|---|---|
| 2 wires | 4 wires |
| Has addresses | No addresses, uses chip select |
| Slower | Faster |
| Half-duplex | Full-duplex |
đ Summaryâ
What we learned today:
- â I2C is a bus protocol
- â Requires SCL and SDA lines
- â Each device has an address
- â JoyKit uses 0xC0 series instructions to enable sensors
- â Completed temperature monitoring project
Next lesson, we'll learn about SPI Protocol, then move to comprehensive projects!