Skip to main content

Lesson 5: I2C Communication - Microcontroller Talking to Sensors

📌 Learning Objectives​

  1. Understand what I2C protocol is
  2. Learn about I2C hardware connections
  3. Learn to enable I2C sensors
  4. 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 NameFunction
SCLClock line
SDAData line
VCCPower (3.3V or 5V)
GNDGround

Connection Diagram​

Microcontroller   Temperature Sensor   OLED Screen
│ │ │
├─ SCL ──────────â”ŧ─ SCL ───────────┤
├─ SDA ──────────â”ŧ─ SDA ───────────┤
├─ VCC ──────────â”ŧ─ VCC ───────────┤
└─ GND ──────────┴─ GND ───────────┘

📮 How I2C Works​

1. Each Device Has an Address​

SensorI2C Address
BMP2800x76
ADS11150x48
TCS347250x29
OLED0x3C

2. Communication Process​

  1. Microcontroller sends address
  2. Corresponding device responds
  3. Send or receive data
  4. End communication

🔧 I2C Instructions in JoyKit​

InstructionFunction
0xC0Enable peripheral
0xC3Enable temperature/humidity sensor (BMP280/BME280)
0xC7Enable 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​

FeatureDescription
Wire efficientOnly needs 2 wires
Multiple devicesCan connect multiple sensors at once
SimpleProtocol is relatively simple

đŸŽ¯ Comparison with SPI​

I2CSPI
2 wires4 wires
Has addressesNo addresses, uses chip select
SlowerFaster
Half-duplexFull-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!