9 years, 2 months ago.

I2C Not Working

I am trying to get I2C working on my MBed but nothing happens. I have 10k pullups on pin 27 and 28. When I run this code the Mbed does not appear to do anything - the lines remain pulled high to Mbed's Vout 3V3 source.

What am I doing wrong?

One bizarre thing that I did notice is that the 4 LEDs on my Mbed are lightly glowing (only noticeable under complete darkness). This is a brand new unit so I am not sure what is wrong...

I2C_Test

#include "mbed.h"
 
I2C i2c(p28 , p27); 

int main() {
    while (1) {
       i2c.write(0x00);
       wait(0.5);
    }
}

Update:

I am now running this code but my output is still bizarre. All I get is a 15Hz clock. I have now tried 1k, 2.2k, 4.7k and 10k pull-ups and the response is the same every time. Any advice?

I2C_Test2

#include "mbed.h"
 
I2C i2c(p28 , p27); 

int main() {
    while (1) {
    char reset[2];    
    reset[0]=0x0C;   // device register I'm writing to
    reset[1]=0x01;   // byte value I'm sending
    i2c.write(0xD0,reset,2);   // 0xD0 is the device i2c address, sending one byte to register 0x0C here to reset the device.

    }
}

/media/uploads/Gonzik007/output_68ihd0v.png

Update 2:

I have switched to pin 9 and 10 but my problem persists. There is no slave connected, only 2.2k pullups on pin 9 and pin 10. All I get are these 15Hz square wave forms. This is my first time working with the Mbed and it's pretty frustrating. Are these issues common to this platform?

I2C_Test3

#include "mbed.h"
/* .....................................................
 
                            __||||__
[Gnd]                       | |||| |            [ 3V3 OUT ]
[VIn][4V5->9V0]             |      |            [5 Volt OUT]
[Vbat]                      |      |            [IF-]
[!Reset]                    |      |            [IF+]
[ p5 ][mosi]        [.....] |      |            [Eth RD+]
[ p6 ][miso]        [.....] |      |            [Eth RD-]
[ p7 ][sclk]        [.....] |      |            [Eth TD-]
[ p8 ]              [.....] |      |            [Eth TD+]
[ p9 ][TX][SDA]     [.....] |  /\  |            [USB D +]
[ p10][RX][scl]     [.....] |  \/  |            [USB D -]
[ p11]    [mosi]    [.....] |      | [.....]    [CAN  RD][p30]
[ p12]    [miso]    [V_CS ] |      | [.....]    [CAN  TD][p29]
[ p13][TX][sclk]    [V CLR] | .... | [Red  ]    [sda][TX][p28]
[ p14][RX]          [V Clk] | .... | [Red  ]    [sck][RX][p27]
[ p15][AnnIn]       [H Snk] | .... | [Red  ]    [PWM Out][p26]
[ p16][AnnIn]       [V Snk] | .... | [Red  ]    [PWM Out][p25]
[ p17][AnnIn]       [Blue ] |      | [Green]    [PWM Out][p24]
[ p18][AnnIO]       [Blue ] |      | [Green]    [PWM Out][p23]
[ p19][AnnIn]       [Blue ] | **** | [Green]    [PWM Out][p22]
[ p20][AnnIn]       [Blue ] |______| [Green]    [PWM Out][p21]
 
 ....................................................  */
 
 

I2C i2c(p9 , p10); 

int main() {
    while (1) {
    char reset[2];    
    reset[0]=0x0C;   // device register I'm writing to
    reset[1]=0x01;   // byte value I'm sending
    i2c.write(0xD0,reset,2);   // 0xD0 is the device i2c address, sending one byte to register 0x0C here to reset the device.

    }
}

/media/uploads/Gonzik007/bizzare15hz.png

Does not look like I2C at all.. Are you sure the code is actually running on the board. Has it compiled and downloaded without errors? Have you tried any other code that just toggles the p27, p28 pins? Is slave connected correctly and powered or could it be doing something to the pins.

posted by Wim Huiskamp 03 Feb 2015

What device are you communicating with? and try pins 9 and 10 (the other i2c port). I have used this code on these pins before.

posted by Paul Staron 03 Feb 2015

The LPC1768 you have is probably the most bug free of all mbed platforms. I also have never seen anything like this (either from myself or on the forums here) on either the LPC1768 or any other platform.

Not that I can imagine what it matters, but is your mbed library up to date? (Click on it, see if the update button is available). And what if you add a short delay in your loop and toggle one of the LEDs? (Just to see if it is properly running its loop).

posted by Erik - 04 Feb 2015

Hi @Erik Olieman. Thanks for the response. I can run the hello world code fine. I tried just toggling the pins myself and ran into issues. I started a new thread for that here - http://developer.mbed.org/questions/6157/MBed-Pin-Functionality-Bizarre/

posted by Fiipp Demenschonok 04 Feb 2015

3 Answers

9 years, 2 months ago.

In general I would advice you to use the full functions for I2C, so for example i2c.write(address, data, 2); These are easier to use and better supported.

Now what you are doing might be rejected by the NXP I2C hardware: They have an internal state machine and might refuse to send data when no start condition has been sent. So first try adding a start condition and then the write.

Finally your LEDs glowing lightly is intended behavior :). By default pins on the MCU have a weak pull up enabled, this causes a very small amount of current to go through the LEDs and they will light up very faintly. How much they light up can differ because this pull up current is not well defined.

Thanks for response. I added an update...issue still persists.

posted by Fiipp Demenschonok 03 Feb 2015
9 years, 2 months ago.

Here is a snip along the lines of what Erik is saying. I use this method on all platforms that works for me (up to now).

You may also change the pull up's to a lower value, I have used 10k but generally I stick to 4k7.

i2c snip

    reset[0]=0x0C;   // device register I'm writing to
    reset[1]=0x00;   // byte value I'm sending

    i2c.write(0xD0,reset,2);   // 0xD0 is the device i2c address, sending one byte to register 0x0C here to reset the device.


Here is an i2c test that will enable you to test any standard i2c device.

i2c test

// I2C - Search for devices on an I2C bus,
//  - Continuously cycles through each device address, checking for a response
//  - Prints 8-bit address for each device found
//  - if the program hangs, the bus isn't working (check pull-ups etc)
//  - if it doesn't find your device, check power/bus wiring etc

#include "mbed.h"
 
Serial pc(USBTX, USBRX);

I2C i2c(p9, p10); // I2C device sda, scl


int main() {
    i2c.frequency(10000); // set required i2c frequency
    
    pc.printf("I2C Searching!\n\n");
    pc.printf("Starting....\n\n");    
    
    while (1) {
        int count = 0;
        for (int address=0; address<256; address+=2) {
            if (!i2c.write(address, NULL, 0)) { // 0 returned is ok                
                pc.printf("I2C address 0x%02X\n", address);
                count++;
            }
        }        
        pc.printf("%d devices found\n\n\n", count);
        wait(2);       
    }
}

As it says on the tin, if you do not get any results there will be a hardware problem with either your Mbed, i2c device or wiring.

I have used this on all the Mbed platforms.

Thanks for response. I added an update...issue still persists.

posted by Fiipp Demenschonok 03 Feb 2015

Added a test program.

posted by Paul Staron 08 Feb 2015

i2c frequency is wrong, 10000 instead of 100000

posted by Carter John 03 Jul 2017

So, I've tried using this code that was posted in another question. It should locate devices connected via I2C, but the response I get is "0 devices found" Any suggestions?

posted by Allie Skaggs 15 Nov 2019

Have you added 4k7ohm 'pull up' resistors, one from SCL to +3.3v and another from SDA to +3.3v ?

posted by Paul Staron 17 Nov 2019
9 years, 2 months ago.

I2C issues seem to crop up every few weeks. Reference this checklist to help in debugging your I2C problems.

https://developer.mbed.org/questions/5910/I2C-problem/#answer6122

Thanks for response. I added an update...issue still persists.

posted by Fiipp Demenschonok 03 Feb 2015