Monday, May 14, 2018

MCP3008 - Analog to Digital Converter with SPI Interface

MCP3008 is an 8-channel, 10-bit analog to digital converter from Microchip.




Figure. MCP3008 pins.


It can be used as 8 single ended analog inputs or 4 differential input pairs. Power supply is 2.7 V to 5 V and its operating temperature is -40 °C to +85 °C. It has an SPI interface which can be operated in mode 0 or mode 3.


Figure. An example SPI mode 0 communication.





Figure. MCP3008 control bits.


A schematic diagram for connecting MCP3008 with Arduino Uno is shown below.


Figure. Schematic for Arduino Uno and MCP3008 connection.


An example program to read analog input channel 0 is shown in the following list.
#include < SPI.h >
const int CS_Pin = 10;// set pin 10 as the chip select

SPISettings settingsA(2000000, MSBFIRST, SPI_MODE0); 
// set up the speed, data order and data mode
//SPI pin configuration: pin 11 as MOSI (SDI), 
//  pin 12 as MISO (SDO) , pin 13 as clock (SPC)

int x;
float K=0.00488758553; 
// (5/1023) V per digit for +5V full scale 
//  using 10 bit digital output

void setup() {
  Serial.begin(9600);
  pinMode (CS_Pin, OUTPUT);  
  digitalWrite(CS_Pin, HIGH);
  SPI.begin();
  SPI.beginTransaction(settingsA);
}

void loop() {
  delay(1000);
  digitalWrite(CS_Pin, LOW);//Enable SPI
  SPI.transfer(0x01);//Send start bit
  x = SPI.transfer(0x80)<<8 | SPI.transfer(0); //read channel 0
  digitalWrite(CS_Pin, HIGH);//Disable SPI
  x&=0x03FF;//mask out invalid bits
  Serial.println("x=" + String(x)+"   \t Voltage=" + String(K*x)+" V");
}



Figure. Output at serial monitor of Arduino.


No comments:

Post a Comment

Comments are moderated and don't be surprised if your comment does not appear promptly.