Could you guys help please review my code and suggest any potential mistakes or improvements?

Good day guys, I'm currently working on a project involving the MCP3424 ADC module and an STM32 microcontroller. My intention is to verify whether the MCP3424 is connected to the master STM32 at the defined address 0x68. While the ADC module successfully connects to an Arduino at the same address, I am facing issues with the STM32 connection. This is my code ;
while (1) { /* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
if((HAL_I2C_IsDeviceReady(&hi2c1, 0x68 << 1, 100, 1000))==HAL_OK){
HAL_UART_Transmit(&huart2, (uint8_t*)"device connected\r\n", 18, 10);
}
else{
HAL_UART_Transmit(&huart2, (uint8_t*)"no device\r\n", 11, 10);
}
}
while (1) { /* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
if((HAL_I2C_IsDeviceReady(&hi2c1, 0x68 << 1, 100, 1000))==HAL_OK){
HAL_UART_Transmit(&huart2, (uint8_t*)"device connected\r\n", 18, 10);
}
else{
HAL_UART_Transmit(&huart2, (uint8_t*)"no device\r\n", 11, 10);
}
}
With this, I am still unable to establish a connection with the MCP3424 using the STM32. The error output keeps showing "no device". Could you guys help please review my code and suggest any potential mistakes or improvements? Also, if there are specific considerations or configurations needed for interfacing the MCP3424 with the STM32, please I would love to know. Thanks guys 🙏.
@Middleware & OS
Solution:
Alright @Sterling this code example can help guide you to refine your code for more detailed debugging: ```c while (1) { HAL_StatusTypeDef result = HAL_I2C_IsDeviceReady(&hi2c1, 0x68 << 1, 10, 1000);...
Jump to solution
8 Replies
Enthernet Code
Enthernet Code4mo ago
Hi @Sterling your code looks generally correct, but let's check a few things. Have you ensured that your I2C peripheral (hi2c1) is properly configured in your STM32CubeMX or your initialization code?
Sterling
Sterling4mo ago
I think so, but I'm not entirely sure. What specific configurations should I look for?
Enthernet Code
Enthernet Code4mo ago
You need to check the I2C clock speed, the GPIO pins for SCL and SDA, and make sure the I2C peripheral is enabled. Also, make sure you have pulled up resistors on the SCL and SDA lines. Without these, the I2C communication won't work correctly.
Sterling
Sterling4mo ago
Okay, I will check the pull-up resistors. Anything else?
Dtynin
Dtynin4mo ago
Hi @Sterling , you can also make sure your MCP3424 is properly powered and the ground connections between the STM32 and MCP3424 are secure. Power issues can often cause connectivity problems.
Sterling
Sterling4mo ago
The power should be fine, but I'll double check. How about the addressing? Is it being done correctly?
RED HAT
RED HAT4mo ago
@Sterling Yes, you're correctly shifting it by a bit. The MCP3424 has a 7-bit address of 0x68, which translates to 0xD0 for write operations and 0xD1 for read operations when shifted. Your code looks fine in this respect.
Solution
Dtynin
Dtynin4mo ago
Alright @Sterling this code example can help guide you to refine your code for more detailed debugging:
while (1) {
HAL_StatusTypeDef result = HAL_I2C_IsDeviceReady(&hi2c1, 0x68 << 1, 10, 1000);

if(result == HAL_OK) {
HAL_UART_Transmit(&huart2, (uint8_t*)"device connected\r\n", 18, 10);
} else {
HAL_UART_Transmit(&huart2, (uint8_t*)"no device\r\n", 11, 10);

char buffer[50];
sprintf(buffer, "I2C Error: %d\r\n", result);
HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), 10);
}
}
while (1) {
HAL_StatusTypeDef result = HAL_I2C_IsDeviceReady(&hi2c1, 0x68 << 1, 10, 1000);

if(result == HAL_OK) {
HAL_UART_Transmit(&huart2, (uint8_t*)"device connected\r\n", 18, 10);
} else {
HAL_UART_Transmit(&huart2, (uint8_t*)"no device\r\n", 11, 10);

char buffer[50];
sprintf(buffer, "I2C Error: %d\r\n", result);
HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), 10);
}
}
This will print a detailed error message to help diagnose specific I2C issues
Want results from more Discord servers?
Add your server