Loading...

MCP3208

The MCP3208 is a 12bit, 8 channel, Serial Peripheral Interface (SPI) Analogue to Digital Converter. It comes as an 16 pin DIP (also soic) pinout below, get the datasheet here. Also available as the 3201/2/4 with 1, 2 and 4 channels.

NOTE: code is for single ended operation, read the datasheet for differential operation.

D10 indicates arduino digital pin 10 etc.

Pinout:

  • 1-8 - chan 0-7 -> the 8 levels to be measured
  • 9 DGND -> GND
  • 10 CS chip select -> D10
  • 11 Din MOSI -> D11
  • 12 Dout MISO -> D12
  • 13 CLC clock -> D13
  • 14 AGN -> GND
  • 15 Vref -> reference voltage (that gives max adc reading)
  • 16 Vdd -> supply voltage, max 5.5V so Arduino 5V is fine
   ___ 
1 | u | 16 
2 |   | 15 
3 |   | 14 
4 |   | 13 
5 |   | 12 
6 |   | 11 
7 |   | 10 
8 |___| 9 

Code:

#define SELPIN 10 //Selection Pin 
#define DATAOUT 11//MOSI 
#define DATAIN  12//MISO 
#define SPICLOCK  13//Clock 
int readvalue; 

void setup(){ 
 //set pin modes 
 pinMode(SELPIN, OUTPUT); 
 pinMode(DATAOUT, OUTPUT); 
 pinMode(DATAIN, INPUT); 
 pinMode(SPICLOCK, OUTPUT); 
 //disable device to start with 
 digitalWrite(SELPIN,HIGH); 
 digitalWrite(DATAOUT,LOW); 
 digitalWrite(SPICLOCK,LOW); 

 Serial.begin(9600); 
} 

int read_adc(int channel){
  int adcvalue = 0;
  byte commandbits = B11000000; //command bits - start, mode, chn (3), dont care (3)

  //allow channel selection
  commandbits|=((channel-1)<<3);

  digitalWrite(SELPIN,LOW); //Select adc
  // setup bits to be written
  for (int i=7; i>=3; i--){
    digitalWrite(DATAOUT,commandbits&1<<i);
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    digitalWrite(SPICLOCK,LOW);    
  }

  digitalWrite(SPICLOCK,HIGH);    //ignores 2 null bits
  digitalWrite(SPICLOCK,LOW);
  digitalWrite(SPICLOCK,HIGH);  
  digitalWrite(SPICLOCK,LOW);

  //read bits from adc
  for (int i=11; i>=0; i--){
    adcvalue+=digitalRead(DATAIN)<<i;
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    digitalWrite(SPICLOCK,LOW);
  }
  digitalWrite(SELPIN, HIGH); //turn off device
  return adcvalue;
}


void loop() { 
 readvalue = read_adc(1); 
 Serial.println(readvalue,DEC); 
 readvalue = read_adc(2); 
 Serial.println(readvalue,DEC); 
 Serial.println(" "); 
 delay(250); 
}