├── .gitignore ├── ads1256_test.c ├── Makefile ├── wrapper.h ├── setup.py ├── read_volts_example.py ├── read_example.py ├── wrapper.c ├── datalogger_example.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | *.so 3 | -------------------------------------------------------------------------------- /ads1256_test.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiovix/py-ads1256/HEAD/ads1256_test.c -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ads1256.so: ads1256_test.c wrapper.c 2 | python setup.py build_ext --inplace 3 | echo "\n Para testar a lib execute:\n python test.py"; 4 | 5 | -------------------------------------------------------------------------------- /wrapper.h: -------------------------------------------------------------------------------- 1 | long int readChannels(long int *); 2 | long int readChannel(long int); 3 | int adcStart(int argc, char*, char*, char *); 4 | int adcStop(void); 5 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup, Extension 2 | 3 | c_ext = Extension("ads1256", ["wrapper.c", "ads1256_test.c"], libraries = ['bcm2835']) 4 | 5 | setup( 6 | ext_modules=[c_ext], 7 | ) 8 | -------------------------------------------------------------------------------- /read_volts_example.py: -------------------------------------------------------------------------------- 1 | import ads1256 # import this lib 2 | 3 | gain = 1 # ADC's Gain parameter 4 | sps = 25 # ADC's SPS parameter 5 | 6 | # Create the first list. It will receive ADC's absolute values 7 | AllChannelValuesVolts = [0,0,0,0,0,0,0,0] 8 | 9 | # Create the second list. It will received absolute values converted to Volts 10 | AllChannelValues = [0,0,0,0,0,0,0,0] 11 | 12 | # Initialize the ADC using the parameters 13 | ads1256.start(str(gain),str(sps)) 14 | 15 | # Fill the first list with all the ADC's absolute channel values 16 | AllChannelValues = ads1256.read_all_channels() 17 | 18 | for i in range(0, 8): 19 | # Fill the second list with the voltage values 20 | AllChannelValuesVolts[i] = (((AllChannelValues[i] * 100) /167.0)/int(gain))/1000000.0 21 | 22 | 23 | for i in range(0, 8): 24 | # Print all the absolute values 25 | print AllChannelValues[i] 26 | 27 | 28 | # Print a new line 29 | print ("\n"); 30 | 31 | 32 | for i in range(0, 8): 33 | # Print all the Volts values converted from the absolute values 34 | print AllChannelValuesVolts[i] 35 | 36 | # Stop the use of the ADC 37 | ads1256.stop() 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /read_example.py: -------------------------------------------------------------------------------- 1 | import ads1256 2 | import time,random 3 | 4 | # Receive the current time since the epoch (Unix Time). 5 | # It's used like a "anchor", like a point of start, to measure the time since 6 | # this point in the program execution 7 | d0 =time.time() 8 | 9 | # Initializes the ADC using py-ads1256 library function 10 | # First argument: GAIN. The second: SPS 11 | # Possible settings: 12 | # GAIN values: 1, 2, 4, 8, 16, 32, 64 13 | # SPS values: 2d5, 5, 10, 15, 25, 30, 50, 60, 100, 14 | # SPS values: 500, 1000, 2000, 3750, 7500, 15000, 30000 15 | ads1256.start("1","2d5") 16 | 17 | 18 | # Calculates and displays how much time has elapsed from the start of the program 19 | # to the end of executing the initialization function 20 | print str(int((time.time()-d0)*1000))+"mS in initializing ADC\n" 21 | 22 | 23 | # Performs 5 readings of all ADC channels. 24 | print "\nReading all channels with the function ads1256.read_all_channels():" 25 | for i in range(1): 26 | d0 =time.time() 27 | valorTodosCanais = ads1256.read_all_channels() 28 | for x in valorTodosCanais: 29 | print x 30 | print "\n" + str(int((time.time()-d0)*1000))+"mS elapsed in reading 8 channels (" + str(int((time.time()-d0)*1000)/8) + " mS in each one)\n" 31 | 32 | 33 | # Performs the reading of ADC channel 0 34 | print "\nReading only the channel 0 with ads1256.read_channel():" 35 | d0 =time.time() 36 | valorCanal = ads1256.read_channel(0) 37 | print valorCanal 38 | 39 | print "\n" + str(int((time.time()-d0)*1000))+"mS in reading only channel 0\n" 40 | 41 | 42 | ads1256.stop() 43 | -------------------------------------------------------------------------------- /wrapper.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "wrapper.h" 3 | 4 | /* Docstrings */ 5 | static char module_docstring[] = 6 | "Esta biblioteca é um wrapper "; 7 | 8 | /* Available functions */ 9 | static PyObject *adc_read_channel(PyObject *self, PyObject *args); 10 | static PyObject *adc_read_all_channels(PyObject *self, PyObject *args); 11 | static PyObject *adc_start(PyObject *self, PyObject *args); 12 | static PyObject *adc_stop(PyObject *self, PyObject *args); 13 | 14 | /* Module specification */ 15 | static PyMethodDef module_methods[] = { 16 | // {"chi2", chi2_chi2, METH_VARARGS, chi2_docstring}, 17 | {"read_channel", adc_read_channel, METH_VARARGS, {"lê o canal especificado do ads1256"}}, 18 | {"read_all_channels", adc_read_all_channels, METH_VARARGS, {"lê todos os 8 canais do ads1256"}}, 19 | {"start", adc_start, METH_VARARGS, {"inicia e configura o ads1256"}}, 20 | {"stop", adc_stop, 0, {"termina e fecha o ads1256"}}, 21 | {NULL, NULL, 0, NULL} 22 | }; 23 | 24 | /* Initialize the module */ 25 | PyMODINIT_FUNC initads1256(void) 26 | { 27 | PyObject *m = Py_InitModule3("ads1256", module_methods, module_docstring); 28 | if (m == NULL) 29 | return; 30 | 31 | } 32 | static PyObject *adc_start(PyObject *self, PyObject *args) 33 | { 34 | 35 | char * ganho, *sps; 36 | PyObject *yerr_obj; 37 | double v[8]; 38 | int value ; 39 | 40 | 41 | /* Parse the input tuple */ 42 | if (!PyArg_ParseTuple(args, "ss", &ganho, &sps,&yerr_obj)) 43 | return NULL; 44 | 45 | /* execute the code */ 46 | value = adcStart(4,"0",ganho,sps); 47 | 48 | /* Build the output tuple */ 49 | PyObject *ret = Py_BuildValue("i",value); 50 | return ret; 51 | } 52 | 53 | static PyObject *adc_read_channel(PyObject *self, PyObject *args) 54 | { 55 | 56 | int ch; 57 | long int retorno; 58 | PyObject *yerr_obj; 59 | 60 | 61 | 62 | /* Parse the input tuple */ 63 | if (!PyArg_ParseTuple(args, "i", &ch,&yerr_obj)) 64 | return NULL; 65 | 66 | 67 | /* execute the code */ 68 | retorno = readChannel(ch); 69 | return Py_BuildValue("l",retorno); 70 | } 71 | 72 | 73 | static PyObject *adc_read_all_channels(PyObject *self, PyObject *args) 74 | { 75 | PyObject *yerr_obj; 76 | long int v[8]; 77 | 78 | 79 | /* execute the code */ 80 | readChannels(v); 81 | 82 | /* Build the output tuple */ 83 | PyObject *ret = Py_BuildValue("[l,l,l,l,l,l,l,l]", 84 | v[0], 85 | v[1], 86 | v[2], 87 | v[3], 88 | v[4], 89 | v[5], 90 | v[6], 91 | v[7] 92 | ); 93 | return ret; 94 | } 95 | 96 | static PyObject *adc_stop(PyObject *self, PyObject *args) 97 | { 98 | /* execute the code */ 99 | int value = adcStop(); 100 | 101 | /* Build the output tuple */ 102 | PyObject *ret = Py_BuildValue("i",value); 103 | return ret; 104 | } 105 | 106 | 107 | -------------------------------------------------------------------------------- /datalogger_example.py: -------------------------------------------------------------------------------- 1 | import ads1256 # import the ads1256 lib 2 | import time # import the time lib 3 | from datetime import datetime # import the datetime lib 4 | 5 | #Python example to use the ADS1256 as a Voltage Data Logger. 6 | 7 | gain = 1 # ADC's Gain parameter. Possible values: 1, 2, 4, 8, 16, 32, 64 8 | sps = "25" # ADC's SPS parameter. Possible values: 2d5, 5, 10, 15, 25, 30, 50, 60, 100, 500, 1000, 2000, 3750, 7500, 15000, 30000 9 | 10 | chv = [0,0,0,0,0,0,0,0] # Create the first list. It will receive ADC's absolute values 11 | ch = [0,0,0,0,0,0,0,0] # Create the second list. It will received absolute values converted to Volts 12 | 13 | ads1256.start(str(gain),str(sps)) # Initialize the ADC using the parameters 14 | 15 | 16 | # Define the CSV filename based on the date and time 17 | timest = str(datetime.now()) 18 | timest = timest.replace(":", "_") 19 | timest = timest.replace(" ", "-") 20 | timest = timest.replace(".", "-") 21 | timest = timest[:-7] 22 | filename = "ads1256-log-"+str(timest)+".csv" 23 | 24 | 25 | header =[] 26 | header.extend(["Row_id","Channel 0","Channel 1","Channel 2","Channel 3","Channel 4","Channel 5","Channel 6","Channel 7","Timestamp"]) # Define a CSV header 27 | 28 | 29 | with open(filename,"w") as file: # With the opened file... 30 | file.write(",".join(str(value) for value in header)+ "\n") # Write each value from the header 31 | 32 | 33 | i = 0 34 | ch= [0,0,0,0,0,0,0,0] 35 | 36 | print ("ADS1256 Voltage Data Logger" + "\n" + "GAIN: " + str(gain) + " SPS: " + str(sps) + " FILENAME: " + filename + "\n") 37 | 38 | 39 | with open(filename,"a") as file: # Open the file into Append mode (enter data without erase previous data) 40 | 41 | while True: # Forever loop 42 | 43 | 44 | 45 | 46 | row_id = i 47 | 48 | ch = ads1256.read_all_channels() # Fill the first list with all the ADC's absolute channel values 49 | 50 | for x in range(0, 8): 51 | chv[x] = (((ch[x] * 100) /167.0)/int(gain))/1000000.0 # Fill the second list with the voltage values 52 | 53 | print str(chv[0]) + " " + str(chv[1]) + " " + str(chv[2]) + " " + str(chv[3]) + " " + str(chv[4]) + " " + str(chv[5]) + " " + str(chv[6])+ " " + str(chv[7]) 54 | 55 | # Registra efetivamente no arquivo .CSV (no padrao americano) 56 | file.write(str(row_id) 57 | + ", " + str(chv[0]) + ", " + str(chv[1]) + ", " + str(chv[2]) 58 | + ", " + str(chv[3]) + ", " + str(chv[4]) + ", " + str(chv[5]) 59 | + ", " + str(chv[6]) + ", " + str(chv[7]) + ", " + str(datetime.now()) + "\r\n") 60 | 61 | time.sleep(0.1) # That's the time the program will wait until the next read. To use higher SPS, use a lower sleep value, of miliseconds 62 | i = i + 1 63 | 64 | 65 | # Stop the use of the ADC 66 | ads1256.stop() 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # py-ads1256 2 | Python Library with wrapers to read 8 channels from the Texas Instruments ADS1256 ADC. 3 | It does make use of the original WaveShare's C library for the [High-Precision_AD/DA_Board 24 Bits] (http://www.waveshare.com/wiki/High-Precision_AD/DA_Board) 4 | 5 | ## Installation 6 | 7 | To install the library, first install it's principal dependency: the SoC bcm2835 library: 8 | 9 | sudo apt-get install automake libtool 10 | wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.50.tar.gz 11 | tar zxvf bcm2835-1.50.tar.gz 12 | cd bcm2835-1.50 13 | autoreconf -vfi 14 | ./configure 15 | make 16 | sudo make check 17 | sudo make install 18 | 19 | 20 | 21 | After this, run the following commands on a Raspberry Pi or other Debian-based OS system: 22 | 23 | sudo apt-get install git build-essential python-dev 24 | cd ~ 25 | git clone https://github.com/fabiovix/py-ads1256.git 26 | cd py-ads1256 27 | sudo python setup.py install 28 | 29 | 30 | ## Testing 31 | 32 | Please run one of these to test 33 | 34 | python read_example.py 35 | python read_volts_example.py 36 | python datalogger_example.py 37 | 38 | 39 | 40 | ## Learn by example 1: reading a single channel's absolute value 41 | 42 | import ads1256 # import this lib 43 | ads1256.start(str(1),"25") # initialize the ADC using 25 SPS with GAIN of 1x 44 | ChannelValue = ads1256.read_channel(0) # read the value from ADC channel 0 45 | print ChannelValue # print the value from the variable 46 | ads1256.stop() # stop the use of the ADC 47 | 48 | 49 | 50 | ## Learn by example 2: reading the absolute values from all the channels at once 51 | 52 | import ads1256 # import this lib 53 | ads1256.start(str(1),"25") # initialize the ADC using 25 SPS with GAIN of 1x 54 | AllChannelValues = ads1256.read_all_channels() # create a list of 8 elements: one for each ADC channel 55 | for x in AllChannelValues: # for each element in the list... 56 | print x # ...print it 57 | ads1256.stop() # stop the use of the ADC 58 | 59 | 60 | 61 | 62 | ## Learn by example 3: reading all the channels in absolute values and in voltage values 63 | 64 | import ads1256 # import this lib 65 | 66 | gain = 1 # ADC's Gain parameter 67 | sps = 25 # ADC's SPS parameter 68 | 69 | AllChannelValuesVolts = [0,0,0,0,0,0,0,0] # Create the first list. It will receive ADC's absolute values 70 | AllChannelValues = [0,0,0,0,0,0,0,0] # Create the second list. It will received absolute values converted to Volts 71 | 72 | ads1256.start(str(gain),str(sps)) # Initialize the ADC using the parameters 73 | AllChannelValues = ads1256.read_all_channels() # Fill the first list with all the ADC's absolute channel values 74 | 75 | for i in range(0, 8): 76 | AllChannelValuesVolts[i] = (((AllChannelValues[i] * 100) /167.0)/int(gain))/1000000.0 # Fill the second list with the voltage values 77 | 78 | for i in range(0, 8): 79 | print AllChannelValues[i] # Print all the absolute values 80 | 81 | print ("\n"); # Print a new line 82 | 83 | for i in range(0, 8): 84 | print AllChannelValuesVolts[i] # Print all the Volts values converted from the absolute values 85 | 86 | ads1256.stop() # Stop the use of the ADC 87 | 88 | 89 | 90 | 91 | ## Explaining the arguments 92 | 93 | The "ads1256.start()" function take two arguments: the ADC gain and the ADC SPS. 94 | 95 | 96 | ADC Gain is one of the following 97 | 98 | 1, 2, 4, 8, 16, 32, 64 99 | 100 | 101 | 102 | SPS (Samples per Second) is one of the following 103 | 104 | 2d5, 5, 10, 15, 25, 30, 50, 60, 100, 500, 1000, 2000, 3750, 7500, 15000, 30000 105 | 106 | The 2d5 SPS equals to 2.5 (it's a nomenclature issue from the original C code. It should by passed this way in the Python) 107 | 108 | 109 | 110 | 111 | ## A Voltage Data Logger 112 | 113 | I've included a example to use the ads1256 as a Voltage Data Logger. 114 | It keeps reading all the ads1256 channels in absolute and voltage values and saving to a CSV file until a break from the user 115 | To test it, run the following: 116 | 117 | python datalogger_example.py 118 | 119 | 120 | 121 | 122 | 123 | --------------------------------------------------------------------------------