I am trying to push the MCP3008 to its limits and sample as many samples as possible from a single channel per second. I am also not trying to have to write any C or assembly code, and keep it all as much as possible in python.
My approach so far has been rather naive, I imported python libraries which instantiate SPI and MCP3008 classes, so my python can read from the MCP3008. All I did was call the function set_clock_hz
to the max when I instantiated the class. The idea was that I can max out the clock on the MCP and sample as high a frequency as possible
Below is my code.
# Simple example of reading the MCP3008 analog input channels and printing
# them all out.
import time
# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
import csv
# Software SPI configuration:
#CLK = 18
#MISO = 23
#MOSI = 24
#CS = 25
#mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)
# Hardware SPI configuration:
SPI_PORT = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, 50000)) #50000 max speed in hz
print('Reading MCP3008 values, press Ctrl-C to quit...')
# Print nice channel column headers.
# Main program loop.
#Run for one second
t_end = time.time() + 1
arr = []
while time.time() < t_end:
# The read_adc function will get the value of the specified channel (0-7).
value = mcp.read_adc(7)
arr.append(value)
#Write results
with open('highFreqMeas.csv', "w") as output:
writer = csv.writer(output, delimiter=",", lineterminator='\n')
for i in arr:
writer.writerow([i])
#Read results to see how many samples in second
with open('highFreqMeas.csv', "r") as infile:
reader = csv.reader(infile, delimiter=",", lineterminator='\n')
reader = list(reader)
print len(reader)