I2C is a widely used protocol for communicating with sensors, displays, and other peripherals. CodeCell simplifies I2C communication by automatically configuring the I2C bus at initialization, so you can start using it right away.
The I2C's SDA and SCL pins are located on the lower side of the CodeCell board.
CodeCell automatically configures I2C in the Init()
function, and this configuration cannot be changed if you plan to use the onboard light and motion sensors, as they are connected to the same I2C bus.
The following onboard sensors are already using I2C, so make sure not to use the same addresses when connecting other I2C devices like displays, additional sensors, or modules:
Sensor | Address |
---|---|
VCNL4040 (Light Sensor) | 0x60 |
BNO085 (IMU - Motion Sensor) | 0x4A |
Note: If a new I2C device has the same address as one of these onboard sensors, they will conflict, and you may get incorrect or no data.
To ensure stable I2C communication, CodeCell has 2kΩ pull-up resistors already connected to the SDA and SCL lines. This means you don’t need to add external pull-ups when using external I2C sensors.
To communicate with an external I2C device, use the standard 'Wire.h' library.
Unlike standard Arduino I2C devices, CodeCell has multiple onboard sensors on the same I2C bus. Calling Wire.endTransmission(false);
ensures the bus stays active, allowing the onboard sensors to function properly.
Wire.beginTransmission(SLAVE_ADDRESS); // Start communication with the device
Wire.write(SLAVE_REG); // Send the register we want to read
Wire.endTransmission(false); // Don't release the bus (needed for onboard sensors)
Wire.requestFrom(SLAVE_ADDRESS, 1); // Request 1 byte from the slave
if (Wire.available()) {
uint8_t data = Wire.read(); // Read received byte
Serial.println(data);
}
The example below can be used to test external I²C devices connected to CodeCell’s pins. It will display the detected I²C slave addresses, including the onboard sensor.
#include <CodeCell.h>
CodeCell myCodeCell;
void setup() {
Serial.begin(115200); // Set Serial baud rate to 115200 - Ensure Tools/USB_CDC_On_Boot is enabled if using Serial
myCodeCell.Init(LIGHT); // Initializes Light Sensor
// I2C Scanner
Serial.println("Scanning I2C devices...");
Wire.begin();
byte error, address;
int nDevices = 0;
for (address = 1; address < 127; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
} else if (error == 4) {
Serial.print("Unknown error at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found.\n");
else
Serial.println("Scan complete.\n");
}
void loop() {
if (myCodeCell.Run(10)) { // Run every 10Hz
}
}
Next up, we hook up an APDS9960 color sensor to the I²C pins and display the readings on the Serial monitor.
#include <CodeCell.h>
#include <Adafruit_APDS9960.h<
CodeCell myCodeCell;
Adafruit_APDS9960 apds;
uint16_t r, g, b, c;
void setup() {
Serial.begin(115200); // Set Serial baud rate to 115200 - Ensure Tools/USB_CDC_On_Boot is enabled if using Serial
myCodeCell.Init(LIGHT); // Initializes Light Sensor
if (!apds.begin()) {
Serial.println("failed to initialize device! Please check your wiring.");
} else Serial.println("Device initialized!");
//enable color sensign mode
apds.enableColor(true);
}
void loop() {
if (myCodeCell.Run(10)) { //Run every 10Hz
myCodeCell.PrintSensors(); //Print sensors data
if (apds.colorDataReady()) {
apds.getColorData(&r, &g, &b, &c);
Serial.print(">> Color Sensors: R");
Serial.print(r);
Serial.print(",G");
Serial.print(g);
Serial.print(",B");
Serial.println(b);
}
}
}
CodeCell automatically configures the I2C bus, so no need to manually set it up. It also automatically configures the onboard sensors, while also supporting additional I2C devices!
Join our Community ~ Be the first to know about new products and get exciting deals!
© 2025 Microbots.