Undalogic logoUndalogic
Loading Light/Dark Toggle

Documentation

Python Interface

Using the Python library to control and automate the miniSMU MS01

The miniSMU MS01 can be controlled programmatically using the official Python library, minismu_py. This interface provides a convenient way to automate measurements, perform complex test sequences, and integrate the miniSMU into your custom applications and research workflows.

Installation

Install the minismu_py library directly from PyPI using pip:

pip install minismu_py

The library has minimal dependencies and requires:

  • Python 3.6 or higher
  • The pyserial package for USB connections
  • The tqdm package (optional, used for progress bars in examples)

Getting Started

Basic Connection

To get started with the Python interface, you'll first need to establish a connection to your miniSMU MS01:

from minismu_py import SMU, ConnectionType

# For USB connection
smu = SMU(ConnectionType.USB, port="/dev/ttyACM0")  # Linux/Mac
# or
smu = SMU(ConnectionType.USB, port="COM3")  # Windows

# For WiFi connection
smu = SMU(ConnectionType.NETWORK, host="192.168.1.100", tcp_port=3333)

# Verify connection
print("Device Info:", smu.get_identity())

Using Context Manager

The SMU class supports Python's context manager protocol, which automatically handles resource cleanup:

from minismu_py import SMU, ConnectionType

# Using context manager ensures proper cleanup
with SMU(ConnectionType.USB, port="COM3") as smu:
    # Work with the SMU
    print("Device Info:", smu.get_identity())
    
    # No need to explicitly close the connection
    # It will be closed automatically when exiting the 'with' block

Basic Measurement Example

Here's a simple example that configures a channel, sets a voltage, and takes measurements:

from minismu_py import SMU, ConnectionType
import time

with SMU(ConnectionType.USB, port="COM3") as smu:
    # Configure channel 1
    smu.set_mode(1, "FVMI")  # Force Voltage, Measure Current mode
    smu.set_voltage_range(1, "AUTO")
    smu.enable_channel(1)
    
    # Set voltage to 3.3V
    smu.set_voltage(1, 3.3)
    time.sleep(0.1)  # Allow settling time
    
    # Take measurements
    voltage, current = smu.measure_voltage_and_current(1)
    print(f"Voltage: {voltage:.6f}V")
    print(f"Current: {current:.6f}A")
    
    # Disable the channel when done
    smu.disable_channel(1)

Key Concepts

Connection Types

The library supports two connection methods:

  1. USB Connection: Direct connection through a USB cable

    smu = SMU(ConnectionType.USB, port="COM3")
    
  2. Network Connection: Connection over WiFi

    smu = SMU(ConnectionType.NETWORK, host="192.168.1.100")
    

Measurement Modes

The miniSMU supports two primary measurement modes:

  1. FVMI (Force Voltage, Measure Current)

    • Apply a specified voltage and measure the resulting current
    • Suitable for resistor characterisation, diode testing, etc.
    smu.set_mode(1, "FVMI")
    
  2. FIMV (Force Current, Measure Voltage)

    • Apply a specified current and measure the resulting voltage
    • Suitable for LED testing, battery characterisation, etc.
    smu.set_mode(1, "FIMV")
    

Error Handling

The library includes custom exception handling through the SMUException class:

from minismu_py import SMU, ConnectionType, SMUException

try:
    with SMU(ConnectionType.USB, port="COM3") as smu:
        # SMU operations
        pass
except SMUException as e:
    print(f"SMU Error: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

Common Operations

Device Information

# Get device identity
identity = smu.get_identity()
print(f"Connected to: {identity}")

# Get system temperatures
adc_temp, ch1_temp, ch2_temp = smu.get_temperatures()
print(f"Temperatures: ADC: {adc_temp}°C, CH1: {ch1_temp}°C, CH2: {ch2_temp}°C")

Channel Configuration

# Select channel to configure
channel = 1

# Set mode (FVMI: Force Voltage, Measure Current)
smu.set_mode(channel, "FVMI")

# Set voltage range (AUTO, LOW, HIGH)
smu.set_voltage_range(channel, "AUTO")

# Enable channel
smu.enable_channel(channel)

Setting Output Values

# Set voltage (in volts)
smu.set_voltage(1, 3.3)

# Set current (in amperes)
smu.set_current(2, 0.01)  # 10mA

Taking Measurements

# Measure voltage only
voltage = smu.measure_voltage(1)
print(f"Voltage: {voltage}V")

# Measure current only
current = smu.measure_current(1)
print(f"Current: {current}A")

# Measure both voltage and current in one command
voltage, current = smu.measure_voltage_and_current(1)
print(f"Voltage: {voltage}V, Current: {current}A")

Control Channel State

# Enable channel
smu.enable_channel(1)

# Disable channel
smu.disable_channel(1)

Advanced Features

Voltage Sweep

You can perform a voltage sweep to generate I-V curves:

import csv
from tqdm import tqdm
import time

def voltage_sweep(smu, channel=1, start=-1.0, stop=1.0, step=0.1):
    """Perform a voltage sweep and measure current"""
    try:
        # Configure channel
        smu.set_mode(channel, "FVMI")
        smu.set_voltage_range(channel, "AUTO")
        smu.enable_channel(channel)
        
        # Create voltage points
        voltages = [round(v, 3) for v in 
                   [start + step*i for i in range(int((stop-start)/step) + 1)]]
        results = []
        
        # Create progress bar
        for voltage in tqdm(voltages, desc="Voltage Sweep"):
            smu.set_voltage(channel, voltage)
            time.sleep(0.1)  # Allow settling time
            v, i = smu.measure_voltage_and_current(channel)
            results.append({
                'target_voltage': voltage,
                'measured_voltage': v,
                'measured_current': i
            })
        
        return results
            
    finally:
        # Always disable channel after measurement
        smu.disable_channel(channel)

# Run voltage sweep and save results
with SMU(ConnectionType.USB, port="COM3") as smu:
    sweep_results = voltage_sweep(smu, channel=1, start=-1.0, stop=1.0, step=0.05)
    
    # Save results to CSV
    with open('voltage_sweep_results.csv', 'w', newline='') as f:
        writer = csv.DictWriter(f, fieldnames=[
            'target_voltage', 'measured_voltage', 'measured_current'])
        writer.writeheader()
        writer.writerows(sweep_results)
    print("Results saved to voltage_sweep_results.csv")

Data Streaming

For high-speed data acquisition, the miniSMU supports data streaming:

import time
import csv
from tqdm import tqdm
from datetime import datetime

def stream_data(smu, channel=1, duration=10.0, sample_rate=100.0, voltage=3.3):
    """Stream data from the SMU for a specified duration"""
    try:
        # Configure channel
        smu.set_mode(channel, "FVMI")
        smu.enable_channel(channel)
        smu.set_voltage(channel, voltage)
        
        # Configure streaming
        smu.set_sample_rate(channel, sample_rate)
        
        # Set miniSMU's internal time for timestamping
        current_time = int(time.time() * 1000)  # Convert to milliseconds
        smu.set_time(current_time)
        
        # Calculate number of samples
        num_samples = int(duration * sample_rate)
        
        # Prepare data storage
        timestamps = []
        voltages = []
        currents = []
        
        # Start streaming
        print("Starting streaming...")
        smu.start_streaming(channel)
        
        # Collect data with progress bar
        pbar = tqdm(total=num_samples, desc="Streaming Data")
        while len(timestamps) < num_samples:
            try:
                # Read streaming data packet
                data_channel, t, v, i = smu.read_streaming_data()
                
                # Only process data from the requested channel
                if data_channel == channel:
                    timestamps.append(t)
                    voltages.append(v)
                    currents.append(i)
                    pbar.update(1)
                
            except Exception as e:
                print(f"\nError reading streaming data: {e}")
                break
        
        # Stop streaming
        smu.stop_streaming(channel)
        pbar.close()
        
        # Save data to CSV
        filename = f'streaming_data_{datetime.now().strftime("%Y%m%d_%H%M%S")}.csv'
        with open(filename, 'w', newline='') as f:
            writer = csv.writer(f)
            writer.writerow(['Timestamp (s)', 'Voltage (V)', 'Current (A)'])
            for t, v, i in zip(timestamps, voltages, currents):
                writer.writerow([t, v, i])
        
        print(f"Data saved to {filename}")
        return timestamps, voltages, currents
            
    finally:
        # Always cleanup
        smu.disable_channel(channel)
        smu.stop_streaming(channel)

# Stream data for 5 seconds at 100Hz
with SMU(ConnectionType.USB, port="COM3") as smu:
    timestamps, voltages, currents = stream_data(
        smu, 
        channel=1,
        duration=5.0,
        sample_rate=100.0,
        voltage=3.3
    )

WiFi Configuration

The library provides functions to manage WiFi settings:

# Scan for available networks
networks = smu.wifi_scan()
for network in networks:
    print(f"SSID: {network['ssid']}, RSSI: {network['rssi']}")

# Configure WiFi credentials
smu.set_wifi_credentials("YourNetworkSSID", "YourPassword")

# Enable WiFi
smu.enable_wifi()

# Check WiFi status
status = smu.get_wifi_status()
print(f"Connected: {status.connected}")
print(f"SSID: {status.ssid}")
print(f"IP Address: {status.ip_address}")
print(f"Signal Strength: {status.rssi}dBm")

Application Examples

Device Characterisation

This example characterises a diode by performing a voltage sweep and analysing the results:

import numpy as np
import matplotlib.pyplot as plt
from minismu_py import SMU, ConnectionType

with SMU(ConnectionType.USB, port="COM3") as smu:
    # Configure channel
    smu.set_mode(1, "FVMI")
    smu.enable_channel(1)
    
    # Perform voltage sweep
    voltages = np.linspace(-0.5, 1.0, 100)  # -0.5V to 1.0V in 100 steps
    measured_voltages = []
    measured_currents = []
    
    for v in voltages:
        smu.set_voltage(1, v)
        time.sleep(0.05)  # Allow settling time
        mv, mi = smu.measure_voltage_and_current(1)
        measured_voltages.append(mv)
        measured_currents.append(mi)
    
    # Disable channel when done
    smu.disable_channel(1)
    
    # Plot results
    plt.figure(figsize=(10, 6))
    plt.plot(measured_voltages, measured_currents * 1000)  # Convert to mA
    plt.grid(True)
    plt.xlabel('Voltage (V)')
    plt.ylabel('Current (mA)')
    plt.title('Diode I-V Characteristic')
    plt.savefig('diode_iv_curve.png')
    plt.show()

Automated Test Sequence

This example demonstrates an automated test sequence for a battery:

import time
import csv
from datetime import datetime
from minismu_py import SMU, ConnectionType

def battery_discharge_test(smu, channel=1, discharge_current=0.01, cutoff_voltage=3.0):
    """Perform battery discharge test until cutoff voltage is reached"""
    try:
        # Configure for constant current discharge (FIMV mode)
        smu.set_mode(channel, "FIMV")
        smu.enable_channel(channel)
        
        # Set discharge current (negative for discharge)
        smu.set_current(channel, -discharge_current)
        
        # Prepare data storage
        start_time = time.time()
        results = []
        
        print(f"Starting battery discharge test at {discharge_current*1000:.1f}mA")
        print(f"Test will run until battery voltage reaches {cutoff_voltage}V")
        
        # Run test until cutoff voltage is reached
        while True:
            # Measure voltage and current
            voltage, current = smu.measure_voltage_and_current(channel)
            
            # Calculate elapsed time
            elapsed_time = time.time() - start_time
            
            # Record data
            results.append({
                'time': elapsed_time,
                'voltage': voltage,
                'current': current
            })
            
            # Display progress
            print(f"\rTime: {elapsed_time:.1f}s, Voltage: {voltage:.3f}V, Current: {current*1000:.1f}mA", end="")
            
            # Check if cutoff voltage reached
            if voltage <= cutoff_voltage:
                print("\nCutoff voltage reached. Test complete.")
                break
            
            # Wait before next measurement
            time.sleep(5)  # Take measurements every 5 seconds
        
        # Save results
        filename = f'battery_discharge_{datetime.now().strftime("%Y%m%d_%H%M%S")}.csv'
        with open(filename, 'w', newline='') as f:
            writer = csv.DictWriter(f, fieldnames=['time', 'voltage', 'current'])
            writer.writeheader()
            writer.writerows(results)
        print(f"Results saved to {filename}")
        
        return results
    
    finally:
        # Always disable channel when done
        smu.disable_channel(channel)

# Run battery discharge test
with SMU(ConnectionType.USB, port="COM3") as smu:
    battery_discharge_test(
        smu,
        channel=1,
        discharge_current=0.01,  # 10mA discharge
        cutoff_voltage=3.0       # 3.0V cutoff
    )

API Reference

Connection Management

MethodDescription
SMU(connection_type, port, host, tcp_port)Initialise SMU connection
close()Close the connection
get_identity()Get device identification
reset()Reset the device

Source and Measurement

MethodDescription
set_voltage(channel, voltage)Set voltage for specified channel
set_current(channel, current)Set current for specified channel
measure_voltage(channel)Measure voltage on specified channel
measure_current(channel)Measure current on specified channel
measure_voltage_and_current(channel)Measure both voltage and current

Channel Configuration

MethodDescription
enable_channel(channel)Enable specified channel
disable_channel(channel)Disable specified channel
set_voltage_range(channel, range_type)Set voltage range (AUTO/LOW/HIGH)
set_mode(channel, mode)Set channel mode (FIMV/FVMI)

Data Streaming

MethodDescription
start_streaming(channel)Start data streaming
stop_streaming(channel)Stop data streaming
read_streaming_data()Read streaming data packet (channel, timestamp, voltage, current)
set_sample_rate(channel, rate)Set sample rate in Hz

System Configuration

MethodDescription
set_led_brightness(brightness)Set LED brightness (0-100)
get_led_brightness()Get current LED brightness
get_temperatures()Get system temperatures (ADC, Channel 1, Channel 2)
set_time(timestamp)Set device's internal clock (Unix timestamp in milliseconds)

WiFi Configuration

MethodDescription
wifi_scan()Scan for available WiFi networks
get_wifi_status()Get current WiFi status
set_wifi_credentials(ssid, password)Set WiFi credentials
enable_wifi()Enable WiFi
disable_wifi()Disable WiFi

Best Practices

Here are some recommended practices when working with the minismu_py library:

1. Use context managers

Always use Python context managers to ensure proper resource cleanup:

with SMU(ConnectionType.USB, port="COM3") as smu:
    # Code here

2. Always disable channels

Disable channels when measurements are complete:

try:
    smu.enable_channel(1)
    # Your measurement code
finally:
    smu.disable_channel(1)

3. Add settling time

Include settling time for stable measurements:

smu.set_voltage(1, 3.3)
time.sleep(0.1)  # Allow settling time before measuring

4. Handle exceptions properly

Implement proper exception handling:

try:
    # SMU operations
except SMUException as e:
    print(f"SMU Error: {e}")

5. Check connection status

Verify connection before running operations:

identity = smu.get_identity()
if not identity:
    print("Device not responding")

6. Close connections

Always close connections when finished:

smu.close()

Troubleshooting

Common Issues

  1. Can't connect to device

    • Verify the device is powered on
    • Check USB cable connection
    • Confirm correct port name
    • Ensure no other software is using the port
  2. Communication errors

    • Reset the device
    • Close and reopen the connection
    • Check for interference or cable issues
    • Verify correct baud rate (115200)
  3. Unexpected measurement results

    • Check channel configuration (mode, range)
    • Allow sufficient settling time
    • Verify connections to device under test
    • Check protection limits
  4. WiFi connection issues

    • Ensure WiFi antenna is properly attached
    • Verify WiFi credentials
    • Check signal strength
    • Try USB connection first to configure WiFi

Further Resources

For more advanced examples and detailed documentation, visit the official repository and documentation links above.