Built-in project · Arduino Uno R3

Digital pot LED dimmer on an Arduino Uno

An MCP4131 digital potentiometer on SPI fades an LED up and down: a knob the Uno turns, with A0 reading the wiper. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $31. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • A digital potentiometer: a knob you turn with code
  • Sending a two-byte command over SPI
  • Chip select (CS) framing a transfer
  • Reading a voltage with analogRead to check your work
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

The MCP4131 is a 10k track with a wiper that can stop at 129 places, 0 to 128. Its ends are on 5 V and ground, so the wiper voltage runs from 0 to 5 V. That voltage feeds the LED through a 220 ohm resistor, and A0 reads it too.

The Uno sets the wiper over SPI. Every half second it moves to the next value in a list that goes up to 128 and back down, then prints the step and the voltage A0 measures.

The LED stays dark until the wiper passes about 1.8 V.

Parts list

5 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Digital potentiometer, MCP4131 pin VSSArduino Uno R3 pin GND
Digital potentiometer, MCP4131 pin P0BArduino Uno R3 pin GND
LED, green pin CArduino Uno R3 pin GND
Digital potentiometer, MCP4131 pin SCKArduino Uno R3 pin 13
Digital potentiometer, MCP4131 pin SDIArduino Uno R3 pin 11
Digital potentiometer, MCP4131 pin CSArduino Uno R3 pin 10
Digital potentiometer, MCP4131 pin P0AArduino Uno R3 pin 5V
Digital potentiometer, MCP4131 pin VDDArduino Uno R3 pin 5V
Digital potentiometer, MCP4131 pin P0WArduino Uno R3 pin A0
Resistor, 220 Ω pin 1Arduino Uno R3 pin A0
LED, green pin AResistor, 220 Ω pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// An MCP4131 digital potentiometer: a knob the Uno turns over SPI.
//
// The MCP4131 is a 10 kohm track with a wiper that stops at 129 places, 0
// to 128, set by a two-byte SPI command. Here its ends are on 5V and GND,
// so the wiper is a voltage from 0 to 5 V, and that voltage lights an LED
// through 220 ohms. A0 reads the wiper too.
//
//   MCP4131  CS -> pin 10, SCK -> pin 13, SDI -> pin 11
//            P0A -> 5V, P0B -> GND, VDD -> 5V, VSS -> GND
//            P0W -> 220 ohm -> LED -> GND, and P0W -> A0
//
// Every half second the sketch moves the wiper to the next step of a fade
// up and down, and prints the step and what A0 reads. The LED stays dark
// until the wiper passes the LED's forward voltage, about 1.8 V.
//
// THINGS TO TRY. Change the value property to 100k: the wiper's volts are
// the same but the LED is dimmer, because the track feeds it less current.

#include <SPI.h>

const int CS = 10;
const uint8_t STEPS[] = {0, 32, 64, 96, 128, 96, 64, 32};

uint8_t i = 0;

void setWiper(uint8_t value) {
  digitalWrite(CS, LOW);
  SPI.transfer(0x00);           // write, wiper 0
  SPI.transfer(value);
  digitalWrite(CS, HIGH);
}

void setup() {
  pinMode(CS, OUTPUT);
  digitalWrite(CS, HIGH);
  SPI.begin();
  Serial.begin(9600);
  Serial.println("MCP4131 on SPI: CS 10, SCK 13, SDI 11");
}

void loop() {
  uint8_t step = STEPS[i];
  setWiper(step);
  delay(5);
  int a0 = analogRead(A0);
  Serial.print("wiper ");
  Serial.print(step);
  Serial.print(" of 128, A0 reads ");
  Serial.print(a0 * 5.0 / 1023, 2);
  Serial.println(" V");
  i = (i + 1) % sizeof(STEPS);
  delay(500);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setWiper sketch.ino

Pulls CS low to start a transfer, sends a zero command byte, then the new wiper value, and pushes CS back high to finish. The chip only listens while CS is low.

void setWiper(uint8_t value) {
  digitalWrite(CS, LOW);
  SPI.transfer(0x00);           // write, wiper 0
  SPI.transfer(value);
  digitalWrite(CS, HIGH);
}

setup sketch.ino

Sets CS as an output and starts it high so the chip is idle. Then it starts SPI and opens serial at 9600 baud.

void setup() {
  pinMode(CS, OUTPUT);
  digitalWrite(CS, HIGH);
  SPI.begin();
  Serial.begin(9600);
  Serial.println("MCP4131 on SPI: CS 10, SCK 13, SDI 11");
}

loop sketch.ino

Picks the next value from STEPS, sets the wiper, waits 5 ms, and reads A0. It prints the step and the voltage, moves the index along, and waits half a second.

void loop() {
  uint8_t step = STEPS[i];
  setWiper(step);
  delay(5);
  int a0 = analogRead(A0);
  Serial.print("wiper ");
  Serial.print(step);
  Serial.print(" of 128, A0 reads ");
  Serial.print(a0 * 5.0 / 1023, 2);
  Serial.println(" V");
  i = (i + 1) % sizeof(STEPS);
  delay(500);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED never lights This project
    Check P0A goes to 5 V and P0B to ground. The LED also needs the wiper above about 1.8 V.
  • Nothing changes when the sketch runs This project
    Check CS is on pin 10, SCK on pin 13 and SDI on pin 11.
  • The wiper does not move, or it jumps to a wrong value. Digital potentiometer
    Hold CS low for the whole two-byte write, command byte first and data byte second, then raise CS. SPI mode 0 works. The data runs from 0 to 128, which is 129 steps.
  • The pot output sags or the chip is damaged when it drives a load. Digital potentiometer
    Use it as a voltage divider into a high impedance input. The wiper carries only a few mA, so buffer it with an op-amp for any load. Keep all pins between VSS and VDD.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.