7 years, 9 months ago.

I2C Connection Problem

I'm trying to use I2C on a nucleo L476RG for comms with an LCD display

I'm using some simple I2C scanner code:

  1. include "mbed.h" I2C i2c(PB_9, PB_8); sda, scl Serial pc(USBTX, USBRX); tx, rx

int main() { pc.printf("RUN\r\n"); for(int i = 0; i < 128 ; i++) { i2c.start(); if(i2c.write(i << 1)) pc.printf("0x%x ACK \r\n",i); Send command string i2c.stop(); } }

I have pullups on both SDA and SCL (PB9 and PB8) at 2.2K as recommended but when running the program I get every I2C address back as ACK

Any help appreciated,

Andy

1 Answer

7 years, 9 months ago.

Hi Andy. Please use the <<code>> and <</code>> tags in the future to format your code for easier viewing.

include "mbed.h"

I2C i2c(PB_9, PB_8); //sda, scl 

Serial pc(USBTX, USBRX); // tx, rx 

int main() 
{ 
   pc.printf("RUN\r\n"); 

   for(int i = 0; i < 128 ; i++) 
   { 
        i2c.start(); 
        if(i2c.write(i << 1)) 
            pc.printf("0x%x ACK \r\n",i); 
           // Send command string 
         i2c.stop(); 
         } 
    }

remove the i2c.start() and i2c.stop() calls. Be sure there is a common ground between your mbed board and the external LCD.

Test again and post your results.

https://developer.mbed.org/handbook/I2C