Problem with a MAX 1272 12-bit ADC

Needing more resolution than the onboard ADC could provide I ordered a couple of ADC chips Max 1272 12 bit SPI. They interfaced to the NANO with one exception they values output do not follow the transfer function from the data sheet.

Here is the code:

#define DATAOUT 11//MOSI
#define DATAIN  12//MISO 
#define SPICLOCK  13//sck
#define SLAVESELECT 10//ss

byte ADC_command_byte = B10010001;

void setup() {
  // set the directions on the pins
  pinMode(DATAIN, INPUT);

  digitalWrite(SLAVESELECT,HIGH); // Disable the device


void Sendcommandbyte(){
  int numberOfCommandBits = 8;
  int numberOfDataBits = 12;

  digitalWrite(SLAVESELECT, LOW); // Enable the device

 for (int i = numberOfCommandBits - 1; i>=0; i--){

    //cycle clock to prepare for next bit

void loop() {
  int adcvalue; // Tried declaring as a long and the read function and that did not help.

  adcvalue = ADCread();
  Serial.print("the value read from the ADC = ");

int ADCread()
  int numberOfDataBits = 12;
  int adcvalue = 0;
  //read bits from adc
  for (int i = numberOfDataBits - 1; i>=0; i--){
    adcvalue += digitalRead(DATAIN) << i;
    //cycle clock to prepare for next bit
  digitalWrite(SLAVESELECT, HIGH); //turn off device
  return adcvalue;

The transfer function is voltage = (adcvalue/4096) * Fullscale Voltage; where adcvalue is the 12 bit result of reading the 1272 and the Fullscale Voltage for the chosen range is 1.22 x Vref, and I have applied 5 volts to the Vref pin on the 1272.

Here is a table of the voltage applied versus the 12-bit adc value it is nicely linear but does not follow the transfer function.

Voltage ADC Reading
0.21 77
1.34 470
2.12 746
3.24 1131
4.36 1490
5 1682

Does anyone see a problem with the code that would lead to this?



See nothing strange in the code (but its getting late here :slight_smile:

if ((ADC_command_byte&1<<i)>>i)

could be easier

if (ADC_command & (1 << i)) // all values <> 0 are true.

Do you have the URL of the datasheet ?

I wonder if you are having problems because you are exceeding the maximum allowable reference input voltage (4.18V). See the last line of page 3 of the datasheet.

-- The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons

As recommended by RuggedCircuits I turned the reference voltage down popped in a new ADC and all is good. For once is was not my code but my failure to read the manual. Also thanks robtillaart for the help on cleaning up the code that helps, C and its variants are not my first language, I am kind of a dinosaur as my first language was FORTRAN.



I am kind of a dinosaur as my first language was FORTRAN.

Respect ! Really, you must have learned programming by paper and pencil without all these fora and internet. Do you remember your first program?