kpower
Published © GPL3+

Arduino and MCP4131 digitally controlled potentiometer (DCP)

Use the Arduino with a MCP4131 digitally controlled potentiometer to create a digital variable resistor

IntermediateFull instructions provided3 hours9,147
Arduino and MCP4131 digitally controlled potentiometer (DCP)

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Microchip MCP4131
×1
Breadboard (generic)
Breadboard (generic)
×1
Resistor 100 ohm
Resistor 100 ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

MCP4131 and Arduino Schematic

Code

MCP4131 Arduino Control

Arduino
/*
 * This sketch shows how an Arduino can control a MCP4131 digital potentiometer
 * Sketch utilizes the MCP4131 library
 * https://www.arduino.cc/reference/en/libraries/mcp4131-library/
 * After initialization, two loops in program increment and decrement
 * potentiometer from 0 to full value and then back again
 * 
 * February 2022
*/


#include <MCP4131.h>

const int chipSelect = 10;
unsigned int wiperValue;
int i;

// Create MCP4131 object nominating digital pin used for Chip Select
MCP4131 Potentiometer(chipSelect);

void setup() {
  // Reset wiper position to 0 Ohms  
  wiperValue = 0;
  Potentiometer.writeWiper(wiperValue);

  // Begin Serial port and print out welcome message
  Serial.begin(9600);
  Serial.println("MCP4131 Digital Potentiometer Test");
  
}

void loop() {
  // Starting at 0 Ohms increment MCP4131 up to max resistance
  for ( i = 0 ; i <= 128; i++){
    Potentiometer.incrementWiper();
    delay(100);
  }

  // Print out message showing voltage is turning
  Serial.println("5V turning point");
  
  // Starting at max resistance decrement MCP4131 to 0 Ohms
  for ( i = 128 ; i >= 0; i--){
    Potentiometer.decrementWiper();
    delay(100);
  }

  // Print out message showing voltage is turning
  Serial.println("0V turning point");
}

MCP4131 Arduino Control from User Input

Arduino
/*
 * This sketch shows how an Arduino can control a MCP4131 digital potentiometer
 * Sketch utilizes the MCP4131 library
 * https://www.arduino.cc/reference/en/libraries/mcp4131-library/
 * After initialization, serial monitor waits for input
 * User inputs a value between 0 and 128
 * (Note - no error checking in case of invalid input)
 * Program sets potentiometer value to match input
 * and prints out nominal voltage
 * 
 * February 2022
*/


#include <MCP4131.h>

const int chipSelect = 10;
unsigned int wiperValue;
String incomingString;

// Create MCP4131 object nominating digital pin used for Chip Select
MCP4131 Potentiometer(chipSelect);

void setup() {
  // Reset wiper position to 0 Ohms
  wiperValue = 0;
  Potentiometer.writeWiper(wiperValue);

  // Begin Serial port and print out welcome message
  Serial.begin(9600);
  Serial.println("MCP413451 Digital Potentiometer Test");
}

void loop() {
  // Wait for user input via serial monitor
  if (Serial.available() > 0){
     //Read the incoming string
     incomingString = Serial.readString();
     //Convert to a integer
     wiperValue = incomingString.toInt();
     //Set Potentiomter to step value
     Potentiometer.writeWiper(wiperValue);
  
     // Print out user input and nominal voltage
     Serial.print(wiperValue);
     Serial.print('\t');
     Serial.print("Nominal Voltage: ");
     Serial.print((5.0 * wiperValue / 128.0) ,DEC);
     Serial.println();
  }

  delay(200);
}

MCP4131 Arduino Control Read Wiper

Arduino
/*
 * This sketch shows how an Arduino can control a MCP4131 digital potentiometer
 * Sketch utilizes the MCP4131 library
 * https://www.arduino.cc/reference/en/libraries/mcp4131-library/
 * After initialization, serial monitor waits for input
 * User inputs a value between 0 and 128
 * (Note - no error checking in case of invalid input)
 * Program sets potentiometer value to match input
 * Progam increments MCP4131 two steps and then reads potentiometer value
 * Prints out user input and potentiometer value after increment
 * 
 * February 2022
*/


#include <MCP4131.h>

const int chipSelect = 10;
unsigned int wiperValue;
byte readWiperValue;
int i;
String incomingString;

// Create MCP4131 object nominating digital pin used for Chip Select
MCP4131 Potentiometer(chipSelect);

void setup() {
  // Reset wiper position to 0 Ohms
  wiperValue = 0;
  Potentiometer.writeWiper(wiperValue);

  // Begin Serial port and print out welcome message
  Serial.begin(9600);
  Serial.println("MCP4131 Digital Potentiometer Test");
}

void loop() {
  // Wait for user input via serial monitor
  if (Serial.available() > 0){
     //Read the incoming string
     incomingString = Serial.readString();
     //Convert to a integer
     wiperValue = incomingString.toInt();
     //Set Potentiomter to step value
     Potentiometer.writeWiper(wiperValue);
     
     //Print out input value
     Serial.print("Input Wiper Value  ");
     Serial.println(wiperValue);
     
     //Increment wiper two steps
     Potentiometer.incrementWiper();
     Potentiometer.incrementWiper();
     
     //Read in Wiper value and print
     readWiperValue = Potentiometer.readWiper();
     Serial.println(readWiperValue);
     delay(250);
  }
}

Credits

kpower

kpower

19 projects • 6 followers
Qualified Electrical Engineer with experience in software and hardware development

Comments