Control a B&K Precision Power Supply in Python

Control a B&K Precision Power Supply in Python

View the app user interface

View the code on GitHub

This Dash DAQ app controls a B&K Precision power supply using a clean and functional UI, written in just over 300 lines of Python code.

You can play with a demo version of the app to get a feel for a Dash DAQ’s slick virtual controls.

Connecting this app required a USB connection from a computer to an RS232 converter, which is then connected to the power supply.

To write the app, we import these packages:

We have to define our layout before we proceed. For this app, there are two containers. The first visualizes the top part of the power supply using Dash DAQ LEDDisplay components:

The second visualizes the bottom of the power supply device, with Dash DAQ PowerButton, NumericInput, and StopButton components:

We can easily wrap these around a DarkThemeProvider component that comes with Dash DAQ.

After creating the layout, we can start working on the callbacks for these components. Here is a callback from the app used to send a voltage input command to the driver:

The above callback is called when you press the button in the app. Our callback relies on the state of the radio buttons, as well as the input in the numeric input component, when the button is pressed. We use these values to send the appropriate command to the driver by calling the specific function. After we do this, we have to update our timer to make sure that the LED displays are updated as well.

Let’s take a look at how the LED displays are updated:

The fetch_data function acts on the interval component or the power status, then fetches all the relevant values from the power supply. After fetching these values, we store these in a hidden container so that the displays can be updated. This is seen in the update_max_current function, which is called when our hidden container is updated, and then sets the LED display value with this information.