Friday, May 24, 2013

How To: Interface with an MCP4241 Dual Digital Pot

Overview
The aim of this post is to present a basic practical understanding of interfacing with an MCP4241 dual digital pot. A digital pot is a digitally-controlled potentiometer, which is a very useful device. It allows a microcontroller to change a potential or a resistance using data values instead of having to turn a physical pot with a human hand. Many instances where a physical pot is used can be substituted with a digital pot.

Examples include: dimming LEDs, controlling the resistance of circuit bent toys, conditioning audio signals, setting the volume of audio signals, creating auto stereo panning devices, controlling an oscillator, creating a digital to analogue converter and so on.



The MCP4241
The MCP4241-104E is a chip with 100k digitally controlled pots that are accessed via an SPI bus. Additionally, the chip features both volatile and non-volatile memory and a number of other useful functions. The pots have 7-bits of resolution (i.e. 128 steps of resistance). It can be powered from 2.7V to 5.5V.

The MCP4241 comes from a larger family of chips, the MCP414X/416X/424X/426X family. More information can be found here.



The SPI Bus
The MCP4241 connects and communicates with a microcontroller (such as an Arduino or a Teensy) via the SPI Bus. SPI is a protocol that allows microcontrollers to interface easily with a large number of external chips and sensors.

SPI is a host / slave type bus. A single host microcontroller can interface with one or more slave chips or sensors.

SPI uses up to four pins for communication:
- CS (may also be called SS): chip select:
Used by the microcontroller to select each device. The host microcontroller has a CS output pin for every slave device that is to be used.

- SCK: serial clock:
Used by the host microcontroller to time the data that is moved to and from the slave device. There is one SCK connection that is shared amongst all SPI devices.

- SDI: slave data in:
Used by the host microcontroller to send data to a slave device. There is one SDI connection that is shared amongst all SPI devices.

- SDO: slave data out:
Used by the host microcontroller to receive data from a slave device. There is one SDO connection that is shared amongst all SPI devices.

All SPI slave devices require the CS and SCK pins. However, many device may not require both the SDI and SDO connections, as they might only either send or receive data.

With the MCP4241 chip, we want to set the resistance of a digital potentiometer using data from a host microntroller. As a result, we only need CS, SCK and SDI, because the information needs to travel only from the host to the slave device.

If you are using a Teensy, the following table shows the CS, SCK, SDI and SDO pins to use:


If you are using an Arduino, the following table shows the CS, SCK, SDI and SDO pins to use:





MCP4241 Physical Layout




The above diagram shows the layout of the 14-pin MCP4241 chip. Pin 1 is to the left of the half-circle indent. Pin 14 is to the right of the half-circle indent.

Pin 1 - CS - SPI Bus Chip Select
Pin 2 - SCK - SPI Bus Serial Clock
Pin 3 - SDI - SPI Bus Slave Digital In
Pin 4 - Vss - Connect to Ground
Pin 5 - P1B - Potentiometer Number 1, Terminal B
Pin 6 - P1W - Potentiometer Number 1, Wiper
Pin 7 - P1B - Potentiometer Number 1, Terminal A
Pin 8 - P0B - Potentiometer Number 0, Terminal A
Pin 9 - P0W - Potentiometer Number 0, Wiper
Pin 10 - P0B - Potentiometer Number 0, Terminal B
Pin 11 - WP - Write Protection - Connect to 5V for normal operation
Pin 12 - SHDN - Shutdown - Connect to 5V for normal operation
Pin 13 - SDO - SPI Bus Slave Digital Out
Pin 14 - Vdd - Connect to 5V



MCP4241 Breadboard Layout - Basic Functions - With Teensy
Set up the power for the MCP4241:
• Connect ground from the Teensy to the ground bus on the breadboard.
• Connect 5V from the Teensy to the 5V bus on the breadboard.
• Connect 5V to Vdd (pin 14) of the MCP4241.
• Connect ground to Vss (pin 4) of the MCP4241.


Initialise the additional function pins of the MCP4241:
• Connect 5V to SHDN (pin 12) of the MCP4241
• Connect 5V to WP (pin 11) of the MCP4241



Connect the SPI Bus to the MCP4241: 
• Connect Teensy digital pin 0 to CS (pin 1) of the MCP4241
• Connect Teensy digital pin 1 to SCK (pin 2) of the MCP4241
• Connect Teensy digital pin 2 to SDI (pin 3) of the MCP4241


Connect to MCP4241 potentiometer 0 for testing:
• Connect P0W (pin 9 of the MCP4241 to a probe of a multimeter
• Connect P0A (pin 9 of the MCP4241) to the other probe of a multimeter

 
Set up the multimeter: 
• Set the multimeter to the 2000Ω range of resistance. 



Programming the Digital Pot
In general, the digital pot is easy to program using the Arduino SPI library, which is included in the Arduino distribution. This library is compatible with Teensy. 

The library is very easy to use, but requires a little setting up as follows: 
• the CS / SS pin needs to be set up manually in the Arduino code
• the SCK, SDI and SDO do not need to be manually set up, as the library does this

A basic test program is shown below. 

Note that this includes a function called writeMCP4241. This function takes two arguments - address and value. The address sets which pot to control (either 0 or 1) and the value sets the value to write to the pot (0 - 127). 

The example simply moves through all 128 possible values for both pots. Measuring the resistance between P0A and P0W should change over time as all 128 values are cycled through.





Demonstration Video

0 comments: