Main Menu

search

You are here

i2c: minimum code

[last updated: 2020-05-16]
i2c home page
--------------------------------



          On This Page:
    • Alternate modes of operation:
      • simplest: master sending single byte to single slave:
      • Master sending a multiple-byte packet to Slave:
      • master requests data from slave
      • example from deepblue?
    • ...
    • ...


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

  • simplest: master sending single byte to single slave:
    • Master code:
      • #include <Wire.h>
        int x = 65; // some nominal number to send to slave
        // -or- char x = 65;
        // -or- char x = 'A';
      • in setup:
        Wire.begin(); // start the i2c bus as master
      • in loop:
        Wire.beginTransmission(9); // open bus to transmit to slave device at address 9
        Wire.write(x); // add var x to queue
        Wire.endTransmission(); // send queue to slave, then stop transmitting and release bus


        for troubleshooting:
        Serial.print("data sent: "); Serial.println(x);
        if (x < 75) {
            Serial.println("incrementing x");
            x = x + 1;
        } else {
            x = 65;
        }
        -----

          With x initially defined as: int x = 65;
          data sent will print out "65", "66", etc with each loop.
          -----
          With x initially defined as: char x = 65;
              or as: char x = 'A';
          it will print: "A", "B", etc.

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

    • Single-byte Slave code:
      • #include <Wire.h>
        int x; // to contain value of data received from master
        // -or- char x;
        // -or- byte x;
      • setup:
        Wire.begin(9); // start the i2c bus as slave at address 9
        Wire.onReceive(receiveEvent); // attach a function (named "receiveEvent") that will execute when something is received.
      • loop:
        nothing needed
      • function defs:
        void receiveEvent(int numBytes) {
            // takes a single int parameter (the number of bytes to be read from the master) and returns nothing.
            x = Wire.read(); // read one character from the bus
        }


        for troubleshooting:
        Serial.print("rcvd: "); Serial.println(x);
        -----

          if x is initially defined as: int x = 65;
              print rcvd will print "65", "66" etc as master loop increments the value
              it will print the same regardless whether the master thinks its sending a char or an int
          -----
          if x is initially defined as: char x = 'A';
              then it will print "A", "B" etc as master loop increments the value
              regardless whether the master thinks its sending a char or an int
          -----

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

  • Master sending a multiple-byte packet to Slave:

          go here for code that did NOT work

    • Master code:
      • #include <Wire.h>
        char msgToSend[] = {"print this"};
        int numChars = sizeof(msgToSend)-1; // gives # of chars in the msg - well ... no, it doesn't ...
      • in setup:
        Wire.begin(); // start the i2c bus as master
      • in loop:
        Wire.beginTransmission(9); // open bus to transmit to slave device at address 9
        for (int n = 0; n < numChars; n++) {
            Wire.write(msgToSend[n]); // put next element of msgToSend into local buffer/queue
        }
        Wire.endTransmission(); // send queue to slave, then stop transmitting and release bus

    • Slave code:
      • #include <Wire.h>
        char x = '3'; // dummy initial value for received char
      • setup:
        Wire.begin(9); // start the i2c bus as slave at address 9
        Wire.onReceive(receiveEvent); // attach a function (named "receiveEvent") that will execute when something is received.
      • loop:
        nothing needed
      • function defs:
        void receiveEvent(int numBytes) {
            while (1 < Wire.available()) { // loop through all but the last
                char c = Wire.read(); // receive byte as a character
                Serial.print(c); // print the character
            }
        }

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

  • Master requests data from Slave:
    • #include <Wire.h>

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

.

.

eof