I recently bought a Digital Potentiometer (MCP41010), and I am wanting to control it using the Arduino Pro Mini (5V, ATmega328), communicating using the SPI library.
First I tested the Digital Potentiometer using an Arduino Mega, connecting the SS to pin 53, SCK to pin 52, and the MOSI to pin 51, and ran a test sketch that worked fine.
However when I run the test sketch on a 5V Arduino Pro Mini, it does not work (it appears to be sending random data to the Pot). When using the Pro Mini, I connect SS to pin 10, SCK to pin 13 and MOSI to pin 11.
Below is the sketch I used. Note that when swapping between Arduino Pro Mini and Arduino MEGA, the variable SSpin needs to be altered.
//Test Code to find/control a digital potentiometer (MCP41010)
//Uses SPI communication
//
//Based on 'DigitalPotControl', the SPI library demo code
//
// Potentiometer connections (from chip to arduino):
// Chip Select CS(1) is connected to SS (pin 53 on Arduino mega, pin 10 on Arduino Pro mini)
// Serial Clock SCK(2) is connected to SCK (pin 52 on Arduino mega, pin 13 on Arduino Pro mini)
// Serial Input SI(3) is connected to MOSI (pin 51on Arduino mega, pin 11 on Arduino Pro mini)
// Neg Power Vss(4) is connected to GND
// Ouput pin PA0(5) is connected to GND
// Wiper pin PW0(6) is connected to pin A1
// Input pin PB0(7) is connected to 5V
// Pos Power Vdd(8) is connected to pin 3
//Include the SPI library
#include <SPI.h>
//Set the slave select pin (as in description)
const int SSpin = 10; //set to 53 for Arduino MEGA
//Set the wipe input pin (as in description)
const int Wpin = 1;
//Set the power pin (as in description)
const int POWpin = 3;
//Define variables for storing current and last read voltage
float voltage = 0;
float prevoltage = 0;
//Main setup function
void setup() {
//Set Power pin as output, and set it high to turn on chip
pinMode(POWpin, OUTPUT);
digitalWrite(POWpin, HIGH);
//Set slave select pin as output, and set it high to de-select chip
pinMode(SSpin, OUTPUT);
digitalWrite(SSpin, HIGH);
//Set wiper pin as input
pinMode(Wpin, INPUT);
//Initialise SPI
SPI.begin();
//Initialise Serial
Serial.begin(9600);
}
void loop() {
//Loop through 128 channel, to find the digital potentiometer:
for (int channel=0; channel<256; channel++) {
//Get initial voltage, and store in prevoltage
prevoltage = (float)analogRead(Wpin)*5.0/1024.0;
//Loop throught the resistance on this channel, from min to max
for (int resist=0; resist<256; resist++) {
//Set resistance
MCP41010Write(channel,resist);
delay(2);
//Read input, and convert to Volts
voltage = (float)analogRead(Wpin)*5.0/1024.0;
//If it has changed from previous voltage (by at least 0.005 Volts) display results
if (abs(voltage-prevoltage)>0.01) {
//Print results to serial
Serial.print("Resitance on Channel ");
Serial.print(channel);
Serial.print(" set to ");
Serial.print(resist);
Serial.print(", reading: ");
Serial.print(voltage);
Serial.println("V");
}
delay(2);
//Update previous voltage
prevoltage=voltage;
}
//Print update
Serial.print("Done testing channel ");
Serial.println(channel);
}
//Turn off Chip, and display end of test message
Serial.println("End of Test");
digitalWrite(POWpin, LOW);
digitalWrite(SSpin, LOW);
}
void MCP41010Write(int address, int value) {
//Take the slave select pin low, to select chip
digitalWrite(SSpin, LOW);
//Send the address, followed by the value to set
SPI.transfer(address);
SPI.transfer(value);
//Take the slave select pin high, to de-select the chip
digitalWrite(SSpin, HIGH);
}
What am I doing wrong? I feel that the problem is related to the Clock.