I2C API stalls when no device is present

01 May 2012

I'm planning to use the mbed to do some sampling from a colour sensor. I don't have the sensor yet but I thought I'd get started on the code and try out the embed system. I don't have anything connected to the mbed at moment, except for the USB cable.

Starting with the I2C Example I tried to drive the I2C bus, but the API calls seem to stall. I put a couple of print statements in to see things working and the I2C read api call doesn't return.

Here is my code:

#include "mbed.h"

I2C i2c(p28, p27);
Serial pc(USBTX, USBRX); // tx, rx

int main()
{
  int address = 0x62;
  char data[2];
    
  pc.printf("1");
  i2c.read(address, data, 2);
  pc.printf("2");
    
}

I get the "1" appearing on the serial interface, but never the "2". I've also tried it with a write, even just a start - stop sequence. It always seems to stall at the stop.

I realise I don't have the bus properly terminated. But I thought I might at least get an error code returned instead.

Why is the I2C API not returning? Does the I2C bus need terminations for the API to work? Or does it need the actual device to respond too?

Andrew.

02 May 2012

You dont need the device, but you do need to install the pull-up resistors on SDA and SCL or the API calls will stall.

03 May 2012

Thanks Wim,

I got hold of a prototyping board and connected some pull-ups. That solved my issue, like you said.