Electric Circuits Laboratory Manual: (Eecs 70B Lab)
Electric Circuits Laboratory Manual: (Eecs 70B Lab)
LABORATORY MANUAL
(EECS 70B LAB)
Instructor
Franco De Flaviis
Office EG2233
Tel. 949-8245631
email [email protected]
EXPERIMENT 6:
Controlling instruments using Python
OBJECTIVES
After performing this lab exercise, learner will be able to:
- Getting familiar with GPIB connections
- Understand and comprehend how to control lab instruments using python
- Run automated experiments
EQUIPMENT
To perform this lab experiment, you will need:
- Digital Storage Oscilloscope
- Signal generator for providing AC input to circuit
- GPIB interface adapter.
- LAN cable
THEORY/KEY CONCEPTS
GPIB (General Purpose Interface Bus) is a standard communication protocol used to connect and control
various electronic instruments, such as function generators and oscilloscopes. Python is a popular
programming language that can be used to control these instruments via GPIB using various libraries,
such as PyVISA or NI-VISA.
To connect your PC to a function generator and an oscilloscope via GPIB and control them using Python,
you will need the following:
• A GPIB interface card or adapter to connect to your computer's GPIB port or USB port.
• A function generator and an oscilloscope that have GPIB ports and support GPIB commands.
• PyVISA or NI-VISA software libraries installed on your computer.
Another way of communicating with an instrument is LAN. Local Area Network is a computer network
that spans a relatively small area, such as a single building or a group of buildings. LANs are typically
used to connect devices within an organization or a geographic location, and they can provide fast and
reliable communication between devices on the network.
To connect a function generator or an oscilloscope to a LAN, you will typically need to configure the
instrument to use a specific IP address and network settings, such as subnet mask and default gateway.
Once the instrument is connected to the LAN, you can communicate with it using the TCP/IP protocol,
which is a widely used communication protocol on LANs.
Once you have these components, you can follow the steps below to control your instruments using
Python.
1. Connect the GPIB interface card or adapter to your computer's GPIB port or USB port.
2. Connect the GPIB interface card or adapter to the GPIB port on the function generator and the
oscilloscope using GPIB cables.
3. Power on the function generator and the oscilloscope.
4. Install the PyVISA or NI-VISA software libraries on your computer.
5. Open a Python environment, such as Anaconda, Jupyter Notebook, or IDLE.
6. Import the PyVISA or NI-VISA library in your Python script.
7. Use the library to establish a connection to the function generator and the oscilloscope using their
respective GPIB addresses. You can find the GPIB address of the instruments in their manuals or
on their front panels.
8. Send GPIB commands to the function generator and the oscilloscope using the library. You can
use the manuals of the instruments to find the appropriate commands, or you can use Keysight
Command Expert.
Keysight Command Expert is a software tool developed by Keysight Technologies that enables easy
and fast instrument control for a wide range of instruments using various communication protocols,
including GPIB, USB, LAN, and serial.
With Command Expert, users can easily send commands and queries to their instruments using a
simple and intuitive graphical user interface (GUI). It supports multiple programming languages,
including Python, MATLAB, C#, and Visual Basic, and provides a library of instrument-specific
commands and syntax that can be easily accessed and used in programming.
The Command Expert tool also provides features for instrument discovery, connection setup, and data
visualization, making it a versatile and powerful solution for controlling and automating instrument
tests and measurements in a variety of applications and industries.
9. Close the connection to the function generator and the oscilloscope using the library.
This example code connects to a function generator and an oscilloscope, turns on the output of the
function generator, turns on the display of the oscilloscope, measures the frequency of the input signal,
and reads the frequency measurement from the oscilloscope. Finally, it turns off the output of the function
generator and closes the connections to the instruments.
here are some steps on how to use the IDLE environment for Python:
1. Install Python: You can download and install the latest version of Python from the official Python
website: https://www.python.org/downloads/
2. Install IDLE: IDLE is included with Python, so once you've installed Python, you should have
access to IDLE.
3. Launch IDLE: To launch IDLE, simply open the Python application and select "IDLE" from the
menu.
4. Create a new file: In IDLE, you can create a new Python file by selecting "File" > "New File"
from the menu.
5. Write code: Once you have a new file open, you can start writing Python code. IDLE includes
features such as syntax highlighting and auto-completion to make it easier to write and debug
your code.
6. Run your code: You can run your code in IDLE by selecting "Run" > "Run Module" from the
menu, or by pressing the F5 key on your keyboard.
Here's a brief overview of how to use Matplotlib in Python:
• Import the Matplotlib library: To use Matplotlib in your Python script or Jupyter notebook, you
need to first import the library. This can be done with the following line of code:
This line imports the pyplot module of the Matplotlib library and gives it an alias plt, which is commonly
used in Matplotlib code.
• Create some data: Before you can create a plot, you need some data to plot. You can create data
in a variety of ways, such as by generating random numbers or by loading data from a file.
• Create a plot: Once you have some data, you can create a plot using the plot function of the
pyplot module. For example, to create a simple line plot, you can use the following code:
This code adds a title to the plot (My plot) and labels to the X and Y axes.
• Save the plot: Finally, once you have customized your plot to your liking, you can save it to a file
using the savefig function. For example, to save the plot as a PNG file, you can use the following
code:
This code saves the plot as a file called my_plot.png in the current directory.
These are just the basics of using Matplotlib in Python, but there's a lot more you can do with this
powerful library. I recommend checking out the Matplotlib documentation and examples to learn more.
PRELAB:
1. Install Python environment IDLE on your laptop.
2. For question 2 of HW #5, use Python to plot Bode plots of magnitude and phase.
Connecting an instrument to PC using GPIB, PROCEDURES:
STEP 1: find GPIB Address:
Power on the oscilloscope (or AFG) and press the "Utility" or "System" button on the front panel and then
Look for I/O option.
Utilility---> I/O
STEP 2: open Keysight Command Expert and click on the "New Instrument" button on the
toolbar
STEP 3: Select an Instrument
After the connection is established, you can use the various instrument commands and syntax provided by
Command Expert to control your oscilloscope and perform various tests and measurements.
Connecting an instrument to PC using LAN, PROCEDURES:
STEP 1: find IP Address:
Power on the oscilloscope and press the "Utility" or "System" button on the front panel and then Look for
I/O option.
Utilility---> I/O
STEP 2: open Keysight Command Expert and click on the "New Instrument" button on the toolbar
STEP 3: Select an Instrument
STEP 2: Select ‘Autoscale’ from the toolbar and click on ‘Add Step & Execute’
STEP 3: Select ‘MEASure’ from the toolbar and choose ‘VPP’ then click on ‘Add Step & Execute’