I'm making a midi controller with the Arduino uno. It contains approximately 50 analog inputs with potentiometers. I have enough MCP3008 adc's here but I have no idea how I connect 2 or more adc's to the Arduino so that I can read 50. How do you stack adc's?
but I have no idea how I connect 2 or more adc's to the Arduino so that I can read 50.
These are SPI devices, so you connect them all to the SPI bus and give each chip its own CE line. Just hold the CE line of the chip you want to address low and them access them as normal.
However that is a bit of an unconventional approach most people go for analogue multiplexers for a lower chip count. You only need four 4067 chips for 64 analogue inputs. As opposed to seven chips the way you want to do things.
You connect all the SCK lines in parallel, all the Data out lines in parallel to MOSI, all the data in lines in parallel to MISO, and use 7 individual chip select lines to address each one individually.
Then you cycle thru the 8 addresses on each chip to read the data from its 8 inputs one at a time.
SPI reads will be much faster than using the 100uS analog read of external analog mux into internal analog mux into internal ADC converter.
For example, here is part of a sketch that used one MCP3008 to cycle thru 8 channels. Add another loop around this one to cycle thru 7 chip selects.
You can leave out the 5mS delay altogether so it can read all 50 inputs quicker.
Add the outer loop, time it with serial prints micros() to see how fast it reads. I was running it at 250K for serial. Midi needs 31250?
/*
Every 5mS, read a Pot via SPI, send it via RS232 and send the state of the 3 switches.
The rest of the time, be watching for an encoder clock pulse, if a pulse occurs then capture that in the ISR with the direction, and at the top of loop set the bits/direction for any of the four that occurred and RS232 message.
On the receive side, set the output bits accordingly & clear.
Change the bias resistor on the LS7184s to shorten up the pulses that create the interrupt.
Every 5mS, there could be some delay for the SPI read of a DAC channel, the rest of the time should be available for the INTs.
*/
void loop(){
// Elapsed time check for ADC and Switch reading
if (millis()>=previousMillis){
previousMillis = previousMillis + 5; // set for next 5 mS interval
// read ADC with 3 byte transfer
PORTB = PORTB & B11111011; // ADC_SS, LOW
// ADCaddress = 0x0600, 640, 680, 6C0, 700, 740, 780, 7C0
dummyADC = SPI.transfer (highByte(ADCaddress)); // 0x06, 0x07 out, read in dummy data
highADC = SPI.transfer (lowByte(ADCaddress)); // 0x00, 0x40, 0x80, 0xC0, read in upper 4 bits
lowADC = SPI.transfer (0); // dummy 0 byte out , read in lower 8 bits
PORTB = PORTB | B00000100; // ADC_SS, HIGH
// send the data out - you need to put some code here for that.
// prep address_count for next pass. Used to create ADC select, DAC select
address_count = address_count +1; // 0 to 7, pass to Rx for DAC sync
if (address_count == 8){
address_count = 0;
} // reset if hit all 8
ADCaddress = baseADCaddress + (address_count * 0x40); // update for next ADC pass
} // end 5mS test
} // end void loop
If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. When you post links please always use the chain links icon on the toolbar to make them clickable.