Uncategorized

MCP3008 Sample Rate Notes

0

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!

 New contributor
  • 1
    What 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
  • 1
    I 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

0

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.

MCP300x Sample rate

/ to continue,

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: