-
Notifications
You must be signed in to change notification settings - Fork 1
/
ad-example.py
119 lines (98 loc) · 4.33 KB
/
ad-example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""PiPyADC: Example file for class ADS1256 in module pipyadc:
ADS1256 cycling through eight input channels.
Hardware: Waveshare ADS1256 board interfaced to the Raspberry Pi 3
Ulrich Lukas 2017-03-10
"""
import sys
import os
import pigpio as io
from ADS1256_definitions import *
from ADS1256_PiGPIO import ADS1256
# Change this to the local DNS name of your Pi (often raspberrypi.local, if you have changed it) or
# make it blank to connect to localhost.
PI_HOST = 'klabs.local'
# if not os.path.exists("/dev/spidev0.1"):
# raise IOError("Error: No SPI device. Check settings in /boot/config.txt")
### START EXAMPLE ###
################################################################################
### STEP 0: CONFIGURE CHANNELS AND USE DEFAULT OPTIONS FROM CONFIG FILE: ###
#
# For channel code values (bitmask) definitions, see ADS1256_definitions.py.
# The values representing the negative and positive input pins connected to
# the ADS1256 hardware multiplexer must be bitwise OR-ed to form eight-bit
# values, which will later be sent to the ADS1256 MUX register. The register
# can be explicitly read and set via ADS1256.mux property, but here we define
# a list of differential channels to be input to the ADS1256.read_sequence()
# method which reads all of them one after another.
#
# ==> Each channel in this context represents a differential pair of physical
# input pins of the ADS1256 input multiplexer.
#
# ==> For single-ended measurements, simply select AINCOM as the negative input.
#
# AINCOM does not have to be connected to AGND (0V), but it is if the jumper
# on the Waveshare board is set.
#
# Input pin for the potentiometer on the Waveshare Precision ADC board:
POTI = POS_AIN0|NEG_AINCOM
# Light dependant resistor of the same board:
LDR = POS_AIN1|NEG_AINCOM
# The other external input screw terminals of the Waveshare board:
EXT2, EXT3, EXT4 = POS_AIN2|NEG_AINCOM, POS_AIN3|NEG_AINCOM, POS_AIN4|NEG_AINCOM
EXT5, EXT6, EXT7 = POS_AIN5|NEG_AINCOM, POS_AIN6|NEG_AINCOM, POS_AIN7|NEG_AINCOM
# You can connect any pin as well to the positive as to the negative ADC input.
# The following reads the voltage of the potentiometer with negative polarity.
# The ADC reading should be identical to that of the POTI channel, but negative.
POTI_INVERTED = POS_AINCOM|NEG_AIN0
# For fun, connect both ADC inputs to the same physical input pin.
# The ADC should always read a value close to zero for this.
SHORT_CIRCUIT = POS_AIN0|NEG_AIN0
# Specify here an arbitrary length list (tuple) of arbitrary input channel pair
# eight-bit code values to scan sequentially from index 0 to last.
# Eight channels fit on the screen nicely for this example..
CH_SEQUENCE = (POTI, LDR, EXT2, EXT3, EXT4, EXT7, POTI_INVERTED, SHORT_CIRCUIT)
################################################################################
def do_measurement():
### STEP 1: Initialise ADC object using default configuration:
# (Note1: See ADS1256_default_config.py, see ADS1256 datasheet)
# (Note2: Input buffer on means limited voltage range 0V...3V for 5V supply)
ads = ADS1256(pi=io.pi(PI_HOST))
### STEP 2: Gain and offset self-calibration:
ads.cal_self()
while True:
### STEP 3: Get data:
raw_channels = ads.read_sequence(CH_SEQUENCE)
voltages = [i * ads.v_per_digit for i in raw_channels]
### STEP 4: DONE. Have fun!
nice_output(raw_channels, voltages)
### END EXAMPLE ###
#############################################################################
# Format nice looking text output:
def nice_output(digits, volts):
sys.stdout.write(
"\0337" # Store cursor position
+
"""
These are the raw sample values for the channels:
Poti_CH0, LDR_CH1, AIN2, AIN3, AIN4, AIN7, Poti NEG, Short 0V
"""
+ ", ".join(["{: 8d}".format(i) for i in digits])
+
"""
These are the sample values converted to voltage in V for the channels:
Poti_CH0, LDR_CH1, AIN2, AIN3, AIN4, AIN7, Poti NEG, Short 0V
"""
+ ", ".join(["{: 8.3f}".format(i) for i in volts])
+ "\n\033[J\0338" # Restore cursor position etc.
)
# Start data acquisition
try:
print("\033[2J\033[H") # Clear screen
print(__doc__)
print("\nPress CTRL-C to exit.")
do_measurement()
except (KeyboardInterrupt):
print("\n"*8 + "User exit.\n")
sys.exit(0)