Example_Powersupply_Keithly
This is an example of using the library.
1# %% ==========================================================================
2# Import and Definitions
3# =============================================================================
4import datetime
5import time
6
7import matplotlib.pyplot as plt
8import numpy as np
9import pandas as pd
10from tqdm import tqdm
11
12# Instrument Libraries Github: https://github.com/MartinMiroslavovMihaylov/Python_Instruments_Automation_Scripts
13# Install with:
14# pip install git+https://github.com/MartinMiroslavovMihaylov/Python_Instruments_Automation_Scripts.git
15# from Instruments_Libraries.KEITHLEY2612 import KEITHLEY2612
16from Instruments_Libraries.InstrumentSelect import SourceMeter
17
18# %% ==========================================================================
19# Select Instruments and Load Instrument Libraries
20# =============================================================================
21# myKEITHLEY2612 = KEITHLEY2612('COMXX') # noqa: N816
22myKEITHLEY2612 = SourceMeter() # noqa: N816
23myKEITHLEY2612.reset()
24
25# %% ==========================================================================
26# Setup the Measurement
27# =============================================================================
28num_of_points = 10
29sleep_time = 0.5
30vcc_ch_num = 'a' # channel for VCC
31vcc = 4.7 # V
32vcc_current_limit = 0.140 # A
33
34# %% ==========================================================================
35# Configure the Instrument
36# =============================================================================
37myKEITHLEY2612.set_voltage(vcc_ch_num, vcc)
38myKEITHLEY2612.set_current_limit(vcc_ch_num, vcc_current_limit)
39
40# or convenience method: Configure Instrument as Voltage Source (measuring current)
41myKEITHLEY2612.setup_voltage_source(vcc_ch_num, vcc, vcc_current_limit)
42
43# %% ==========================================================================
44# Measurement
45# =============================================================================
46myKEITHLEY2612.set_output(channel=vcc_ch_num, state='ON') # turn on the channel
47
48records = [] # Empty list to store data and meta data
49for idx in tqdm(range(num_of_points)):
50 rec = {} # single record
51 rec["VCC_Voltage"] = myKEITHLEY2612.measure_voltage(vcc_ch_num)
52 rec["VCC_Current"] = myKEITHLEY2612.measure_current(vcc_ch_num)
53 rec["Timestamps"] = datetime.datetime.now()
54 records.append(rec)
55 time.sleep(sleep_time)
56 temp = idx*np.pi # do something with idx
57
58# %% ==========================================================================
59# Create Dataframe
60# =============================================================================
61meas_df = pd.DataFrame.from_records(records)
62
63# %% ==========================================================================
64# Plot the Measurement
65# =============================================================================
66t0 = meas_df["Timestamps"].iloc[0]
67relative_time = (meas_df["Timestamps"] - t0).dt.total_seconds()
68
69plt.plot(relative_time, meas_df["VCC_Current"])
70plt.xlabel('Time (s)')
71plt.ylabel('Current (A)')
72plt.show()
73# %% ==========================================================================
74# Save Dataframe
75# =============================================================================
76# Save DataFrame to HDF5 (better than CSV)
77meas_df.to_hdf("measurements.h5", key="data", mode="w")
78# key="data" is like a "dataset name" inside the HDF5 file
79# (you can store multiple DataFrames in one file with different keys).
80# mode="w" overwrites the file. Use mode="a" if you want to append new datasets.
81
82# Later: Load it back
83loaded_df = pd.read_hdf("measurements.h5", key="data")
84print(loaded_df.head())
85
86#or
87
88# Save DataFrame to CSV
89meas_df.to_csv("measurements.csv", index=False)
90
91# Load it back, auto-parsing the "Timestamps" column as datetime
92loaded_df = pd.read_csv("measurements.csv", parse_dates=["Timestamps"])
93print(loaded_df.head())
94
95# %% ==========================================================================
96# Close Instrument
97# =============================================================================
98myKEITHLEY2612.Close()