Main Menu

search

You are here

Wire.h

[last updated: 2017-07-06]
Design/Concept: This page will list Wire.h function calls needed for various i2C implementations. Intention is to be useful to newbies like myself who need a basic-level tutorial with step-by-step procedures to follow.
-----

  • The Wire.h library is built in to Arduino IDE (as of not sure which version)
  • For all Arduino implementations using Wire.h,
    put this line into your sketch in Includes/Globals section:
         #include <Wire.h> (you may want to use quotes instead of brackets)

  • Note: i2C nominally communicates at 100kHz (on Uno at least...)
    but can be changed with Wire.setClock([speed]) command (only certain speeds are allowed...)

  • Here's what you need for the Simple (simplest) i2C:
    • two Arduinos (one master, one slave),
      enables one-direction communication (from master to slave).
    • in your Master sketch:
      • in SETUP:
        // Start the I2C Bus as Master
        Wire.begin();
      • in LOOP:
        Wire.beginTransmission(9); // transmit to device #9 (eg.)
        Wire.write(x); // sends x (be sure to declare x as int or char or whatever)
        (Wire.send(x) is used in older versions)
        Wire.endTransmission(); // stop transmitting
    • in your Slave sketch:
      • in SETUP:
        // Start the I2C Bus as Slave at address 9
        Wire.begin(9);
        Wire.onReceive([function name]); // define a function to execute when slave receives something from master
      • define function to execute when data is received:
        (this function will run as an interrupt whenever data is received)
          void [function name](int bytes) {
          x = Wire.read(); // read one character from the I2C
          } // ----------------- END [function name] ----------------------
      • in LOOP:
        [put code to do whatever you want in response to x,
        which is the variable that contains the single byte
        that was received from the master and handled by your receive-data function]

    ----------------------------------------------------------------------------------------

  • Here's what you need for two-way communication between master & slave
    • master is still in control:
      slave only sends data to master when master requests it.
    • in your Master sketch:
      • in SETUP:
        // Start the I2C Bus as Master
        Wire.begin();
      • in LOOP:
        • if you want to send data to slave, include code as in single-char version
          Wire.beginTransmission(9); // transmit to device #9
          Wire.write(x); // sends x (be sure to declare x as int or char or whatever
          (Wire.send(x) is used in older versions)
          Wire.endTransmission(); // stop transmitting
        • LOOP code to send request for data to slave:
          Wire.beginTransmission(9);
          Wire.requestFrom(9, 1); // 9 is slave addr, 1 is # bytes requested
          [after the requestFrom, you must immediately Wire.read to get data that the slave will send]
          char charRcvd = Wire.read(); // repeat this for # bytes expected
          Wire.endTransmission();

    • in your Slave sketch:
      • in SETUP:
        ...
      • in LOOP:
        ...

.

.

.

eof