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 Runs in your browser. Free, and no account needed.
The parts retail for about $31. On Mokxi they're free.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// 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);
}
Parts list
5 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.
How it is wired
7 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; Digital potentiometer, MCP4131 pin VSS; Digital potentiometer, MCP4131 pin P0B; LED, green pin C
- Arduino Uno R3 pin 13; Digital potentiometer, MCP4131 pin SCK
- Arduino Uno R3 pin 11; Digital potentiometer, MCP4131 pin SDI
- Arduino Uno R3 pin 10; Digital potentiometer, MCP4131 pin CS
- Arduino Uno R3 pin 5V; Digital potentiometer, MCP4131 pin P0A; Digital potentiometer, MCP4131 pin VDD
- Arduino Uno R3 pin A0; Digital potentiometer, MCP4131 pin P0W; Resistor, 220 Ω pin 1
- Resistor, 220 Ω pin 2; LED, green pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.