I need to sample three audio signals, each roughly 34k samples/second, so I want to sample ADC at around 100kHz. I'm using the Due. I started by using the Due's built-in ADC, which seemed like it was fast enough, but because of how the analog inputs are muxed I was getting bad results.
Now I have a MCP3008 ADC chip and am trying to get it working with the Arduino SPI library. I got it working with this library GitHub - nodesign/MCP3008: MCP3008 - Analog to digital converter Arduino library which bit bangs it. I'm under the impression the SPI library would be faster, does this sound right? The issue I'm having is that SPI.transfer() is only returning 1's, not any useful results, and I cannot figure out why.
Here's a bit of my latest code from trying to debug this. I've tried using the Due's extended library, adding delays all over the place, setting the SPI clock as low as I can, etc., but none of it seems to be working. Every time I try all I get back is a lot of 1's and nothing else.
If anyone could help with this I would really appreciate it, I'm out of ideas on this. I'm also open to suggestions on better ways to get fast ADC from multiple (3) sources. Thanks!
pylon:
Remove all the delay()s from the program, you don't need them.
Post schematics or a photograph of how you wired the chip to the Arduino.
And post your complete sketch not just parts of it. Use code tags if you post code!
On the Due the SPI library (better: the hardware) takes care of the SS pin, so you have to program a bit different from an UNO.
I added the delays in an attempt to get any sort of reading, was planning to remove them.
Updated my post with how it's wired.
I tried using the Due's extended library instead of manually controlling the cs pin, so I had SPI.begin(10), SPI.transfer(10, 0x00), etc. Didn't seem to make any difference. The code I posted was just my latest attempt. Let me know if there's any other info that would be helpful!
Just to reiterate, the code above is just an attempt to get a proper ADC reading. I have 1V hooked up to ch0 of the MCP3008, so I'm expecting to get a value of about 307. I'm confident my wiring is okay as it was working with that other library I mentioned.
I'm using the Due, started with its built in ADC but couldn't switch between the inputs fast enough and it was giving bad results at the speeds I needed.
"DUE has better ADC, than you trying to connect" what exactly do you mean by this?
I'm trying to sample three different audio signals, ideally sampling each at about 34kHz, so I want roughly 100k samples/second. I tried using the built in ADC on the Due and I could sample fast enough, but apparently muxing the inputs this quickly was messing up my results. I'm now using the MCP3008. I can get it to work using the library I linked which bit bangs it, but I want to try using the Arduino SPI library which supposedly would be faster. When I try to use the SPI library I can't get any results, SPI.transfer() just returns a bunch of 1's.
"DUE has better ADC, than you trying to connect" what exactly do you mean by this?
I mean, what I sad, DUE has faster ( 1 Mega sample / sec ) and higher resolution (12 bits) ADC than MCP3008. You are making things more complicated, bringing SPI in the picture. All you have to do, just look into the link I posted above, there is an example, where you can easily defined sampling rate and Number of channels. Example is using one input, but as many as 12 may be activated. What next, 100 ksps pretty high rate, where this data supposed to flow?
"DUE has better ADC, than you trying to connect" what exactly do you mean by this?
I mean, what I sad, DUE has faster ( 1 Mega sample / sec ) and higher resolution (12 bits) ADC than MCP3008. You are making things more complicated, bringing SPI in the picture. All you have to do, just look into the link I posted above, there is an example, where you can easily defined sampling rate and Number of channels. Example is using one input, but as many as 12 may be activated. What next, 100 ksps pretty high rate, where this data supposed to flow?
I'll definitely try that, I'd definitely prefer to just use the Arduino if possible. When I was using the Due's ADC weird things were happening. For example if I had:
int val0 = analogRead(0);
int val1 = analogRead(1);
int val2 = analogRead(2);
val2 was getting the value on pin 0. I was using this in my setup:
'REG_ADC_MR = (REG_ADC_MR & 0xFFF0FFFF) | 0x00020000;'
So maybe that wasn't the right way to speed things up...
Fortunately the data doesn't really need to "flow" anywhere, it's just getting thrown out after I check the value. I'm doing something with multilateration, so when I detect that a threshold has been broken for each pin I record the time and stop sampling that pin. It's kinda a crude method but I didn't want to mess with cross-correlation and such, and if I can get the sampling speed I need it should work fine. The frequency I mentioned gives me a precision of about a centimeter which is fine for what I'm doing.
44.6.7
Comparison Window
The ADC Controller features automatic comparison functions. It compares converted values to a
low threshold or a high threshold or both, according to the CMPMODE function chosen in the
Extended Mode Register (ADC_EMR). The comparison can be done on all channels or only on
the channel specified in CMPSEL field of ADC_EMR. To compare all channels the CMP_ALL
parameter of ADC_EMR should be set.
Moreover a filtering option can be set by writing the number of consecutive comparison errors
needed to raise the flag. This number can be written and read in the CMPFILTER field of
ADC_EMR.
The flag can be read on the COMPE bit of the Interrupt Status Register (ADC_ISR) and can trig-
ger an interrupt.
The High Threshold and the Low Threshold can be read/write in the Comparison Window Regis-
ter (ADC_CWR).
Though, I haven't tested this feature yet. Something for you to to read and/ or search if there is already projects exists on-line
If you still want to use the MCP3008, you have to connect it to the SPI bus of the Due. The connections you've made are good for an UNO, but not for a Due. You have to use the pins on the ICSP connector for the SPI bus, pins 11, 12 and 13 won't work.
If you still want to use the MCP3008, you have to connect it to the SPI bus of the Due. The connections you've made are good for an UNO, but not for a Due. You have to use the pins on the ICSP connector for the SPI bus, pins 11, 12 and 13 won't work.
Thanks, I'll try that as well. The SPI reference had a connections table but it was a little ambiguous.