Whenever I take data from the MCP3008, my sample rate keeps changing. Sometimes it’s 24ksps, sometimes 200sps. I want to keep this fixed. Python Code for SPI communication on raspberry pi:
import time
import sys
import spidev
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz=(3600000)
def readadc(adcnum):
if ((adcnum > 7) or (adcnum < 0)):
return -1
r = spi.xfer2([1, (8+adcnum) <<4, 0])
adcout = ((r[1]&3) << 8) +r[2]
return adcout
ch0 = 0
seconds = time.time()
while (time.time() < seconds+1):
pc_value = readadc(ch0)
print pc_value
output_file.write("%s \n" %(pc_value))
Thanks for your help!
-
1What does “a sampling rate of 1KHz” mean? You need to talk in terms of KSPS (thousands of samples per second) and the max for the MCP3008 is 200KSPS according to MCP3008 datasheet. Also what does this have to do with a Raspberry Pi. – Dougie Apr 4 at 14:05
-
1I think raspberrypi.org/forums is the better place to ask. This question has been asked and answered many times. – joan Apr 4 at 14:11
-
@Dougie, I’ve edited the question. Is it clear now? The max is 200ksps, but I want it at a fixed value of 1ksps. Any insight? – Supriya Asutkar Apr 4 at 14:18
Question
Can I fix the MCP3008 sample rate?
Short Answer
Yes, you can. You make one conversion by one SPI writing/reading. In other words, more SPI write/read means more frequent samples/conversions.
Long Answer
I would suggest you skim through the datasheet quickly once, and read very slowly Section 5.0, Fig 5.1, and 5.2 a couple of times. I have made a summary below to refresh your memory.
You stare at my summary for a couple of minutes, then I will explain.
/ to continue,
Categories: Uncategorized

