Using the ADS131M0x Analog to Digital Converter¶
This tutorial demonstrates how to use the ADS131M0x analog to digital converter with the Open Source Leg platform. This example shows how to:
- Initialize and configure an ADS131M0x ADC
- Read voltage data
Hardware Setup¶
- Connect the ADS131M0x ADC to your Raspberry Pi's SPI pins
- Make sure the ADC is powered on and the SPI pins are connected correctly
Code Structure¶
The tutorial script is organized as follows:
1. Initialization¶
from opensourceleg.logging.logger import Logger
from opensourceleg.sensors.adc import ADS131M0x
from opensourceleg.utilities import SoftRealtimeLoop
FREQUENCY = 500
DT = 1 / FREQUENCY
if __name__ == "__main__":
adc_logger = Logger(
log_path="./logs",
file_name="reading_adc_data",
)
clock = SoftRealtimeLoop(dt=DT)
adc = ADS131M0x(
tag="ADS131M0x",
spi_bus=0,
spi_cs=0,
data_rate=FREQUENCY,
clock_freq=8192000,
num_channels=6,
gains=[1] * 6,
voltage_reference=1.2,
gain_error=[0] * 6,
offline=False,
)
adc_logger.track_function(lambda: adc.data, "Ch Voltages")
This section:
- Creates a data logger for recording measurements
- Sets up a real-time loop for consistent timing
- Initializes the ADS131M0x ADC with specified parameters
- Gets the latest ADC data in millivolts
2. Main Loop¶
with adc:
adc.calibrate()
for t in clock:
adc.update()
adc_logger.info(f"Time: {t}; Ch Voltages = {adc.data}")
adc_logger.update()
The main loop:
- Updates the ADC to get the latest reading
- Logs the time and current voltage reading
ADC Parameters¶
When initializing the ADS131M0x ADC, several parameters can be configured:
adc = ADS131M0x(
tag="ADS131M0x",
spi_bus=0,
spi_cs=0,
data_rate=FREQUENCY,
clock_freq=8192000,
num_channels=6,
gains=[1] * 6,
voltage_reference=1.2,
gain_error=[0] * 6,
offline=False,
Parameter Details¶
-
tag (str):
- Unique identifier for the ADC instance
- Useful when using multiple ADCs
- Default is "ADS131M0x"
-
spi_bus (int):
- SPI bus number
- Default bus on Raspberry Pi is typically
0
-
spi_cs (int):
- SPI chip select line
- Default is
0
-
data_rate (int):
- Sampling rate in Hz
- Default is 500 Hz
-
clock_freq (int):
- SPI clock frequency in Hz
- Default is 8192000 Hz.
-
num_channels (int):
- Number of ADC channels
- Default is 6
-
gains (List[int]):
- Programmable gain values for each channel
- Default is [1] * num_channels.
-
voltage_reference (float):
- ADC reference voltage in volts
- Default reference for ADS131M0x ADC is 1.2 V and should not be changed if using this ADC
-
gain_error (List[int]):
- Gain error correction values for each channel
- Default is None.
-
offline (bool):
- Enables and disables offline mode for ADS131M0x ADC
- If
True
, the ADC operates in offline mode, meaning that the ADC hardware does not have to be connected in order for your script to run - Default is
False
Available Properties¶
The ADS131M0x ADC provides the following properties:
-
is_streaming
- Checks if ADC is streaming data
- Returns
True
if streaming,False
otherwise
-
gain:
- Gets the programmable gain values for each channel
- Returns array of gain values (np.ndarray)
-
data
- Gets the latest ADC data in millivolts
- Returns array of voltage readings for each channel (np.ndarray)
-
data_counts
- Gets the latest ADC data in raw counts
- Returns array of raw ADC counts for each channel (np.ndarray)
-
num_channels
- Get the number of ADC channels.
- Returns # of channels (int)
Running the Example¶
-
Navigate to the tutorial directory:
-
Run the script:
-
Expected behavior:
- ADC begins reading voltage values in mV continuously at sampling rate (default is 500 Hz)
- Data is logged to
./logs/reading_adc_data.csv
Common Issues¶
If you have any questions or need further assistance, please post on the Open Source Leg community forum.