This reference is for Processing 3.0+. If you have a previous version, use the reference included with your software in the Help menu. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Core Javadoc and Libraries Javadoc.

Class

I2C

Name

read()

Examples
import processing.io.*;
I2C compass;

void setup() {
  //printArray(I2C.list());
  compass = new I2C(I2C.list()[0]);
}

void draw() {
  // read the heading over I2C from a compass module
  // with address 33 (hex 0x21)
  compass.beginTransmission(0x21);
  // first send a command byte
  compass.write(0x41);
  // read in two bytes
  byte[] in = compass.read(2);

  // put bytes together to tenth of degrees
  // & 0xff makes sure the byte is not interpreted as a negative value
  int deg = (in[0] & 0xff) << 8 | (in[1] & 0xff);
  println((deg / 10.0));
}

Description Read bytes from the attached device

You must call beginTransmission() before calling this function. This function also ends the current transmission and sends any data that was queued using write() before. It is not necessary to call endTransmission() after read().
Syntax
.read(len)
Parameters
len int: number of bytes to read
Returnsbyte[]
Updated on January 1, 2021 03:38:10am EST