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() {
Serial.begin(9600);
// set the directions on the pins
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
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--){
if((ADC_command_byte&1<<i)>>i){
digitalWrite(DATAOUT,HIGH);
}
else
{
digitalWrite(DATAOUT,LOW);
}
//cycle clock to prepare for next bit
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);
}
}
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 = ");
Serial.println(adcvalue,DEC);
}
int ADCread()
{
int numberOfDataBits = 12;
int adcvalue = 0;
Sendcommandbyte();
//read bits from adc
for (int i = numberOfDataBits - 1; i>=0; i--){
adcvalue += digitalRead(DATAIN) << i;
//cycle clock to prepare for next bit
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);
}
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?
Thanks
wade