← Back to questions

Q: ADC

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__'. 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 def read(adc_channel=0, spi_channel=0): conn = spidev.SpiDev(0, spi_channel) conn.max_speed_hz = 1200000 # 1.2 MHz cmd = 128 if adc_channel: cmd += 32 reply_bytes = conn.xfer2([cmd, 0]) reply_bitstring = ''.join(bitstring(n) for n in reply_bytes) reply = reply_bitstring[5:15] return int(reply, 2) / 2**10 while True: if __name__ == '__main__': print read() The signal will record for about 5 seconds and then produce an error that there are too many open files. I don't understand the error, or what exactly the code is doing. Can someone help me understand what is happening here? I realize that this is not a small question and would appreciate any help. Thanks in advance.
jiblankman Asked by: jiblankman

Answers

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

def read(adc_channel=0, spi_channel=0):
  conn = spidev.SpiDev(0, spi_channel)
  conn.max_speed_hz = 1200000 # 1.2 MHz
  cmd = 128
  if adc_channel:
    cmd += 32
  reply_bytes = conn.xfer2([cmd, 0])
  reply_bitstring = ''.join(bitstring(n) for n in reply_bytes)
  reply = reply_bitstring[5:15]
  return int(reply, 2) / 2**10

while True:
  if __name__ == '__main__':
    print read()


The signal will record for about 5 seconds and then produce an error that there are too many open files. I don't understand the error, or what exactly the code is doing. Can someone help me understand what is happening here? I realize that this is not a small question and would appreciate any help. Thanks in advance.

jiblankman Answer by: jiblankman
Votes: 0