MCP41100 digital potentiometer with Arduino/STM32

In this video I show you a very simple but useful circuit. You just need a microcontroller (Arduino or STM32) and the MCP41100 8-bit digital potentiometer. The potmeter is controlled via the SPI protocol. I will use this in a digital power supply that I am going to build soon, so I thought I share this step of the process with you.



Wiring diagram

This is a simplified wiring diagram for the MCP41100 digital potentiometer. The two potentiometers are using the same SPI pins, but their chip select (SS or CS) pins are controlled by different digital pins. This is how we switch between the two dif…

This is a simplified wiring diagram for the MCP41100 digital potentiometer. The two potentiometers are using the same SPI pins, but their chip select (SS or CS) pins are controlled by different digital pins. This is how we switch between the two different chips. The outputs of the potentiometers are “potmeter 1” and “potmeter 2”. Since, they work as voltage dividers, the outputs simply give some voltage based on the settings we apply via the microcontroller. A0 and A1 is used to confirm the values of the potmeters by measuring the output voltages.



STM32/Arduino source code

//STM32-based digital potmeter
//The code works with Arduino as well, you just have to change the pins
#include <SPI.h> //SPI comunication

int analogValue_1;
int analogValue_2;

const byte analogInputPin_1 = PA0;
const byte analogInputPin_2 = PA1;

const byte CS1_pin = PA4; //CS pin for potmeter 1
const byte CS2_pin = PB9; //CS pin for potmeter 2

float rwa_1;
float rwa_2;
float rwb_1;
float rwb_2;

int counter_1 = 0; //counter for the potmeter 1
int counter_2 = 256; //counter for the potmeter 2

void setup()
{
 Serial.begin(9600);
 Serial.println("MCP41100");

 pinMode(CS1_pin, OUTPUT); //Chip select is an output
 pinMode(CS2_pin, OUTPUT); //Chip select is an output
 
 digitalWrite(CS1_pin, HIGH);
 digitalWrite(CS2_pin, HIGH);
 
 SPI.begin();
}

void loop() 
{
  
  writePotmeter_1();
  writePotmeter_2();
  
  delay(300);

  readAnalogPin_1();
  readAnalogPin_2();

  counter_1++;
  counter_2--;

  if(counter_1 > 256) //this is for the increasing counter
  {
  counter_1 = 0;  
  }
  
  if(counter_2 < 1)  //this is for the decreasing counter
  {
  counter_2 = 256;  
  }

}

void writePotmeter_1()
{   
    //CS goes low
    digitalWrite(CS1_pin, LOW);   
    
    SPI.transfer(0x11);  //command 00010001 [00][01][00][01]    
    SPI.transfer(counter_1);  
    
    delayMicroseconds(100);
    Serial.print("counter_1: ");    
    Serial.println(counter_1);    
    
    //CS goes high
    digitalWrite(CS1_pin, HIGH);
  
}

void writePotmeter_2()
{
    
    digitalWrite(CS2_pin, LOW);   
    
    SPI.transfer(0x11);  //command 00010001 [00][01][00][11]    
    SPI.transfer(counter_2);  
    
    delayMicroseconds(100);    
    Serial.print("counter_2: ");    
    Serial.println(counter_2);
    //End of transaction

    //CS goes high
    digitalWrite(CS2_pin, HIGH); 
}

void readAnalogPin_1()
{
  analogValue_1 = analogRead(analogInputPin_1);
  Serial.print("Analog_1: ");
  Serial.println(analogValue_1);  
  
  //Calculate resistance
  rwa_1 = (100000 * (256 - counter_1)/256) + 125;
  rwb_1 = (100000 * (counter_1)/256) + 125;

  Serial.print("rwa_1: ");
  Serial.println(rwa_1);  
  Serial.print("rwb_1: ");
  Serial.println(rwb_1);  
  Serial.print("sum: ");
  Serial.println(rwa_1+rwb_1);
  
}

void readAnalogPin_2()
{
  analogValue_2 = analogRead(analogInputPin_2);
  Serial.print("Analog_2: ");
  Serial.println(analogValue_2);  

  
  //Calculate resistance
  rwa_2 = (100000 * (256 - counter_2)/256) + 125;
  rwb_2 = (100000 * (counter_2)/256) + 125;

  Serial.print("rwa_2: ");
  Serial.println(rwa_2);  
  Serial.print("rwb_2: ");
  Serial.println(rwb_2);  
  Serial.print("sum: ");
  Serial.println(rwa_2+rwb_2);
  
}


/*
 *  Resistance formula
 *  R = 100 kOhm *(256-x)/256 + Rw
 *  Rw = 125 for the 100 kOhm
 * 	Check datasheet, because Rw depends on the VDD  
 */

Previous
Previous

Arduino and joysticks - Part 1 - Introduction

Next
Next

Ping Pong with the AccelStepper library and two limit switches - Polling and interrupts