Skip to content

Instantly share code, notes, and snippets.

@cfangmeier
Last active April 22, 2022 11:40
Show Gist options
  • Save cfangmeier/1859de7d67ab74099a3b to your computer and use it in GitHub Desktop.
Save cfangmeier/1859de7d67ab74099a3b to your computer and use it in GitHub Desktop.
Creates an IV curve using Keithley 2410 Sweep Function
#!/usr/bin/env python3
import serial
import matplotlib.pyplot as plt
script = """
# hashes make comments
# @ on a read specifies the parser for the read data
*RST
:SENS:FUNC:CONC OFF #Turn off concurrent functions
:SOUR:FUNC VOLT #Set supply to voltage
:SENS:FUNC 'CURR:DC' #Set measurement to DC current
:SENS:CURR:PROT 20E-3 #Set compliance current
:SOUR:VOLT:START 0 #Set Start Voltage
:SOUR:VOLT:STOP 10 #Set Stop Voltage
:SOUR:VOLT:STEP 1 #Set Voltage Step
:SOUR:VOLT:MODE SWE #Enable sweep mode
:SOUR:SWE:RANG AUTO #Set range to auto
:SOUR:SWE:SPAC LIN #Use linear sweep
:TRIG:COUN 10 #Number of measurements(should be compatable with START|STOP|STEP)
:SOUR:DEL 0.1 #Delay between Voltage set and current measure
:FORM:ELEM VOLT,CURR #Format of output
:OUTP ON #Enable HV
:READ? @IV #Read back measurements
:OUTP OFF #Turn off output
"""
def process_script():
global script
script = script.splitlines()
script = [line.split('#')[0].strip() for line in script]
script = [line for line in script if len(line) > 0]
for i, line in enumerate(script):
line = line.split('@')
if len(line) == 1:
script[i] = (line[0]+'\n',None)
else:
script[i] = (line[0].strip()+'\n',line[1])
def read(ser):
data = bytes()
endl = '\n'.encode('ascii')[0]
while True:
data += ser.read(1)
if data[-1] == endl:
break
return data.decode('ascii')
def parse(parser, result):
if parser == "IV":
result = result.split(',')
current = []
voltage = []
for i, item in enumerate(result):
if not i % 2:
voltage.append(float(item))
else:
current.append(float(item))
for v, c in zip(voltage, current):
print("{0:+10.5e} {1:+10.5e}".format(v,c))
plt.xlabel('Voltage(Volts)')
plt.ylabel('Current(Amps)')
plt.plot(voltage,current,'.')
else:
raise NotImplementedError("This parser is not implemented: {0}".format(parser))
def main():
global script
process_script()
with serial.Serial('/dev/ttyUSB1') as ser:
for command, parser in script:
print('==>'+command,end='')
ser.write(command.encode('ascii'))
if parser != None:
parse(parser, read(ser))
ser.flushOutput()
plt.show()
if __name__ == '__main__':
main();
@cfangmeier
Copy link
Author

On 1 1k Resistor, The output is the following

==>*RST
==>:SENS:FUNC:CONC OFF
==>:SOUR:FUNC VOLT
==>:SENS:FUNC 'CURR:DC'
==>:SENS:CURR:PROT 20E-3
==>:SOUR:VOLT:START 0
==>:SOUR:VOLT:STOP 10
==>:SOUR:VOLT:STEP 1
==>:SOUR:VOLT:MODE SWE
==>:SOUR:SWE:RANG AUTO
==>:SOUR:SWE:SPAC LIN
==>:TRIG:COUN 10
==>:SOUR:DEL 0.1
==>:FORM:ELEM VOLT,CURR
==>:OUTP ON
==>:READ?
+0.00000e+00  -1.54654e-07
+1.00000e+00  +1.01177e-03
+2.00000e+00  +2.02404e-03
+3.00000e+00  +3.03561e-03
+4.00000e+00  +4.04646e-03
+5.00000e+00  +5.06017e-03
+6.00000e+00  +6.07269e-03
+7.00000e+00  +7.08480e-03
+8.00000e+00  +8.09952e-03
+9.00000e+00  +9.11248e-03
==>:OUTP OFF

Also creates a scatter-plot using matplotlib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment