4

I am trying to sample an analog voltage signal on the Raspberry Pi using an MCP3004 ADC. I am using the spidev package and the basic code is something like:

import spidev
import time
list = []
timeout = time.time() + 5
spi = spidev.SpiDev()
spi.open(0,0)
def getAdc(channel)
    r = spixfer([1, (8+channel) << 4, 0])
    adcOut = ((r[1]&3) << 8 + r[2]
    voltage = (adcOut * 3.3)/ (1023)
    list.append(voltage)
    print(voltage)
while time.time()<timout
    getAdc(0)
print(len(list))*

I am running the code for 5 seconds and it gives me around 1000 samples (which is sampling at 200 Hz). Is there a way I can increase this? I need at least 1 kHz.

Ghanima
  • 15,958
  • 17
  • 65
  • 125

1 Answers1

1

There is nothing obviously inefficient in your Python.

Perhaps you need to go to C. The standard C library SPI calls should approach 20 thousand samples per second.

joan
  • 71,852
  • 5
  • 76
  • 108