Tuesday, May 30, 2017

Teensy 3.6 Basics: Using a Digital Pot

A digital pot is a digitally-controlled potentiometer, which is a very useful device. It allows a microcontroller, such as the Teensy, to change a potential or a resistance using data values instead of having to turn a physical pot with a human hand. There are many instances where a physical pot is used can be substituted with a digital pot.

Uses 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 IC contains two digital pots, with each being 100k. The resolution is 7 bits for resistance. The two channels can be addressed independently. The Teensy 3.6 uses an SPI bus to communicate with the MCP4241.

Hardware Setup
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 (or CLK): 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 (or MOSI): 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 (or MISO): 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, the resistance of a digital potentiometer is set using data from a host microntroller. As a result, only CS, SCK and SDI are required, because the information needs to travel only from the host to the slave device (i.e. from the Teensy to the MCP4241).

The hardware setup is shown below.

  • Digital output pin 10 of the Teensy is SPI Chip Select (CS or SS), and is connected to pin 1 of the MCP4241. 
  • Digital output pin 13 of the Teensy is SPI Clock (SCK or CLK), and is connected to pin 2 of the MCP4241. 
  • Digital output pin 11 of the Teensy is SPI digital output (MOSI or SDI). Teensy ground is connected to pin 4 of the MCP4241 (VSS). 
  • Teensy 3V is connected to pin 11, 12, 14 of the MCP4241 (WP or Write Protect, SHDN or Shutdown, and VDD respectively). 
The first digital pot is formed from pins 8, 9 and 10 while the second is pins 7, 6 and 5 of the MCP4241. 

Software Setup
The MCP4241 can be easily addressed using the SPI library in combination with writing the correct bytes and addresses to the chip. 

An example encapsulation is shown below: 

This function can then be used, as shown below in the example code. 

Example 1 - Incrementally Increase Resistance, and Loop
In this example, the digital pot is connected via the SPI bus. A multimeter is set to measure resistance with a range of 0 - 200K. The value of the digital pot is controlled so that the resistance increases by one step for every 100 milliseconds, looping after 128 steps. 

This is a simple example of how to use a digital pot with the Teensy 3.6. The digital pot can be used to map MIDI control to change in resistance, thus allowing a setup such as this to control, say, the volume and pitch of a 40106 oscillator (or a wide array of signals).