# I2C Communication on STM32 fails with Digital Analog Converter

I am using a MCP4706A2T-E/CH digital analog converter. I try to get the I2C communication running but for what ever reason I always get a HAL_ERROR message. The STM chip I am using is the STM32F042K6Tx. Here are my I2C configurations:

static void MX_I2C1_Init(void)
{

/* USER CODE BEGIN I2C1_Init 0 */
HAL_StatusTypeDef status;
/* USER CODE END I2C1_Init 0 */

/* USER CODE BEGIN I2C1_Init 1 */

/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x00101D2D;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_DISABLE) != HAL_OK)
{
Error_Handler();
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
uint8_t test[1] = {0x06};
status = HAL_I2C_Master_Transmit(&hi2c1, 0xC4,test, sizeof(test), 100); //00000110
uint8_t blub = 0;
/* USER CODE END I2C1_Init 2 */

}


It does not matter what I put in the test array nothing is send (status is always HAL_ERROR). The adress of my chip is 0x62. I have to shift this adress by a 0 since we send 8 bits so it becomes 0xC4.

I think that my address is correct. Do you have any idea what I do wrong? (I double checked my wireing). Here is a picture of the CubeMX setup:

• Your depicted configuration code is insufficient to actually make the pins wiggle as it does not even try to configure the actual pins but only the I2C engine, so hopefully there is more code somewhere which you neglected to include. At any rate, get a scope or cheap logic analyzer on the lines and see what, if anything, is actually happening compared to what should be. Also see if you cannot get a more precise error message. Without more details such as the rest of the code and the wiring connections, or at least more debug results this is likely to end up closed as unanswerable. Nov 19, 2020 at 17:02
• @ChrisStratton If that code is simply generated by CubeMX by configuring the IO pins as I2C, it will call and have initialized the IO pins for I2C peripheral. I don't think that is an issue. Nov 19, 2020 at 18:41
• Have you double-checked that you have the correct ADC chip type with address 0x62 (7-bit) / 0xC4 (8-bit)? Have you tried all the other addresses? What value pull-up resistances you have? Is the DAC having power supplies connected? Is it 3.3V supply? How are they connected, are SDA and SCL the right way around? Nov 19, 2020 at 18:43
• What is 0x06 referring to? Usually you would have to write into a memory within the IC. If 0x06 is the data you want to input into the DAC to get an analog value, is there a command byte/register address that you have to transmit first before transmitting a data? If there is, for example, an 8-bit command you have to transmit prior to sending the raw digital value to be converted, then that means you need to send 16-bits to the DAC. Nov 19, 2020 at 21:03
• Hi many thanks for your answers. I use this github.com/uChip/MCP47X6/blob/master/MCP47X6.cpp arduino library as a reference (since it is working with the chip and I used address 0x62). For now I do not even want to send any command just get a simple acknoledge status from the ADC to see if I2C is working. MCP4706A2T-E/CH is the model number. Nov 20, 2020 at 8:52

I solved it by adding this code in the main() loop. I checked what got send on the arduino with a logic analyzer.

    HAL_Delay(100);
uint8_t test[1] = {0x80};
uint8_t data[2] = {0x00,0xFF}; // Change 2nd Decimal to Analog output you need
uint8_t data2[2] = {0x00,0x7C}; // Change 2nd Decimal to Analog output you need

status = HAL_I2C_Master_Transmit(&hi2c1, 0xc4,test, 1, 10);
status = HAL_I2C_Master_Transmit(&hi2c1, 0xc4,data, 2, 10);
HAL_Delay(500);
status = HAL_I2C_Master_Transmit(&hi2c1, 0xc4,data2, 2, 10);
HAL_Delay(500);