Skip to content

Commit fa460d4

Browse files
modernize my old guide from doc (Feb 15, 2024) to md
1 parent eb154de commit fa460d4

1 file changed

Lines changed: 227 additions & 0 deletions

File tree

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
> This was a mini guide for those who want to try starting interfacing instruments, from Feb 15, 2024.
2+
3+
# Python Instrument Interfacing: A Beginner's Guide
4+
5+
This guide provides a basic introduction to interfacing with instruments using Python. It covers the necessary setup, basic commands, and provides example scripts for common tasks.
6+
7+
## 1. Hardware and Driver Setup
8+
9+
### USB to GPIB Converter
10+
- **Converter:** Keysight USB to GPIB Converter
11+
- **Indicator:** A green light on the converter indicates it's ready.
12+
13+
### Instrument Configuration
14+
- **Enable GPIB:** Ensure GPIB is enabled on your instrument.
15+
- **GPIB Address:** Note the GPIB address of your instrument.
16+
17+
### Driver Installation
18+
Install the required Python packages using pip:
19+
```bash
20+
pip install pyvisa pymeasure numpy pandas matplotlib
21+
```
22+
23+
## 2. Basic Communication Test
24+
25+
You can quickly test the connection to your instruments by listing the available resources.
26+
27+
```python
28+
import pyvisa
29+
30+
# Initialize the resource manager
31+
rm = pyvisa.ResourceManager()
32+
33+
# List all connected resources
34+
print(rm.list_resources())
35+
```
36+
This will print a list of connected instrument IDs. Copy the ID for the instrument you want to communicate with.
37+
38+
## 3. Instrument Initialization
39+
40+
To start sending commands to an instrument, you need to initialize it using its VISA resource ID.
41+
42+
```python
43+
import pyvisa
44+
45+
# Initialize the resource manager
46+
rm = pyvisa.ResourceManager()
47+
48+
# Open a connection to the instrument
49+
keithley = rm.open_resource("GPIB::12") # Replace "GPIB::12" with your instrument's ID
50+
51+
# Reset the instrument and clear its status
52+
keithley.write("*rst; status:preset; *cls")
53+
```
54+
55+
**Key Functions:**
56+
- `keithley.write()`: Sends a command to the instrument. Refer to the instrument's manual for a list of commands.
57+
- `keithley.query()`: Sends a command and returns the instrument's response as a string.
58+
59+
## 4. Using PyMeasure
60+
61+
PyMeasure provides a higher-level interface for many instruments, simplifying communication.
62+
63+
**Discovering Commands:**
64+
You can see the available commands for a `pymeasure` instrument object using the `dir()` function.
65+
```python
66+
# Eg: print(dir(keithley_2400))
67+
```
68+
69+
---
70+
71+
## 5. Example Scripts
72+
73+
### Example 1: Keithley 2400 Current Source Test
74+
75+
This script demonstrates how to interface with a Keithley 2400 to source current.
76+
77+
`2400_current_check.py`
78+
```python
79+
# Name: Interfacing Keithley 2400 (current source)
80+
# Author: Prathamesh
81+
# Created: 27/10/2022
82+
# Copyright: (c) Instrument-DSL 2022
83+
84+
import pymeasure
85+
import numpy as np
86+
from time import sleep
87+
from pymeasure.instruments.keithley import Keithley2400
88+
import pandas as pd
89+
90+
# Object creation
91+
keithley_2400 = Keithley2400("GPIB::4") # Replace with your instrument's ID
92+
93+
# Initial setup for Keithley 2400
94+
keithley_2400.apply_current() # Set up to source current
95+
keithley_2400.source_current_range = 1e-3 # Set the source current range
96+
sleep(10)
97+
keithley_2400.compliance_voltage = 210 # Set the compliance voltage to 210V
98+
keithley_2400.source_current = 0 # Set the source current to 0A
99+
keithley_2400.enable_source() # Enable the source output
100+
sleep(15)
101+
102+
# Ramp to a specific current
103+
cur = 1
104+
keithley_2400.ramp_to_current(cur * 1e-3) # Ramp to 1mA
105+
sleep(15)
106+
print(f"Current set to: {cur * 1e-3} A")
107+
sleep(180)
108+
109+
# Shutdown the instrument
110+
keithley_2400.shutdown() # Ramp the current to 0A and disable output
111+
```
112+
113+
### Example 2: Interfacing Keithley 2400 and Keithley 2182
114+
115+
This script shows how to use a Keithley 2400 as a current source and a Keithley 2182 as a nanovoltmeter to perform an I-V sweep.
116+
117+
`combine-2400-2182-Updated.py`
118+
```python
119+
# Name: Interfacing Keithley 2400 (current source) and Keithley 2182 (nanovoltmeter)
120+
# Author: Instrument-DSL
121+
# Created: 27/10/2022
122+
# Copyright: (c) Instrument-DSL 2022
123+
124+
import pymeasure
125+
import numpy as np
126+
import matplotlib.pyplot as plt
127+
from time import sleep
128+
import pyvisa
129+
from pymeasure.instruments.keithley import Keithley2400
130+
import pandas as pd
131+
132+
# Object creation
133+
rm1 = pyvisa.ResourceManager()
134+
keithley_2182 = rm1.open_resource("GPIB::7") # Replace with your instrument's ID
135+
keithley_2182.write("*rst; status:preset; *cls")
136+
keithley_2400 = Keithley2400("GPIB::4") # Replace with your instrument's ID
137+
sleep(5)
138+
139+
# Data storage
140+
I = []
141+
Volt = []
142+
interval = 1
143+
number_of_readings = 2
144+
145+
# User input
146+
I_range = float(input("Enter value of I: "))
147+
I_step = float(input("Enter steps: "))
148+
filename = input("Enter filename: ")
149+
150+
# Initial setup for Keithley 2400
151+
keithley_2400.apply_current() # Set up to source current
152+
keithley_2400.source_current_range = 1e-6 # Set source current range to 1uA
153+
sleep(10)
154+
keithley_2400.compliance_voltage = 150 # Set compliance voltage to 150V
155+
keithley_2400.source_current = 0 # Set source current to 0A
156+
keithley_2400.enable_source() # Enable the source output
157+
sleep(15)
158+
159+
# I-V sweep loop
160+
for cur in np.arange(-I_range, I_range + I_step, I_step):
161+
keithley_2400.ramp_to_current(cur * 1e-6)
162+
sleep(15)
163+
164+
# Configure and trigger Keithley 2182
165+
keithley_2182.write("status:measurement:enable 512; *sre 1")
166+
keithley_2182.write(f"sample:count {number_of_readings}")
167+
keithley_2182.write("trigger:source bus")
168+
keithley_2182.write(f"trigger:delay {interval}")
169+
keithley_2182.write(f"trace:points {number_of_readings}")
170+
keithley_2182.write("trace:feed sense1; feed:control next")
171+
keithley_2182.write("initiate")
172+
keithley_2182.assert_trigger()
173+
sleep(10)
174+
keithley_2182.wait_for_srq()
175+
sleep(20)
176+
177+
# Read data
178+
voltages = keithley_2182.query_ascii_values("trace:data?")
179+
keithley_2182.query("status:measurement?")
180+
keithley_2182.write("trace:clear; feed:control next")
181+
v_avr = sum(voltages) / len(voltages)
182+
sleep(10)
183+
184+
# Store data
185+
I.append(cur * 1e-6)
186+
Volt.append(v_avr)
187+
print(f"Current: {cur * 1e-6} A, Voltage: {v_avr} V")
188+
189+
# Reset Keithley 2182
190+
keithley_2182.write("*rst; status:preset; *cls")
191+
keithley_2182.clear()
192+
sleep(15)
193+
194+
# Data saving
195+
df = pd.DataFrame({'I': I, 'V': Volt})
196+
print(df)
197+
df.to_csv(f'E:/Python/Python output files/IV Output/Test_IV_data_at_RT_{filename}.txt', index=None, sep=' ', mode='w')
198+
199+
# Plotting
200+
plt.plot(I, Volt, marker='o', linestyle='-', color='g', label='I-V Curve')
201+
plt.xlabel('Current (A)')
202+
plt.ylabel('Voltage (V)')
203+
plt.title('I-V Curve')
204+
plt.legend()
205+
plt.show()
206+
207+
# Shutdown instruments
208+
keithley_2400.shutdown()
209+
keithley_2182.clear()
210+
keithley_2182.close()
211+
212+
```
213+
214+
## 6. Troubleshooting and Tips
215+
216+
- **Restart Instrument:** If you encounter issues, try restarting the instrument.
217+
- **Restart Program:** Restarting the Python script can also resolve connection problems.
218+
219+
## 7. References
220+
221+
- **PyVISA Documentation:** [https://pyvisa.readthedocs.io/en/latest/](https://pyvisa.readthedocs.io/en/latest/)
222+
- **PyMeasure Documentation:** [https://pymeasure.readthedocs.io/en/latest/](https://pymeasure.readthedocs.io/en/latest/)
223+
224+
### Recommended Videos
225+
226+
- [https://www.youtube.com/watch?v=TLUTCDbt52I](https://www.youtube.com/watch?v=TLUTCDbt52I)
227+
- [https://www.youtube.com/watch?v=DUJpL9pMy8Y](https://www.youtube.com/watch?v=DUJpL9pMy8Y)

0 commit comments

Comments
 (0)