Search code examples
arduinomicrochiparduino-ide

How to control an Mcp41010 wiper position with an analog value


I am using an Mcp41010 digipot chip and am wondering how to vary the wiper position of the chip with an analog input voltage that I can adjust, I need a way of decrementing(--) the wiper position if the voltage goes over a certain point and incrementing(++) the wiper position of the chip back to the normal position this is some code that i found that just fades the wiper position up and down I need a way of controlling it. I am still very new to arduino so sorry if my explanation was clear enough.

int CS_signal = 12;                      // Chip Select signal onsul pin 2 of Arduino
int CLK_signal = 52;                     // Clock signal on pin 4 of Arduino
int MOSI_signal = 51;                    // MOSI signal on pin 5 of Arduino
byte cmd_byte2 = B00010001 ;            // Command byte
int initial_value = 100;                // Setting up the initial value

void initialize() {                     // send the command byte of value 100 (initial value)
spi_out(CS_signal, cmd_byte2, initial_value);
}

void spi_out(int CS, byte cmd_byte, byte data_byte){                         // we need this function to send command byte and data byte to the chip

digitalWrite (CS, LOW);                                                 //  to start the transmission, the chip select must be low
spi_transfer(cmd_byte); // invio il COMMAND BYTE
delay(2);
spi_transfer(data_byte); // invio il DATA BYTE
delay(2);
digitalWrite(CS, HIGH);                                                 //     to stop the transmission, the chip select must be high
}

void spi_transfer(byte working) {
for(int i = 1; i <= 8; i++) {                                           //      Set up a loop of 8 iterations (8 bits in a byte)
if (working > 127) { 
digitalWrite (MOSI_signal,HIGH) ;                                    // If the MSB is a 1 then set MOSI high
} else { 
digitalWrite (MOSI_signal, LOW) ; }                                  // If the MSB is a 0 then set MOSI low                                           

digitalWrite (CLK_signal,HIGH) ;                                        //  Pulse the CLK_signal high
working = working << 1 ;                                                // Bit-shift the working byte
digitalWrite(CLK_signal,LOW) ;                                          // Pulse the CLK_signal low
}
}

void setup() {
pinMode (CS_signal, OUTPUT);
pinMode (CLK_signal, OUTPUT);
pinMode (MOSI_signal, OUTPUT);

initialize();

Serial.begin(9600);                                                     // setting the serial speed
Serial.println("ready!");
}

void loop() {
for (int i = 0; i < 255; i++) {
spi_out(CS_signal, cmd_byte2, i); 
Serial.println(i); delay(10); 
}
for (int i = 255; i > 0; --i) {
spi_out(CS_signal, cmd_byte2, i);
Serial.println(i);
delay(10);
}
}

Solution

  • int CS_signal = 12;                      // Chip Select signal onsul pin 2     of Arduino
    int CLK_signal = 52;                     // Clock signal on pin 4 of Arduino
    int MOSI_signal = 51;                    // MOSI signal on pin 5 of Arduino
    byte cmd_byte2 = B00010001 ;            // Command byte
    int initial_value = 100;                // Setting up the initial value
    
    void initialize() {                     // send the command byte of value 100 (initial value)
    spi_out(CS_signal, cmd_byte2, initial_value);
    

    }

    void spi_out(int CS, byte cmd_byte, byte data_byte){                        // we need this function to send command byte and data byte to the chip
    
    digitalWrite (CS, LOW);                                                 // to start the transmission, the chip select must be low
    spi_transfer(cmd_byte); // invio il COMMAND BYTE
    delay(2);
    spi_transfer(data_byte); // invio il DATA BYTE
    delay(2);
    digitalWrite(CS, HIGH);                                                 // to stop the transmission, the chip select must be high
    }
    
    void spi_transfer(byte working) {
    for(int i = 1; i <= 8; i++) {                                           // Set up a loop of 8 iterations (8 bits in a byte)
     if (working > 127) { 
       digitalWrite (MOSI_signal,HIGH) ;                                    //  If the MSB is a 1 then set MOSI high
     } else { 
       digitalWrite (MOSI_signal, LOW) ; }                                  // If the MSB is a 0 then set MOSI low                                           
    
    digitalWrite (CLK_signal,HIGH) ;                                        // Pulse the CLK_signal high
    working = working << 1 ;                                                // Bit-shift the working byte
    digitalWrite(CLK_signal,LOW) ;                                          // Pulse the CLK_signal low
    }
    }
    
    void setup() {
    pinMode (CS_signal, OUTPUT);
    pinMode (CLK_signal, OUTPUT);
    pinMode (MOSI_signal, OUTPUT);
    
    initialize();
    
    Serial.begin(9600);                                                     // setting the serial speed
    Serial.println("ready!");
    }
    
      void loop() {
      //  read the input on analog pin 0:
      int sensorValue = analogRead(A0);
      if(sensorValue <= 200){
      //  Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
       float voltage = sensorValue * (5.0 / 1023.0);
       //   print out the value you read:
       Serial.println(voltage);
    
        int i = sensorValue;{
        spi_out(CS_signal, cmd_byte2, i); 
        Serial.println(i); 
    
       }
       }
    
       }