Arduino Tutorial: How to use a Digital Potentiometer with Arduino

What is a Digital Potentiometer and why might I need one?

A Circuit with a Mechanical Potentiometer

Consider the common 555 based audio oscillator:

audioOscilator_schemThis device makes a sound (a note). Turning the potentiometer changes the pitch of the note. You could play a tune by quickly turning the knob to the exact position to generate specific notes.

That would be kind of hard.

Wouldn’t it be nice if your Arduino could turn the knob? Then you’d get exactly the pitch you need.

A digital potentiometer behaves the same as the mechanical potentiometer in the circuit, except that instead of having a knob you can turn, it controlled by an interface.

Arduino controlled 555 noisemaker circuit

audioOscilatorDigiPotArduino_schemArduino code

/*
  Based on Digital Pot Control  by Tom Igoe
  This example controls a Microchip 41010 digital potentiometer.
  The MCP41010 is SPI-compatible. 
  To command it, you send two bytes: First the command 
  and then the code for the desired resistance (0 - 255).  
 */
 
#include <SPI.h>  // include the SPI library
const int slaveSelectPin = 10;
 
void setup() {
  pinMode (slaveSelectPin, OUTPUT); // slaveSelectPin is an output  
  SPI.begin();    // initialize SPI:
}
 
void loop() {
  // fade the resistance up
  for (int level = 0; level < 255; level++) 
  {
    digitalPotWrite(level);
    delay(10);
  }
  delay(1000);  // wait a second at the top
  for (int level = 255; level > 0; level--)  // now down
  {
    digitalPotWrite(level);
    delay(10);
  }
}
 
void digitalPotWrite(byte value) 
{
  // take the SS pin low to select the chip:
  digitalWrite(slaveSelectPin,LOW);
  SPI.transfer(B00010001); // The command byte
  SPI.transfer(value);     // The data byte
  // take the SS pin high to de-select the chip
  digitalWrite(slaveSelectPin,HIGH); 
}

  • Also possible to play a tune
    /*
      Based on Digital Pot Control  by Tom Igoe
      This example controls a Microchip 41010 digital potentiometer.
      The MCP41010 is SPI-compatible. 
      To command it, you send two bytes: First the command 
      and then the code for the desired resistance (0 - 255).  
     */
     
    #include <SPI.h>  // include the SPI library
    const int slaveSelectPin = 10;
     
    void setup() {
      pinMode (slaveSelectPin, OUTPUT); // slaveSelectPin is an output  
      SPI.begin();    // initialize SPI:
    }
     
    void loop() {
      digitalPotWrite(0);
      delay(200);
      digitalPotWrite(037);
      delay(300);
      digitalPotWrite(128);
      delay(500);
      digitalPotWrite(192);
      delay(400);
      digitalPotWrite(255);
      delay(100);
    }
     
    void digitalPotWrite(byte value) 
    {
      // take the SS pin low to select the chip:
      digitalWrite(slaveSelectPin,LOW);
      SPI.transfer(B00010001); // The command byte
      SPI.transfer(value);     // The data byte
      // take the SS pin high to de-select the chip
      digitalWrite(slaveSelectPin,HIGH); 
    }

    For an extra challenge, use Blink Without Delay  to make LED blink while still playing the tune. The blinking must not interrupt the tune.

Related links

 
Print Friendly
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

One thought on “Arduino Tutorial: How to use a Digital Potentiometer with Arduino

  1. Pingback: Digital Potentiometer – Crazy For Electronics

Leave a Reply

Your email address will not be published. Required fields are marked *