Example_Powersupply_Keithly

This is an example of using the library.

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