|
1 | | - |
2 | 1 | # prg for LCR Keysight E 4980 A |
3 | | -#supplimentry stuff 20-6-23 |
4 | | - |
| 2 | +# supplimentry stuff 20-6-23 |
5 | 3 |
|
6 | 4 | import pyvisa |
7 | 5 | from pymeasure.instruments.agilent import AgilentE4980 |
|
10 | 8 | import matplotlib.pyplot as plt |
11 | 9 | import pandas as pd |
12 | 10 |
|
13 | | - |
14 | | - |
15 | 11 | #--------------------------------------------------------------- |
16 | | -#user input |
17 | | - |
18 | | -V=2 # volt for loop (V) |
19 | | -V_step=2 #interval between measurements (V) |
20 | | -freq=1000 #freq in Hz |
21 | | -loop=1 |
22 | | -name="Swastika_Test2_" |
23 | | - |
24 | | -V_ac=0.5 |
25 | | - |
26 | | - |
| 12 | +# User Input |
| 13 | +V = 2 # volt for loop (V) |
| 14 | +V_step = 2 # interval between measurements (V) |
| 15 | +freq = 1000 # freq in Hz |
| 16 | +loop = 1 |
| 17 | +name = "Swastika_Test2_" |
| 18 | +V_ac = 0.5 |
27 | 19 | #--------------------------------------------------------------- |
28 | 20 |
|
| 21 | +filename = "E:/Prathamesh/Python Stuff/CV/CV_Measurements/" + str(name) + "_freq_" + str(freq) + "_volt_" + str(V) + "_V_step_" + str(V_step) + "_Loops" + str(loop) + ".txt" |
29 | 22 |
|
30 | | -filename="E:/Prathamesh/Python Stuff/CV/CV_Measurements/"+str(name)+"_freq_"+str(freq)+"_volt_"+str(V)+"_V_step_"+str(V_step)+"_Loops"+str(loop)+".txt" |
31 | 23 | #--------------------------------------------------------------- |
32 | 24 |
|
| 25 | +loop_ind_new = 0 |
| 26 | +protocol_list = [] |
| 27 | +V_list = [] |
| 28 | +C_list = [] |
| 29 | +loop_list = [] |
33 | 30 |
|
34 | | -loop_ind_new=0 |
35 | | -protocol_list=[] |
36 | | -V_list=[] |
37 | | -C_list=[] |
38 | | -loop_list=[] |
39 | 31 | #--------------------------------- |
40 | 32 | rm = pyvisa.ResourceManager() |
41 | | -my_instrument= rm.open_resource("GPIB::17") |
| 33 | +# Note: using pyvisa directly for setup commands |
| 34 | +my_instrument = rm.open_resource("GPIB::17") |
42 | 35 | LCR = AgilentE4980("GPIB::17") |
43 | 36 |
|
44 | | - |
45 | | - |
46 | 37 | my_instrument.timeout = 100000 |
47 | 38 | my_instrument.read_termination = '\n' |
48 | 39 | my_instrument.write_termination = '\n' |
49 | 40 |
|
| 41 | +my_instrument.write('*RST; *CLS') |
| 42 | +my_instrument.write(':DISP:ENAB') |
| 43 | +time.sleep(2) |
50 | 44 |
|
51 | | -my_instrument. write( '*RST; *CLS' ) |
52 | | -my_instrument. write( ':DISP:ENAB' ) |
53 | | - |
54 | | -time. sleep( 2) |
55 | | - |
56 | | -my_instrument. write( ':INIT:CONT' ) |
57 | | -my_instrument. write( ':TRIG:SOUR EXT' ) |
| 45 | +my_instrument.write(':INIT:CONT') |
| 46 | +my_instrument.write(':TRIG:SOUR EXT') |
| 47 | +time.sleep(2) |
58 | 48 |
|
59 | | -time. sleep( 2) |
| 49 | +my_instrument.write(':APER MED') |
| 50 | +my_instrument.write(':FUNC:IMP:RANGE:AUTO ON') |
| 51 | +time.sleep(2) |
60 | 52 |
|
61 | | -my_instrument. write( ':APER MED' ) |
62 | | -my_instrument. write( ':FUNC:IMP:RANGE:AUTO ON' ) |
| 53 | +my_instrument.write(':MMEM EXT') |
| 54 | +time.sleep(2) |
63 | 55 |
|
64 | | -time. sleep( 2) |
| 56 | +my_instrument.write(':MEM:DIM DBUF, ' + str(100)) |
| 57 | +time.sleep(1) |
65 | 58 |
|
66 | | -my_instrument. write( ':MMEM EXT' ) |
67 | | -time. sleep( 2) |
68 | | - |
69 | | -my_instrument. write( ':MEM:DIM DBUF, ' , str(100)) |
70 | | -time. sleep( 1) |
71 | | - |
72 | | -my_instrument. write( ':MEM:FILL DBUF' ) |
73 | | -time. sleep( 2) |
74 | | -my_instrument. write( ':MEM:CLE DBUF' ) |
75 | | -time. sleep( 3) |
76 | | -print(my_instrument.write( ':BIAS:STATe ON' )) |
77 | | -time. sleep(2) |
78 | | -my_instrument. write( ':VOLT:LEVEL ' , str(V_ac)) |
79 | | -time. sleep(2) |
| 59 | +my_instrument.write(':MEM:FILL DBUF') |
| 60 | +time.sleep(2) |
| 61 | +my_instrument.write(':MEM:CLE DBUF') |
| 62 | +time.sleep(3) |
| 63 | +print(my_instrument.write(':BIAS:STATe ON')) |
| 64 | +time.sleep(2) |
| 65 | +my_instrument.write(':VOLT:LEVEL ' + str(V_ac)) |
| 66 | +time.sleep(2) |
80 | 67 | #--------------------------------- |
81 | 68 |
|
82 | 69 |
|
83 | | - |
84 | | - |
85 | 70 | # LCR_fcn for the actual measurements |
86 | 71 | def LCR_fcn(volt_ind): |
87 | | - |
88 | | - #C=V33+3*np.random.rand() |
89 | | - |
90 | | - |
91 | | - #V_list.append(V33) |
92 | | - #C_list.append(C) |
| 72 | + # --- FIX: Removed unnecessary global declarations here --- |
93 | 73 | global v1 |
94 | | - global V_list |
95 | 74 | global output1 |
96 | | - global C_list |
97 | | - global Volt |
98 | | - #v1=[] |
99 | | - #V_list=[] |
100 | | - #output1=[] |
101 | | - #C_list=[] |
102 | 75 |
|
103 | | - #my_instrument. write( ':VOLT:LEVEL ' , str(volt_ind)) |
104 | | - my_instrument.write( ':BIAS:VOLTage:LEVel '+str(volt_ind)) |
| 76 | + my_instrument.write(':BIAS:VOLTage:LEVel ' + str(volt_ind)) |
105 | 77 |
|
106 | | - time. sleep( 5) |
107 | | - my_instrument. write( ':INITiate[:IMMediate]') |
| 78 | + time.sleep(5) |
| 79 | + my_instrument.write(':INITiate[:IMMediate]') |
108 | 80 |
|
109 | | - #output=my_instrument. query_ascii_values( ':MEM:READ? DBUF' ) |
110 | | - time. sleep( 2) |
| 81 | + time.sleep(2) |
111 | 82 |
|
112 | | - #output1=LCR.freq_sweep([freq], False) |
113 | | - output1=LCR.values(":FETCh:IMPedance:FORMatted?") |
114 | | - time. sleep( 2) |
| 83 | + output1 = LCR.values(":FETCh:IMPedance:FORMatted?") |
| 84 | + time.sleep(2) |
115 | 85 |
|
116 | | - C_list+=output1[0] |
117 | | - #v1=my_instrument.query( ':VOLT:LEVEL?' ) |
118 | | - v1=my_instrument.query( ':BIAS:VOLTage:LEVel?') |
| 86 | + C_list.append(output1[0]) # Fixed append logic |
| 87 | + v1 = my_instrument.query(':BIAS:VOLTage:LEVel?') |
119 | 88 | V_list.append(v1) |
120 | | - time. sleep( 4) |
121 | | - |
122 | | - print("Output: "+str(output1)+" | Volt : "+str(v1)+" | Cp : "+str(output1[0])+" | Loop: "+str(loop_ind_new)+" | ") |
123 | | - |
| 89 | + time.sleep(4) |
124 | 90 |
|
| 91 | + print("Output: " + str(output1) + " | Volt : " + str(v1) + " | Cp : " + str(output1[0]) + " | Loop: " + str(loop_ind_new) + " | ") |
125 | 92 |
|
126 | 93 |
|
127 | 94 | # Proto_fcn for the measurements protocol |
128 | | - |
129 | 95 | def Proto_fcn(): |
130 | 96 | global loop_ind_new |
131 | | - global protocol_list |
132 | | - loop_ind_new+=1 |
| 97 | + # --- FIX: Removed 'global protocol_list' (unnecessary for append) --- |
| 98 | + |
| 99 | + loop_ind_new += 1 |
133 | 100 |
|
134 | | - #first protocol 0 to V {A} |
135 | | - for v_ind in np.arange(0,V+V_step,V_step) : |
| 101 | + # First protocol 0 to V {A} |
| 102 | + for v_ind in np.arange(0, V + V_step, V_step): |
136 | 103 | LCR_fcn(v_ind) |
137 | 104 | loop_list.append(loop_ind_new) |
138 | 105 | protocol_list.append("A") |
139 | 106 |
|
140 | | - |
141 | | - |
142 | | - #Second protocol V to 0 {B} |
143 | | - for v_ind in np.arange(V,0-V_step,-V_step) : |
| 107 | + # Second protocol V to 0 {B} |
| 108 | + for v_ind in np.arange(V, 0 - V_step, -V_step): |
144 | 109 | LCR_fcn(v_ind) |
145 | 110 | loop_list.append(loop_ind_new) |
146 | 111 | protocol_list.append("B") |
147 | 112 |
|
148 | | - |
149 | | - #Third protocol 0 to -V {C} |
150 | | - for v_ind in np.arange(0,-V-V_step,-V_step) : |
| 113 | + # Third protocol 0 to -V {C} |
| 114 | + for v_ind in np.arange(0, -V - V_step, -V_step): |
151 | 115 | LCR_fcn(v_ind) |
152 | 116 | loop_list.append(loop_ind_new) |
153 | 117 | protocol_list.append("C") |
154 | 118 |
|
155 | | - #Second protocol -V to 0 {D} |
156 | | - for v_ind in np.arange(-V,0+V_step,V_step) : |
| 119 | + # Second protocol -V to 0 {D} |
| 120 | + for v_ind in np.arange(-V, 0 + V_step, V_step): |
157 | 121 | LCR_fcn(v_ind) |
158 | 122 | loop_list.append(loop_ind_new) |
159 | 123 | protocol_list.append("D") |
160 | 124 |
|
161 | 125 |
|
162 | | - |
163 | | - |
164 | | - |
165 | 126 | # Loop_fcn for the looping number of times |
166 | | - |
167 | 127 | def Loop_fcn(loop): |
168 | | - |
169 | 128 | for loop_ind in range(loop): |
170 | 129 | Proto_fcn() |
171 | 130 |
|
172 | 131 |
|
173 | | - |
174 | | - |
175 | | -Loop_fcn(loop) |
176 | | -#print(C_list) |
177 | | - |
178 | | - |
179 | | -my_instrument. write( ':MEM:CLE DBUF' ) |
180 | | -my_instrument. write( ':DISP:PAGE MEAS' ) |
181 | | -time. sleep( 1) |
182 | | -LCR.shutdown() |
183 | | - |
184 | | - |
185 | | - |
186 | | - |
187 | | -dict = {'Volt': V_list, 'Cp':C_list,'Loop':loop_list,'Protocol':protocol_list} |
188 | | -df = pd.DataFrame(dict) |
189 | | -df.to_csv(filename, sep=',',index=False, encoding='utf-8') |
190 | | - |
191 | | -''' |
192 | | -#ploting seprate plots |
193 | | -
|
194 | | -for plt_loop in range(1,loop+1): |
195 | | - Loop_df =df.loc[df['Loop'] == plt_loop] |
196 | | - Loop_df.plot(x="Volt",y="Cp",c="red",label=plt_loop) |
197 | | -''' |
198 | | - |
199 | | -print(dict) |
200 | | -plt.show() |
201 | | -print(df) |
202 | | - |
203 | | -# |
204 | | - |
205 | | -#plt.scatter(V_list, C_list,s=3,c=loop_list,cmap='YlOrRd') # s is a size of marker |
206 | | -#plt.plot(V_list, C_list,'-o',c=loop_list,cmap='YlOrRd') |
207 | | -#plt.plot(V_list, C_list, linestyle="-", marker="o",color='#CB4335') |
208 | | - |
209 | | - |
210 | | - |
211 | | -print("Measurements Completed and Data saved ") |
212 | | -plt.scatter(V_list,C_list) |
213 | | -plt.title("Cp vs V , Loops:"+str(loop)+" V_max:"+str(V)+" step size : "+str(V_step)) |
214 | | -plt.xlabel("V") |
215 | | -plt.ylabel("Cp") |
216 | | - |
217 | | -plt.show() |
| 132 | +if __name__ == "__main__": |
| 133 | + try: |
| 134 | + Loop_fcn(loop) |
| 135 | + |
| 136 | + my_instrument.write(':MEM:CLE DBUF') |
| 137 | + my_instrument.write(':DISP:PAGE MEAS') |
| 138 | + time.sleep(1) |
| 139 | + LCR.shutdown() |
| 140 | + |
| 141 | + # Save Data |
| 142 | + data_dict = {'Volt': V_list, 'Cp': C_list, 'Loop': loop_list, 'Protocol': protocol_list} |
| 143 | + df = pd.DataFrame(data_dict) |
| 144 | + |
| 145 | + # Ensure directory exists or save to local for safety if path fails |
| 146 | + try: |
| 147 | + df.to_csv(filename, sep=',', index=False, encoding='utf-8') |
| 148 | + print(f"Data saved to {filename}") |
| 149 | + except Exception: |
| 150 | + fallback_name = "LCR_Data_Backup.csv" |
| 151 | + df.to_csv(fallback_name, sep=',', index=False) |
| 152 | + print(f"Could not save to specified path. Saved to {fallback_name} instead.") |
| 153 | + |
| 154 | + # Plotting |
| 155 | + plt.scatter(V_list, C_list) |
| 156 | + plt.title("Cp vs V , Loops:" + str(loop) + " V_max:" + str(V) + " step size : " + str(V_step)) |
| 157 | + plt.xlabel("V") |
| 158 | + plt.ylabel("Cp") |
| 159 | + plt.show() |
| 160 | + |
| 161 | + except Exception as e: |
| 162 | + print(f"An error occurred: {e}") |
0 commit comments