0

I am trying to use MCP3008 for sampling. I want obtain 8000 samples/second, but i really can't obtain much than 1000 samples/second. This is my code:

    spi = busio.SPI(clock = board.SCK, MISO=board.MISO, MOSI=board.MOSI)
    cs = digitalio.DigitalInOut(board.D5)
    mcp = MCP.MCP3008(spi,cs)
    
    def myadc():
        channel = AnalogIn(mcp, MCP.P0)
        es.append(channel.value)
    ok = 1
    k = 0
    try:
        while ok:
            myadc()
            time.sleep(0.000125) # this is the time for 8kHz sampling frequency

May be a problem with the SPI clock of Raspberry Pi? I have tried a lot, but nothing...

0andriy
  • 4,183
  • 1
  • 24
  • 37
Mihai
  • 3
  • 1
  • In general, using `time.sleep(x)` won't give you very reliable sampling rates as your period is subject to variation depending on CPU load. I'm not an expert with the MCP, SPI packages, but it looks like it should definitely be possible to achieve 8kS/s with the MCP3008. See here: https://raspberrypi.stackexchange.com/questions/104742/how-to-increase-sampling-rate-on-mcp3008-in-python – tdpu Mar 23 '22 at 04:41
  • And what prevents you using the in-kernel driver https://elixir.bootlin.com/linux/latest/source/drivers/iio/adc/mcp320x.c with `libiio`? – 0andriy Apr 17 '22 at 07:11

0 Answers0