powered by TinkerSpace.in
Join free
TinkerPy Tutorial ๐Ÿ“˜ Part 1 ยท Starter ยท Chapter 11 of 24

I2C and All I2C Modules

What if one pair of wires could talk to a screen, a clock, a motion sensor and a weather sensor โ€” all at the same time? Thatโ€™s I2C! Every module waits for its own address, like houses on the same street waiting for the postman.

๐Ÿ“–Key words

I2C

Say โ€œI-squared-Cโ€ or โ€œI-two-Cโ€. A way for many modules to share just two wires with the board.

SDA

Serial Data โ€” the wire that carries the messages. On a TinkerPy board it is always o7.

SCL

Serial Clock โ€” the wire that keeps the timing, like a drum beat. It is always o8.

Address

Each I2C moduleโ€™s own number, like a house number: the OLED is 0x3C, the BMP280 is 0x76. The 0x means the number is written in hexadecimal.

Address clash

Two modules with the same address on the same wires. The board canโ€™t tell them apart, so Circuit Connections wonโ€™t let you switch both on.

๐ŸŽฌSee it work

Two wires, many modules

Watch the board call each address in turn on SDA and SCL. Tap a step to read what happens.

Can they share the wires?

Check the addresses: can these I2C modules be switched on together, or do they clash?

๐Ÿง How it works

What I2C is: two wires, many modules

  • Most modules need a pin each. I2C modules all share the same two signal wires (plus + and GND for power).
  • The board calls an address; only the module with that address answers. The others stay quiet.
  • So you can add a screen, a clock and a sensor and still use only two pins.
  • I2C works the same way on the TinkerPy Mini and the TinkerPy Mega.

SDA and SCL on o7 and o8

  • SDA (data) is always o7 and SCL (clock) is always o8 โ€” they canโ€™t be moved.
  • Switch an I2C part on in Circuit Connections โ†’ Special components: it adds the import line and o7 = set_function(SDA), o8 = set_function(SCL), marked โ€œcannot be changedโ€.
  • The OLED is switched on in every new project โ€” thatโ€™s why o7 and o8 are already taken. Switch it off if you need o7 and o8 for something else.
  • Switch on as many I2C parts as you need; o7 and o8 are free again only when all of them are off.
  • Robot motors use o3โ€“o6 on purpose, so a robot can still have a display on o7 and o8.

Addresses and address clashes

  • Addresses are written in hexadecimal: 0x3C, 0x68โ€ฆ Just think of them as house numbers.
  • Two modules with the same address canโ€™t share the wires: MPU6050 + RTC are both 0x68, VL53L0X + TCS34725 are both 0x29.
  • Circuit Connections spots this and says โ€œI2C address clashโ€ โ€” switch one off first.
  • A few OLEDs use 0x3D (oled_begin(0x3D)); many LCDs use 0x27, some 0x3F โ€” lcd_begin() with nothing inside finds it by itself.

All the I2C modules

  • Displays: OLED ๐Ÿ–ฅ๏ธ (4 lines of text, animations) and LCD 16ร—2 ๐Ÿ“Ÿ (2 lines of 16 letters).
  • Motion: MPU6050 ๐Ÿงญ โ€” tilt, shake, fall and steps. Time: RTC ๐Ÿ•ฐ๏ธ (DS3231 or DS1307) โ€” keeps time with its own small battery, even when the board is off.
  • Weather: BMP280 ๐ŸŒค๏ธ โ€” air pressure, altitude and temperature. Distance: VL53L0X ๐Ÿ“ โ€” a laser, 3 cm to about 2 m.
  • Colour: TCS34725 ๐ŸŽจ โ€” colour name and R G B. Gesture: APDS-9960 ๐Ÿ‘‹ โ€” swipes, near/far and colour. Pulse oximeter โค๏ธ (MAX30100/MAX30102) โ€” heart rate and SpO2, for learning only.
  • RTC, BMP280, VL53L0X, TCS34725, APDS-9960 and the pulse oximeter need board firmware 2.27 or newer. Coding them: TinkerPy Coder chapters 9, 10 and 11.

๐Ÿ’ปCode

Text
I2C part                         Address   Firmware   What it gives
-------------------------------  --------  ---------  ---------------------------------
OLED display                     0x3C      -          4 lines of text, animations
LCD 16x2                         0x27      -          2 lines x 16 letters (some 0x3F)
MPU6050 tilt & motion            0x68      -          tilt, shake, fall, steps, turn
RTC clock (DS3231 / DS1307)      0x68      2.27+      time and date, battery backup
Pulse oximeter (MAX30100/30102)  0x57      2.27+      heart rate, SpO2 (learning only)
BMP280 pressure                  0x76      2.27+      pressure, altitude, temperature
VL53L0X laser distance           0x29      2.27+      3 cm to about 2 m
TCS34725 colour                  0x29      2.27+      colour name, R G B
APDS-9960 gesture                0x39      2.27+      swipes, near / far, colour

All use o7 = SDA and o8 = SCL.   Clashes: 0x68 (MPU6050 + RTC), 0x29 (VL53L0X + TCS34725).

Every I2C part in Circuit Connections โ†’ Special components, with its address, the board firmware it needs and what it gives. The addresses tell you at a glance which two canโ€™t be on together.

# Add the libraries you need
import oled_display
import bmp280

# ===== Pressure station =====

# This code block executes only once
setup:
    oled_begin()  # starts the OLED
    o7 = set_function(SDA)  # cannot be changed
    o8 = set_function(SCL)  # cannot be changed
    oled_write(1, left, small, regular, "Air pressure")  # write "Air pressure" on OLED line 1 (small, from the left)

# This code block executes forever
loop:
    p = pressure()  # air pressure in hPa
    oled_write(2, left, medium, bold, p)  # write the value of p on OLED line 2 (medium bold, from the left)
    wait(1000)  # pause here for 1000 milliseconds before continuing

A pressure station: the OLED (already on in a new project) and a BMP280 share o7 and o8. Switching on BMP280 pressure added import bmp280; the o7 / o8 lines were already there. Needs board firmware 2.27 or newer.

# Add the libraries you need
import oled_display
import rtc

# ===== Real clock =====

# This code block executes only once
setup:
    oled_begin()  # starts the OLED
    o7 = set_function(SDA)  # cannot be changed
    o8 = set_function(SCL)  # cannot be changed

# This code block executes forever
loop:
    h = rtc_hour()  # 0 to 23
    m = rtc_minute()  # 0 to 59
    oled_write(1, left, small, regular, "Hour and minute")  # write "Hour and minute" on OLED line 1 (small, from the left)
    oled_write(2, left, small, regular, h)  # write the value of h on OLED line 2 (small, from the left)
    oled_write(3, left, small, regular, m)  # write the value of m on OLED line 3 (small, from the left)
    wait(1000)  # pause here for 1000 milliseconds before continuing

A real clock: the RTC module keeps the time even when the board is switched off, and the OLED shows the hour and minute. Two I2C parts, still only two pins. Needs board firmware 2.27 or newer.

# Add the libraries you need
import mpu6050
import buzzer

# Give your pins easy-to-remember names
o1 = set_name("alarm")

# ===== Shake alarm =====

# This code block executes only once
setup:
    o7 = set_function(SDA)  # cannot be changed
    o8 = set_function(SCL)  # cannot be changed
    alarm = set_output(buzzer)

# This code block executes forever
loop:
    if shake_detected() == 1:  # when shake_detected() == 1
        alarm = ON  # turn alarm ON
        wait(1000)  # pause here for 1000 milliseconds before continuing
        alarm = OFF  # turn alarm OFF

A shake alarm with the MPU6050 motion sensor: shake it and the buzzer sounds for a second. The OLED was switched off here, so only the MPU6050 is on o7 and o8.

Open TinkerPy Code

๐ŸŽ‰Fun examples

๐Ÿ“ฑPhone in your pocket

Your phone knows when you turn it sideways because a tiny motion sensor inside measures tilt โ€” the same idea as the MPU6050.

โŒšGlowing watch

Fitness watches shine a small light into your skin and watch how it changes with every heartbeat โ€” just like a pulse oximeter module.

๐ŸŒง๏ธRain coming?

When the air pressure falls quickly, rain is often on the way. Weather stations use pressure sensors like the BMP280 to help forecast.

๐ŸŒWhere itโ€™s used

๐Ÿ› ๏ธProject ideas

I2C module flash cards

Make one card for each I2C module: picture, name, address and what it measures. Play โ€œclash or notโ€: draw two cards and decide if they can be on together.

๐Ÿงฐ You need: 9 card pieces, pens
Weather station plan

Plan a station with an OLED, a BMP280 and an RTC: list each partโ€™s address, check for clashes and write what each OLED line will show. Then switch them on in Circuit Connections and see which pins they take.

๐Ÿงฐ You need: notebook, TinkerPy board or Simulator

โญPoints to remember

  1. I2C lets many modules share two wires.
  2. SDA = data = o7; SCL = clock = o8.
  3. o7 and o8 canโ€™t be changed for I2C.
  4. Each I2C module has its own address.
  5. Addresses are hex numbers like 0x3C.
  6. Same address = address clash: not both at once.
  7. MPU6050 and RTC are both 0x68.
  8. VL53L0X and TCS34725 are both 0x29.
  9. The OLED is on in every new project.
  10. Switch I2C parts on in Special components.
  11. o7 and o8 are free only when all I2C parts are off.
  12. RTC, BMP280 and the newer parts need firmware 2.27+.
  13. I2C works on both the Mini and the Mega.
๐Ÿคฏ
Did you know?

I2C was invented by the company Philips in the early 1980s so that the chips inside a television could talk to each other over just two wires โ€” today it is in phones, cars and your TinkerPy board.

โ“Mini quiz

๐Ÿ”‘
Log in to try the mini quiz

5 quick questions to check yourself โ€” free with your TinkerPy account.

Log in