Skip to content

Instantly share code, notes, and snippets.

@gsampallo
Created September 13, 2020 21:51
Show Gist options
  • Save gsampallo/d90fca1ef87e8462b0a01ad4d59f0f9e to your computer and use it in GitHub Desktop.
Save gsampallo/d90fca1ef87e8462b0a01ad4d59f0f9e to your computer and use it in GitHub Desktop.
How to use a digital potenciometer with Arduino (MCP 4131-103)
/*
* How to use a digital potenciometer with Arduino
* Link to article: https://www.gsampallo.com/?p=952
*/
#include <SPI.h>
byte address = 0x00;
int CS = 10;
void setup() {
pinMode (CS, OUTPUT);
SPI.begin();
}
void loop() {
for (int i = 0; i <= 128; i++) {
digitalPotWrite(i);
delay(10);
}
delay(500);
for (int i = 128; i >= 0; i--) {
digitalPotWrite(i);
delay(10);
}
}
int digitalPotWrite(int value) {
digitalWrite(CS, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment