# Keithley 6221

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

**Driver:** [pymeasure](https://pymeasure.readthedocs.io)  
**Type:** [Power supplies](https://dev.nominal.io/instro/instruments/type/psu.md)  
**Manufacturer:** [Keithley](https://dev.nominal.io/instro/instruments/manufacturer/keithley.md)

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](https://dev.nominal.io/instro/library/publishers.md). It takes two forms, both described in the [pymeasure guide](https://dev.nominal.io/instro/library/pymeasure.md). Install with `uv add "instro[pymeasure]"`.

### With a PymeasureInstrument

[`PymeasureInstrument`](https://dev.nominal.io/instro/library/pymeasure.md#write-new-instro-code) 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.

```python
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()`](https://dev.nominal.io/instro/library/pymeasure.md#publish-from-existing-code) 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.

```python
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`](https://pymeasure.readthedocs.io/en/latest/api/instruments/keithley/keithley6221.html#pymeasure.instruments.keithley.keithley6221.Keithley6221), which lists every property and method.
