# PIC32 I2C not operating as expected

**URL:** <https://forum.core-electronics.com.au/t/pic32-i2c-not-operating-as-expected/524>\
**Category:** PIC Microchip\
**Created:** [17 November 2016 21:03 UTC](https://forum.core-electronics.com.au/t/pic32-i2c-not-operating-as-expected/524 "2016-11-17T21:03:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yumi22820](https://avatars.discourse-cdn.com/v4/letter/y/e9c0ed/32.png) [@yumi22820](https://forum.core-electronics.com.au/u/yumi22820)\
**Post date:** [17 November 2016 21:03 UTC](https://forum.core-electronics.com.au/t/pic32-i2c-not-operating-as-expected/524/1 "2016-11-17T21:03:12Z")

</div>

I’m having a problem getting I2C running with a [PIC32MX795F512L](http://www.kynix.com/uploadfiles/pdf8798/PIC32MX795F512LT-80I2fPT_16984.pdf). It was not acknowledging the address byte being sent out, so I put a logic analyzer on SDA and SCL and got the trace shown below:

 ![](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/1X/a62879f8bac1102d3feed9e14bd9e57dd8634ed2.jpg)  
The two excursions on the SCL line are the only ones that occur. So I don’t understand why there are only two, instead of one per bit plus the start and stop conditions. This is obviously why I never see any acknowledgment.

Also, SCL is low all of the time before these sequence starts, and I expected it to be high and then go low with SDA high to indicate a start condition. I have checked that I have pullups on both SCL and SDA.

The code I am using is straight out of Microchip’s I2C example code. Here is the relevant part:  
// Start the transfer to write data to the EEPROM  
if( !StartTransfer(FALSE) )  
{  
while(1);  
}

```
// Transmit all data
Index = 0;
while( Success && (Index < DataSz) )
{
    // Transmit a byte
    if (TransmitOneByte(i2cData[Index]))
    {
        // Advance to the next byte
        Index++;

        // Verify that the byte was acknowledged
        if(!I2CByteWasAcknowledged(EEPROM_I2C_BUS))
        {
            DBPRINTF("Error: Sent byte was not acknowledged\n");
            Success = FALSE;
        }
        else
        {
            Success = TRUE; 
        }
    }
    else
    {
        Success = FALSE;
    }
}

// End the transfer (hang here if an error occured)
StopTransfer();
if(!Success)
{
    while(1);
}

```

It gets to the I2CByteWasAcknowledged call and fails. Anyone have any pointers on getting I2C on a PIC32 to work with the Peripheral Library code?

---

<div class="post-metadata">

**Author:** ![Sam](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/sam/32/99_2.png) [@Sam](https://forum.core-electronics.com.au/u/Sam)\
**Post date:** [17 November 2016 22:53 UTC](https://forum.core-electronics.com.au/t/pic32-i2c-not-operating-as-expected/524/2 "2016-11-17T22:53:53Z")

</div>

Hi Yumi,  
Unfortunately I have no idea on using the PIC microcontroller gear, perhaps @Graham might be the man.
