2024-10-27 11:42 AM - edited 2024-10-27 11:55 AM
I recently created a digital potentiometer breakout board for the MCP4541-104E/MS, where I would set the resistance value dependent on a particular wiper value. Here is the schematic and layout (I changed the pullups from 10k to 4.7k):
Here is the code:
#include "main.h"
#include <stdio.h>
#include <string.h>
#define MCP4541_ADDR 0x2C // Device Address
#define WIPER_CMD 0x00 // Command to set the wiper
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart2;
char msg;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART2_UART_Init(void);
void Set_Potentiometer(uint8_t wiperValue) {
uint8_t data[2];
data[0] = WIPER_CMD; // Command
data[1] = wiperValue; // Wiper value
HAL_I2C_Master_Transmit(&hi2c1, MCP4541_ADDR << 1, data, 2, HAL_MAX_DELAY);
}
void Print_Value_Uart(uint8_t wiperValue) {
char msg[50];
sprintf(msg, "Wiper set to: %d\r\n", wiperValue);
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART2_UART_Init();
while (1)
{
HAL_Delay(1000);
static uint8_t currentWiperValue = 12; // Set to 5kΩ
// Set the new wiper value
Set_Potentiometer(currentWiperValue);
// Print the current wiper value via UART
Print_Value_Uart(currentWiperValue);
}
}
I am using PB6 as my SCL and PB7 as my SDA on the Nucleo L476RG. I have HVC/AC pulled to ground and wiper pin B pulled to ground. The POW pin is connected to a green LED with a 1k resistor tied to ground. P0A is floating.
The resistance value across P0A and P0B is 100k and P0A and P0W is 50k, regardless of what I set the wiper value to. I checked the SCL and SDA lines with a scope and I am getting signals sent every second, and the UART terminal outputs the following:
I'm unsure what is missing in my code or setup, any help would be appreciated.