Printing analogue to digital converter (ADC) readings

Intro

Analogue to digital converter (ADC).

These programs utilise an analogue to digital converter (ADC) from Adafruit (the ADS1015, which has a library to use it).

If you want to use a different ADC just follow this guide, import any libraries for your ADC and change the reading method — see also Other ADCs.

Connecting the components together

Connecting the Raspberry Pi to the ADC.
Raspberry Pi to ADC wiring diagram.

First of all the ADC needs to be connected to the Raspberry Pi, it will use a bus called I²C. This is used for communication between simple integrated circuits (ICs; such as the ADC) and computers (the Raspberry Pi).

The ADC requires four plugs to connect it to the Raspberry Pi:

  • two for power, positive: + (normally called Vcc or Vdd), and negative: - (normally called GND)
  • SCL (a clock line, it says when to communicate)
  • SDA (a data line, across which the readings are transfered)

In total: Vdd, GND, SCL and SDA. You can see these printed on the ADC.

The Raspberry Pi has all of these as well. The two on the top left below 3v3 are SDA and SCL.

Connect the plugs as in the adjacent wiring diagram (not the photographs). Then plug the seismometer into the ADC, at A2 and A3. We could use any A0-3, but my programs use A2 and A3.

Using the ADC

Connecting the ADC to the seismometer

Now you can start programming. With the Raspberry Pi booted and ready, you will need to get the libraries for the ADC.

Download the two ‘Adafruit’ files from Github; ignore the two ‘ads...’ files. Then put these in a folder on your Raspberry Pi.

Now you can write python code into a file in the same directory. You can do this from the terminal, using nano/vi/emacs, or using the GUI, using leafpad.

Write the following code into your text editor:

from Adafruit_ADS1x15 import ADS1x15
sps = 16 #samples per second
adc = ADS1x15(ic=0x01) #create class identifing model used
# this looprints all the data appearing on this port
sample = adc.readADCDifferential23(256, sps)*1000

This will print a single number, which is the reading. The program is quite self explanatory; the first line imports the library used, then the next two set it up and the last line prints it to the console.

The last line is the one to change if you are using a different ADC.

Continuous reading

A single reading is not very useful, a sequence of readings is better. This can be achieved by making a loop to read the data input: 

from Adafruit_ADS1x15 import ADS1x15
sps = 16 #samples per second
adc = ADS1x15(ic=0x01) #create class identifing model used
# this looprints all the data appearing on this port
while True:
   sample = adc.readADCDifferential23(256, sps)*1000
   print sample

The first three lines are the same. Then the loop begins after that; it continuously prints readings, so to stop it, use ‘ctrl-z’.

Next step... saving data

Now you can show the seismic activity, but it is not in a very useful format.

The next thing to do is save it in a file — see Saving data with ObsPy & MiniSeed