Arduino with MCP3304 13bit adc converter

Hello all i have bought the MCP3304 13b adc converter and having a problem with it i can not find a sketch that will work i did find something on the arduino forum page but didn't work can someone help me out please trying to program this chip it's a spi chip.

Joseph

So what have you tried? How did you wire it up?
Can you post a schematic of what you have done and post the code of what you have tried.

hello thanks for the reply not good at drawing but i did mange to figure out the wiring wasn't that hard i just can't find a sketch to it i did find this page Arduino Playground - MCP3208 my chip is the 3304 one not the 3308 ummm i can take a snapshot of what i did with my cellphone to show but that's all.

this is how i wired it up
CLK to D13
Dout to D11
Din to D12
CS/SHDN to D10
AGND and DGND to GND
VDD to 5V

this is the image to the pinout of the chip

(deleted)

hello spycatcher2k yes i did the wiring is not the problem i can't not find a sketch that will work i looked online all over and only found the one in the arduino forum page and i tried it and nothing. I'm not a coder so i can't tell what is good or bad I'm trying to learn but it's a little over my head or i would make a sketch my self.

At least post your code as Mike asked so we have a chance of seeing any problems.


Rob

What are you connecting Vref to?

To use it single-ended mode I think the SPI transfer code will be:

#include <SPI.h>

void setup ()
{
  SPI.begin () ;
  SPI.setBitOrder (MSBFIRST) ;
  SPI.setDataMode (SPI_MODE0) ;
  SPI.setClockDivider (SPI_CLOCK_DIV16) ;
  ...
}

int read_adc (int channel)
{
  digitalWrite (10, LOW) ;
  channel &= 7 ;
  spi.transfer (0x0C | (channel >> 1)) ;
  int  result = spi.transfer (channel << 7) & 0x0F ;
  result <<= 8 ;
  result |= spi.transfer (0) ;
  digitalWrite (10, HIGH) ;
  return result ;
}

Untested, based on reading section 6.3 of the datasheet

hello the code i was trying to use was from Arduino Playground - MCP3208
but all it came up with are 1s and 0s

 // read the MCP3304 in quad differential mode, non-bit-banging version
#include <Serial.h>
#include <SPI.h>

#include <HardwareSerial.h>

long ticks = 0;

// for Uno
#define SELPIN 10    // chip-select
#define DATAOUT 11   // MOSI 
#define DATAIN 12    // MISO 
#define SPICLOCK 13  // Clock 

// for Mega2560 / Max32
//#define SELPIN 53    // chip-select
//#define DATAOUT 51   // MOSI 
//#define DATAIN 50    // MISO 
//#define SPICLOCK 52  // Clock 

int readvalue; 


void setup(){ 
  //set pin modes 
  pinMode(SELPIN, OUTPUT); 

  // disable device to start with 
  digitalWrite(SELPIN, HIGH); 

  SPI.setClockDivider( SPI_CLOCK_DIV8 ); // slow the SPI bus down
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);    // SPI 0,0 as per MCP330x data sheet 
  SPI.begin();

  Serial.begin(115200); 
}


void loop() {
  long ticks = millis();
  int A = 0, B = 0;

  A = read_adc(1);
  B = read_adc(2);

  // do whatever you want with these readings

  long tcnv = millis() - ticks;

  delay(100 - tcnv);
}

// non-bit-banging version
// channel ranges from 1.. 3 (not zero!)
// maximum clock frequency is 2.1 MHz @ 5V
//
// this is SPI_CLOCK_DIV8
// at DIV64, 512 in 60ms (8.5 ksps)
// at DIV32, 512 in 35ms (14.6 ksps)
// at DIV16, 512 in 22ms (23.3 ksps)
// at DIV8, 512 in 16ms (32 ksps)

int read_adc(int channel){
  int adcvalue = 0;
  int b1 = 0, b2 = 0;
  int sign = 0;

  // command bits for MCP3304
  // 0000 = diff, ch0 = in+, ch1 = in-
  // 0010 = diff, ch2 = in+, ch3 = in-
  // 0100 = diff, ch4 = in+, ch5 = in-

  digitalWrite (SELPIN, LOW); // Select adc

  // first byte
  // first byte will always be B000010xx where xx are the D2 and D1 channel bits  
  byte commandbits = B00001000;
  commandbits |= (channel >> 1);         // high bit of channel

  SPI.transfer(commandbits);       // send out first byte of command bits

  // second byte; Bx0000000; leftmost bit is D0 channel bit
  commandbits = B00000000;
  commandbits |= (channel << 7);        // if D0 is set it will be the leftmost bit now
  b1 = SPI.transfer(commandbits);       // send out second byte of command bits

  // hi byte will have XX0SBBBB
  // set the top 3 don't care bits so it will format nicely
  b1 |= B11100000;
  Serial.print(b1, BIN); Serial.print(" ");
  sign = b1 & B00010000;
  int hi = b1 & B00001111;

  // read low byte
  b2 = SPI.transfer(b2);              // don't care what we send
  Serial.print(b2, BIN); Serial.print("\r\n");
  int lo = b2;
  digitalWrite(SELPIN, HIGH); // turn off device

  int reading = hi * 256 + lo;

  if (sign) {
    reading = (4096 - reading) * -1;
  }

  return (reading);
}

Grumpy_Mike:
What are you connecting Vref to?

Also what are you connecting your analogue inputs to?

i have a lm35 temperature sensor on it just to see if it will read anything from it and all getting are

11100000 0
11100000 0
11100000 0
11100000 0
11100000 0
11100000 0
11100000 0

OK if you are not prepared to answer questions properly then I guess you are not serious in wanting an answer.
I am off.
Good luck.

i reply with the answer on what I'm hooking up to the adc convertor chip i connected all the spi pins correctly i followed where the wiring is going and connected them to the uno also power as well. and i connected a lm35 temperature sensor to the channel 0 on the ADC converter chip to see if it will read from that and the last post i posted is what i got from the Serial monitor how is that not being serious if i wasn't serious why would i been here then if i didn't need help. makes no sense I'm very serious in needing help I'm not a coder or a programer and trying to get help shows I'm very serious in trying to make this work and need help in here.

maybe i didn't understand your Question if so I'm sorry about that.

also i didn't see the part with the Vref part i connected that to the Aref on the uno board right above the ground side on digital pins area.

Post your program! And use code tags, please.

hello thank you for the reply back code i used came from Arduino Playground - MCP3208

this is the code

 // read the MCP3304 in quad differential mode, non-bit-banging version
#include <Serial.h>
#include <SPI.h>

#include <HardwareSerial.h>

long ticks = 0;

// for Uno
#define SELPIN 10    // chip-select
#define DATAOUT 11   // MOSI 
#define DATAIN 12    // MISO 
#define SPICLOCK 13  // Clock 

// for Mega2560 / Max32
//#define SELPIN 53    // chip-select
//#define DATAOUT 51   // MOSI 
//#define DATAIN 50    // MISO 
//#define SPICLOCK 52  // Clock 

int readvalue; 


void setup(){ 
  //set pin modes 
  pinMode(SELPIN, OUTPUT); 

  // disable device to start with 
  digitalWrite(SELPIN, HIGH); 

  SPI.setClockDivider( SPI_CLOCK_DIV8 ); // slow the SPI bus down
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);    // SPI 0,0 as per MCP330x data sheet 
  SPI.begin();

  Serial.begin(115200); 
}


void loop() {
  long ticks = millis();
  int A = 0, B = 0;

  A = read_adc(1);
  B = read_adc(2);

  // do whatever you want with these readings

  long tcnv = millis() - ticks;

  delay(100 - tcnv);
}

// non-bit-banging version
// channel ranges from 1.. 3 (not zero!)
// maximum clock frequency is 2.1 MHz @ 5V
//
// this is SPI_CLOCK_DIV8
// at DIV64, 512 in 60ms (8.5 ksps)
// at DIV32, 512 in 35ms (14.6 ksps)
// at DIV16, 512 in 22ms (23.3 ksps)
// at DIV8, 512 in 16ms (32 ksps)

int read_adc(int channel){
  int adcvalue = 0;
  int b1 = 0, b2 = 0;
  int sign = 0;

  // command bits for MCP3304
  // 0000 = diff, ch0 = in+, ch1 = in-
  // 0010 = diff, ch2 = in+, ch3 = in-
  // 0100 = diff, ch4 = in+, ch5 = in-

  digitalWrite (SELPIN, LOW); // Select adc

  // first byte
  // first byte will always be B000010xx where xx are the D2 and D1 channel bits  
  byte commandbits = B00001000;
  commandbits |= (channel >> 1);         // high bit of channel

  SPI.transfer(commandbits);       // send out first byte of command bits

  // second byte; Bx0000000; leftmost bit is D0 channel bit
  commandbits = B00000000;
  commandbits |= (channel << 7);        // if D0 is set it will be the leftmost bit now
  b1 = SPI.transfer(commandbits);       // send out second byte of command bits

  // hi byte will have XX0SBBBB
  // set the top 3 don't care bits so it will format nicely
  b1 |= B11100000;
  Serial.print(b1, BIN); Serial.print(" ");
  sign = b1 & B00010000;
  int hi = b1 & B00001111;

  // read low byte
  b2 = SPI.transfer(b2);              // don't care what we send
  Serial.print(b2, BIN); Serial.print("\r\n");
  int lo = b2;
  digitalWrite(SELPIN, HIGH); // turn off device

  int reading = hi * 256 + lo;

  if (sign) {
    reading = (4096 - reading) * -1;
  }

  return (reading);
}

That chip has no internal reference, it has a VREF pin. Therefore whatever is connected to that pin is critical, hence Mike's question which you did not address.

i connected that to the Aref on the uno board

Is AREF on the 328 an output by default in the Arduino setup, I think it is but that should be checked.


Rob