Analog sampling on the Beaglebone Black only reads noise
Question: I want to write a script in python that will do data acquisition with the
beaglebone black. However, I'm only getting noise when I read in values.

I have a circuit set up to feed in a signal (I was running about a .1-10 Hz
12v square pulse), the voltage regulator limits the voltage to 5v, and the
voltage divider drops it down to a safe voltage (~1v) for the analog pin on
the BBB.

Circuit:

![enter image description here](http://i.stack.imgur.com/BvYJv.png)

Then using a python script I wanted to log the values into an array and plot
them once the program finished.

In ipython shell:

    
    
    In [1]: import Adafruit_BBIO.ADC as adc
    In [2]: import time
    In [3]: AIN0 = "P9_39"
    In [4]: T = 0.1                           # or some other value
    In [5]: adc.setup()
    In [6]: a = []
    In [7]: while True:
                a.append(adc.read(AIN0)*1800) # pin value times 1.8 ref voltage
                time.sleep(T)
    

After running for a while, I crash the script and write `a` to a file to be
`scp`ed to my desktop. But when I plot it, it's only noise. I've hooked up on
o-scope between `AIN0` and ground and the wave form is what I expect. I also
set up a potentiometer like so:

![enter image description here](http://i.stack.imgur.com/qH6S1.png)

And I am able to read in the correct values through python. So, I figure it
must be something about doing a continuous sampling that's creating problems.


Answer: If you don't connect the power supply ground to your Beaglebone ground it will
not work, and you should indeed see only noise, since `AIN0` will be sampling
the Beaglebone's ground. You have to connect them in order to `AIN0` see the
signal of interest.

![fixed schematic](http://i.stack.imgur.com/cpi06.png)

See the "missing connection" wire. If you don't have that you have no return
path for the current coming from the power supply (blue arrows), so there
can't be any voltage across the pontentiometer (remember Ohm's law: `voltage =
resistance x current`. If current is zero, the sampled voltage must also be
zero).

As for the script part, you can directly write the sampled data to a file with
this:

    
    
    with open('sampled_data.csv', 'w') as f:
      while True:
        f.write(','.join(str(adc.read(AIN0)*1800)))
        time.sleep(T)
    

When you interrupt the script you'll get the `sample_data.csv` file, with all
values separated by commas (`,`), which is easily importable to a spreadsheet
or other software you use to plot it.

