← Back to questions

Edit...Let me try this again with some formatting

I have been trying to use an ADC to monitor an analog signal and need some help understanding the code. I am reasonably new with Python, SPI and electronics, so please use small words. I have been following the tutorial at http://raspberry.io/projects/view/reading-from-a-mcp3002-analog-to-digital-converter/ and do not understand what is happening with the code.

I am purely a hobbyist and new to Python and electronics generally. I have been doing a lot of reading about Python and working tutorials but this is the first time that I am seeing "__future__" and if __name__=='__main__'. Among other things, the double underscores make no sense to me.

I am trying to record the analog signal as a function of time from a separate circuit. I know that the voltage is normally high (about 3.0 v) and dropping when the desired signal is generated (to about 0.02 v). The length of time for this drop is variable depending on what caused the signal and can last for anywhere from about 10 to about 50 msec. I want to record the signal as quickly as possible to develop a software filter to remove false signals, but I cannot figure out the proper code to analyze. Here is the code from the tutorial that I modified:

from __future__ import division
import spidev
def bitstring(n):
s = bin(n)[2:]
return '0'*(8-len(s)) + s

conn = spidev.SpiDev(0, spi_channel)
conn.max_speed_hz = 1200000 # 1.2 MHz
cmd = 128
cmd += 32
reply_bytes = conn.xfer2([cmd, 0])
reply_bitstring = ''.join(bitstring(n) for n in reply_bytes) Answer by: jiblankman