Keithley 6221#

Control the Keithley 6221 power supply from Python with the Keithley6221 driver.

Library: pymeasure (docs)
Type: Power supplies
Manufacturer: Keithley

Keithley 6221

Represents the Keithley 6221 AC and DC current source and provides a high-level interface for interacting with the instrument.

Publishing through instro#

instro publishes the commands you send to a Keithley6221 and the values you read from it, through any publisher. It takes two forms, both described in the pymeasure guide. Install with uv add "instro[pymeasure]".

With a PymeasureInstrument#

PymeasureInstrument wraps the driver in an instro instrument, with tags and background polling. The instrument is created inside the with block, and .device is the pymeasure instrument.

from pymeasure.instruments.keithley import Keithley6221
from instro.pymeasure import PymeasureInstrument

with PymeasureInstrument(
    name="keithley",
    instrument=Keithley6221("GPIB::1"),
    dataset_rid="<dataset rid>",
) as keithley:
    keithley.device.clear()

    # Use the keithley as an AC source
    keithley.device.waveform_function = "square"   # Set a square waveform
    keithley.device.waveform_amplitude = 0.05      # Set the amplitude in Amps
    keithley.device.waveform_offset = 0            # Set zero offset
    keithley.device.source_compliance = 10         # Set compliance (limit) in V
    keithley.device.waveform_dutycycle = 50        # Set duty cycle of wave in %
    keithley.device.waveform_frequency = 347       # Set the frequency in Hz
    keithley.device.waveform_ranging = "best"      # Set optimal output ranging
    keithley.device.waveform_duration_cycles = 100 # Set duration of the waveform

    # Link end of waveform to Service Request status bit
    keithley.device.operation_event_enabled = 128  # OSB listens to end of wave
    keithley.device.srq_event_enabled = 128        # SRQ listens to OSB

    keithley.device.waveform_arm()                 # Arm (load) the waveform

    keithley.device.waveform_start()               # Start the waveform

    keithley.device.adapter.wait_for_srq()         # Wait for the pulse to finish

    keithley.device.waveform_abort()               # Disarm (unload) the waveform

    keithley.device.shutdown()                     # Disables output

With existing pymeasure code (attach())#

attach() adds publishing to pymeasure code you already have. The imports, the attach() call after the instrument is created, and recorder.close() are all that instro adds.

from pymeasure.instruments.keithley import Keithley6221
from instro.lib import NominalCorePublisher
from instro.pymeasure import attach

keithley = Keithley6221("GPIB::1")
recorder = attach(keithley, "keithley", [NominalCorePublisher(dataset_rid="<dataset rid>")])
keithley.clear()

# Use the keithley as an AC source
keithley.waveform_function = "square"   # Set a square waveform
keithley.waveform_amplitude = 0.05      # Set the amplitude in Amps
keithley.waveform_offset = 0            # Set zero offset
keithley.source_compliance = 10         # Set compliance (limit) in V
keithley.waveform_dutycycle = 50        # Set duty cycle of wave in %
keithley.waveform_frequency = 347       # Set the frequency in Hz
keithley.waveform_ranging = "best"      # Set optimal output ranging
keithley.waveform_duration_cycles = 100 # Set duration of the waveform

# Link end of waveform to Service Request status bit
keithley.operation_event_enabled = 128  # OSB listens to end of wave
keithley.srq_event_enabled = 128        # SRQ listens to OSB

keithley.waveform_arm()                 # Arm (load) the waveform

keithley.waveform_start()               # Start the waveform

keithley.adapter.wait_for_srq()         # Wait for the pulse to finish

keithley.waveform_abort()               # Disarm (unload) the waveform

keithley.shutdown()                     # Disables output

recorder.close()  # flush and close the publishers

The examples are adapted from the pymeasure documentation for Keithley6221, which lists every property and method.

Send the data to Nominal#

With pymeasure, the Keithley 6221 sends each reading to a Nominal dataset, where your team can plot it, compare runs, and analyze it together. This code creates an asset for the bench, a dataset and a run, and a workbook scoped to the run, then publishes the readings to the dataset. See Bench bring-up for the same steps on a whole bench.

from pymeasure.instruments.keithley import Keithley6221
from instro.lib import NominalCorePublisher
from instro.pymeasure import PymeasureInstrument

import pandas as pd
from nominal.core import NominalClient

client = NominalClient.from_profile("default")
bench = client.get_or_create_asset_by_properties(
    {"bench_id": "bench-1"}, name="Bench 1"
)
dataset = bench.get_or_create_dataset("bench", name="Bench 1 instruments")
run = bench.create_run(
    "Bench 1 session", start=pd.Timestamp.now(tz="UTC").floor("s"), end=None
)

keithley6221 = PymeasureInstrument(
    name="keithley6221",
    instrument=Keithley6221("<visa_resource>"),
    publishers=[NominalCorePublisher(dataset=dataset)],
)

workbook = run.create_workbook(title="Keithley 6221 readings")
print(workbook.nominal_url)  # open it to plot the channels

Open the Workbook link and plot the instrument’s channels, as in the mock below.

Illustrative mock of a Nominal workbook plotting Keithley 6221 dataapp.gov.nominal.ioKeithley 6221 readingsPrimary tabCHANNELSkeithley6221.ch1.voltagebenchkeithley6221.ch1.currentbench02.55voltage (V)00.51current (A)12:00:0012:00:1012:00:2012:00:3012:00:4012:00:502026-01-01
An illustrative mock of the workbook. The channels are named for the instrument, and the traces are made up.

Find out if your company is using Nominal