PyVISA
PyVISADocs

Signal Generator Example

Control a Keysight 33500B signal generator with PyVISA to output waveforms, frequency sweeps, burst mode, and modulation.

This example connects to a Keysight 33500B (or any 335xxB) function generator over USB. It generates a sine wave, configures a frequency sweep, sets up burst mode, and applies AM modulation.

import pyvisa

rm = pyvisa.ResourceManager()
gen = rm.open_resource("USB0::0x0957::0x2C07::MY52100001::INSTR")
gen.timeout = 10000

try:
    idn = gen.query("*IDN?")
    print(f"Connected to: {idn.strip()}")

    gen.write("*RST")
    gen.write("*CLS")

    # Generate a 1 kHz sine wave at 1 Vpp
    gen.write("SOUR1:FUNC SIN")
    gen.write("SOUR1:FREQ 1000")
    gen.write("SOUR1:VOLT 1.0")
    gen.write("SOUR1:VOLT:OFFS 0")
    gen.write("OUTP1 ON")

    freq = gen.query("SOUR1:FREQ?")
    ampl = gen.query("SOUR1:VOLT?")
    print(f"Output: {float(freq):.0f} Hz sine at {float(ampl):.3f} Vpp")

finally:
    gen.close()
    rm.close()

Log test waveforms automatically

TofuPilot records signal generator configurations and test verdicts across production runs. Free to start.

Waveform Types

Switch between standard waveforms by changing the function.

import pyvisa

rm = pyvisa.ResourceManager()
gen = rm.open_resource("USB0::0x0957::0x2C07::MY52100001::INSTR")

try:
    gen.write("*RST")

    # Square wave, 10 kHz, 2 Vpp, 50% duty cycle
    gen.write("SOUR1:FUNC SQU")
    gen.write("SOUR1:FREQ 10000")
    gen.write("SOUR1:VOLT 2.0")
    gen.write("SOUR1:FUNC:SQU:DCYC 50")
    gen.write("OUTP1 ON")
    print(f"Square: {gen.query('SOUR1:FREQ?').strip()} Hz")

    # Ramp wave, 500 Hz, 3 Vpp, 75% symmetry
    gen.write("SOUR1:FUNC RAMP")
    gen.write("SOUR1:FREQ 500")
    gen.write("SOUR1:VOLT 3.0")
    gen.write("SOUR1:FUNC:RAMP:SYMM 75")
    print(f"Ramp: {gen.query('SOUR1:FREQ?').strip()} Hz")

    # Pulse, 1 MHz, 100 ns width
    gen.write("SOUR1:FUNC PULS")
    gen.write("SOUR1:FREQ 1e6")
    gen.write("SOUR1:FUNC:PULS:WIDT 100e-9")
    print(f"Pulse: {gen.query('SOUR1:FREQ?').strip()} Hz")

    gen.write("OUTP1 OFF")
finally:
    gen.close()
    rm.close()

Frequency Sweep

Sweep from 100 Hz to 100 kHz over 5 seconds with a linear ramp.

import pyvisa
import time

rm = pyvisa.ResourceManager()
gen = rm.open_resource("USB0::0x0957::0x2C07::MY52100001::INSTR")

try:
    gen.write("*RST")
    gen.write("SOUR1:FUNC SIN")
    gen.write("SOUR1:VOLT 1.0")

    # Configure sweep
    gen.write("SOUR1:FREQ:STAR 100")
    gen.write("SOUR1:FREQ:STOP 100000")
    gen.write("SOUR1:SWE:TIME 5")
    gen.write("SOUR1:SWE:SPAC LIN")  # LIN or LOG
    gen.write("SOUR1:SWE:STAT ON")
    gen.write("TRIG1:SOUR IMM")
    gen.write("OUTP1 ON")

    print("Sweeping 100 Hz to 100 kHz over 5 seconds")
    time.sleep(5.5)

    gen.write("OUTP1 OFF")
    gen.write("SOUR1:SWE:STAT OFF")
finally:
    gen.close()
    rm.close()

Burst Mode

Output exactly 5 cycles of a 10 kHz sine when triggered.

import pyvisa

rm = pyvisa.ResourceManager()
gen = rm.open_resource("USB0::0x0957::0x2C07::MY52100001::INSTR")

try:
    gen.write("*RST")
    gen.write("SOUR1:FUNC SIN")
    gen.write("SOUR1:FREQ 10000")
    gen.write("SOUR1:VOLT 1.5")

    # Burst config: 5 cycles, triggered
    gen.write("SOUR1:BURS:MODE TRIG")
    gen.write("SOUR1:BURS:NCYC 5")
    gen.write("SOUR1:BURS:PHAS 0")
    gen.write("TRIG1:SOUR BUS")
    gen.write("SOUR1:BURS:STAT ON")
    gen.write("OUTP1 ON")

    # Fire the burst
    gen.write("*TRG")
    print("Burst sent: 5 cycles at 10 kHz")

    gen.write("OUTP1 OFF")
finally:
    gen.close()
    rm.close()

AM Modulation

Apply amplitude modulation with a 1 kHz modulating signal on a 100 kHz carrier.

import pyvisa

rm = pyvisa.ResourceManager()
gen = rm.open_resource("USB0::0x0957::0x2C07::MY52100001::INSTR")

try:
    gen.write("*RST")

    # Carrier: 100 kHz sine, 2 Vpp
    gen.write("SOUR1:FUNC SIN")
    gen.write("SOUR1:FREQ 100000")
    gen.write("SOUR1:VOLT 2.0")

    # AM modulation: 80% depth, 1 kHz internal sine
    gen.write("SOUR1:AM:SOUR INT")
    gen.write("SOUR1:AM:INT:FUNC SIN")
    gen.write("SOUR1:AM:INT:FREQ 1000")
    gen.write("SOUR1:AM:DEPT 80")
    gen.write("SOUR1:AM:STAT ON")
    gen.write("OUTP1 ON")

    print("AM modulation active: 100 kHz carrier, 1 kHz mod, 80% depth")

    gen.write("OUTP1 OFF")
finally:
    gen.close()
    rm.close()

Commands Used

CommandWhat it does
SOUR1:FUNC SIN|SQU|RAMP|PULSSet waveform shape
SOUR1:FREQ <Hz>Set output frequency
SOUR1:VOLT <Vpp>Set peak-to-peak amplitude
SOUR1:VOLT:OFFS <V>Set DC offset
SOUR1:FUNC:SQU:DCYC <percent>Set square wave duty cycle
SOUR1:FUNC:RAMP:SYMM <percent>Set ramp symmetry
SOUR1:FUNC:PULS:WIDT <seconds>Set pulse width
SOUR1:FREQ:STAR / STOPSet sweep start/stop frequency
SOUR1:SWE:TIME <seconds>Set sweep duration
SOUR1:SWE:SPAC LIN|LOGSet sweep spacing (linear or logarithmic)
SOUR1:SWE:STAT ON|OFFEnable/disable sweep
SOUR1:BURS:MODE TRIGSet burst to triggered mode
SOUR1:BURS:NCYC <n>Set number of cycles per burst
SOUR1:BURS:STAT ON|OFFEnable/disable burst mode
SOUR1:AM:DEPT <percent>Set AM modulation depth
SOUR1:AM:INT:FREQ <Hz>Set internal modulation frequency
SOUR1:AM:STAT ON|OFFEnable/disable AM modulation
OUTP1 ON|OFFEnable/disable channel 1 output
*TRGSend a software trigger (bus trigger)