How to Build a Digital Potentiometer Circuit Using a MCP4131


MCP4131 digital potentiometer


In this project, we are going to show how to connect a digital potentiometer to a microcontroller so that we can control the resistance put out by the potentiometer in software.

A digital potentiometer serves the same function as a potentiometer in hardware in that it varies resistance output. The difference is a digital potentiometer IC is controlled by software, while a regular potentiometer is controlled manually by a person.

Just like a regular potentiometer, a digital Pot IC comes in all different values of resistance.

The specific digital potentiometer we will use in this circuit is the MCP4131 IC. The MCP line of potentiometers come in 5KΩ, 10KΩ, 50KΩ, and 100KΩ, meaning you can purchase the IC in any of these maximum resistance values.

We will connect this digital pot IC to the arduino microcontroller. Through software, we will then be able to change the resistance output at the wiper terminal of the potentiometer.

In this circuit, we will use an LED as our output device. We will operate the potentiometer from the highest resistance (when the LED will be off) to the lowest resitsance (where the LED will be the brightest) and cycle over and over again. This will serve to demonstrate all the resistances the potentiometer can offer and how it in turn can change an output.

Components Needed

  • MCP4131 Digital Potentiometer
  • 1.5KΩ resistor
  • Ultra low-current LED
  • Arduino microcontroller


The MCP4131 is a single digital potentiometer chip.

By single, it's meant that the chip contains a single potentiometer. Some digital pot ICS contain 2 potentiometers; these are called dual potentiometers. This means you could connect 2 or more output devices.

You can download the datasheet of the MCP4131 at the following link: MCP4131 Datasheet.

The MCP4131 operates on anywhere from 1.8V to 5.5V. Since the arduino provides 5V of power, it provides a perfect power source for the MCP4131 IC.

The MCP4131 changes resistance in a total of 129 steps, from 0 to 128, or in a total of 256 steps, from 0 to 255. However, we will operate ours in the 129-step interval. Thus, with a 100KΩ resistor, each step is an increment of approximately 775Ω (100K/129≈775Ω). With a 10KΩ resistor, each step is an increment of approximately 78Ω.

The MCP4131 is a 8-pin IC.

Its pinout is shown below.

MPC4131 digital potentiometer pinout

The table below summarizes the pins of the IC.

MCP4131 Digital Potentiometer
Pin # Pin Name Description
1 CS The CS, or Chip Select, pin is the SS (slave select) pin for the SPI interface. It is active low. 0V means the chip is selected and 5V means it is not selected.
2 SCLK SCLK is the Shared/Serial Clock. It is the SPI clock line.
3 SDI/SDO These pins are the serial data in and out, also known as the MOSI and MISO.
4 VSS This is where ground is connected to.
5 PA0 This is one terminal of the potentiometer.
6 PW0 This is the wiper terminal of the potentiometer.
7 PB0 This is one terminal of the potentiometer.
8 VCC This is where the positive voltage source connects to.


The MCP4131 digital potentiometer communicates via the Serial Peripheral Interface bus, or SPI bus.

Other ways of communicating are through the I2C bus and the serial UART bus.

The SPI us was originally created by Motorola and is a full-duplex serial communication standard that enables simultaneous bidirectional communication between a master device and one or more slave devices. Many times, SPI devices connect to one another, so that there is a master SPI and then slave SPI devices which communicate in sync with each other.

With the SPI, unlike the I2C bus, the SPI bus uses separate lines for sending and receiving data, and it employs an additional line for selecting which slave device you are talking to. This adds additional wires, but also eliminates needing different slave device addresses (since it's hardware connected instead of through software). SPI runs at a faster speed than I2C and generally easier for beginners to work with.

SPI devices are synchronous, meaning that data is transmitted in sync with a shared clock signal (SCLK). Data can be shifted into the slave device on either the rising or the falling edge of the clock signal.

The Arduino has SPI support, containing a built-in library and hardware to communicate with a digitally controllable potentiometer.

The Chip Select pin decides which slave device you are communicating to. The Chip Select pin is also known as the Slave Select (SS) pin. If you just have one slave device, the chip select pin of that slave device has to drawn LOW to communicate with it, since the CS pin is an active low pin. After you are done communicating with the device, the CS pin should then be drawn HIGH.


MCP4131 Digital Potentiometer Circuit

The MCP4131 digital potentiometer circuit that we will build to control the brightness of an LED is shown below.

MCP4131 digital potentiometer circuit

This above circuit built on a breadboard is shown below.

MCP4131 digital potentiometer breadboard circuit

In this circuit, we connect pin 1, the CS pin, to digital pin 10 on the arduino.

We connect pin 2, SCK, to digital pin 13 on the arduino.

We connect pin 3, SDI/SDO, to digital pin 11 on the arduino.

We connect pin 4, GND, to the ground terminal on the arduino.

We connect pin 5, P0A, to +5V.

We connect pin 6, P0W, the wiper terminal of the potentiometer, to the output device that we want to turn on. In the case of this circuit, it is the LED which is in series with a current-limiting resistor. Being that the wiper terminal attaches to this LED, we can adjust the resistance, through software, to display different brightnesses of the LED.

According to the datasheet, the potentiometer can have a maximum of 2.5mA of current flowing through any of the terminals of the potentiometer. This is the reason we must use an ultra low-current LED that can operate with only 2mA of forward current. The power that this circuit consumes is, 5V-1.65V/1.5KΩ= 0.00223A or 2.23mA. This ensures that the current power ratings of the potentiometer are not exceeded.

We connect, Pin 7, P0B, to ground. So one of the terminals of the potentiometer are connected to ground and the other is connected to +5V.

We connect pin 8, VDD, to the +5V terminal on the arduino.


Code

The code so that we can run the full range of resistances from the potentiometer to get varying levels of light from the LED is shown below.

#include <SPI.h>

byte address = 0x00;
int CS= 10;

void setup()
{
pinMode (CS, OUTPUT);
SPI.begin();
}

void loop()
{
for (int i = 0; i <= 128; i++)
{
digitalPotWrite(i);
delay(10);
}
delay(500);
for (int i = 128; i >= 0; i--)
{
digitalPotWrite(i);
delay(10);
}
}

int digitalPotWrite(int value)
{
digitalWrite(CS, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS, HIGH);
}


The first thing is we include the SPI.h library. You shouldn't have to download this. It should automatically come with the arduino software. You just have to import the library. Then you should see the line, #include appear.

We then initialize 2 variables. These are address and CS. The Address variables holds a register value. An SPI devices uses registers to know the address of the register on which to perform a command. The MCP4131 command byte is AD3 AD2 AD1 AD0 C1 C0 D9 D8. AD3-1D0 is the address of the register on which you would like to perform a command. The address of the wiper terminal of the potentiometer is 0000. This is what allows you to change the resistance value of the potentiometer. So it's the most important address. C1 C0 is one of 4 possible commands that can be sent to the address we've chosen. We can either read data from the address, write data to the address, increment the data of the wiper by a value of 1, or decrement the data by a value of 1. The command for writing data to the address is 00. Since we want to write data to the wiper terminal (in order to change the resistance), we use 00 (write command). D9 D8 refers the data bits. D9 is undefined and is always 0. D8 refers to the total number of steps in one complete revolution. For 257-step potentiometers, D8 is used to have the full resistance scale. Otherwise, it should be 0. We keep ours as 00.

So the final register value is 00000000, which we can abbreviate as 0x00.

So the chip select pin is connected to digital pin 10 on the arduino, so we initalize the CS variable to 10.

In the setup() function, we set the CS as output. We then call the SPI.begin() function, which creates all the proper initialization so that the SPI can get up and running.

In our loop() function, we call a for loop that goes from 0 to 128, corresponding to the number of steps in a complete revolution of the potentiometer. We call the digitalWrite() function in this for loop to pass the i value to the wiper terminal. The first for loop counts from 0 to 128. So during this sequence, the LED is fully on at its brightest level at first and then gets dimmer and dimmer until it shuts off completely. The second for loop counts from 128 down to 0. So the LED during this sequence goes from being off to getting brighter and brighter till it's at its brightest level. In between these 2 sequences is a half a second delay. You can adjust this if you want.

The digitalPotWrite() function is the last block of code. In this function, we select the digital potentiometer chip by drawing the CS pin LOW. This is how we select the device, so the MCP4131 is now selected. After this, we call the address that we want to write to. Above, we initialized the address variable to 0x00; this selects the wiper terminal of the potentiometer which we will write to. We then transfer the value which is taken from the i in the for loop to the wiper terminal. This changes its resistance. Lastly, we draw the CS pin HIGH, ending the communication with the MCP4131. Whenever we write to a slave device, we draw the CS LOW. Afterwards, when we're finished with the function, we draw the CS HIGH, to unselect it. Typically, if you only have one SPI slave device, it wouldn't be necessary to do this. But it's good practice, hence, we show it here. But if you have multiple SPI devices you are communicating with, you would have to draw the CS pin HIGH. This is because you can only communicate with one device at a time. Therefore, you would have to draw the CS pin HIGH after communicating with a device.


Related Resources

How to Build a Digital Potentiometer Circuit Using a MCP4131

HTML Comment Box is loading comments...