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&3) << 8) +r 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!
Can I fix the MCP3008 sample rate?
Yes, you can. You make one conversion by one SPI writing/reading. In other words, more SPI write/read means more frequent samples/conversions.
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,