Control an LED Strip in Python

Control an LED Strip in Python

View the app user interface

Team Plotly is getting colorful with Dash DAQ! This application controls the colored LED lights in a BlinkStick.

Using Dash DAQ components like ColorPicker, Slider, and others, we created a virtual control panel. We even wrote a Rainbow mode!🌈

Check out the demo app, and read how we did it below.

The setup required a simple USB connection between the BlinkStick and the USB port of a PC. The BlinkStick API was used to control this piece of hardware.

Before creating this app, we had to import the dash_daq, dash and blinkstick libraries:

Using the BlinkStick API, we open a serial port to communicate with the BlinkStick:

Dash DAQ’s versatile UI components make the app layout a breeze to put together. In this simple block of code, we used BooleanSwitch, PowerButton, and NumericInput components to control multiple parts of the BlinkStick:

Using Dash callbacks, we created a simple system to control and color the lights. We can turn on and pick the color of one specific LED, or all of them. We can select a range of lights with our slider mode, and we also created a rainbow color mode.

Eight Dash DAQ Indicator components simulate the BlinkStick’s LEDs in the app’s GUI. The callback below controls how each Dash DAQ component affects the color of an indicator component.

The bstick.set_color() function turns off an LED on the BlinkStick device.

When the Dash DAQ boolean switch component is turned on, where all_switch is True, the Dash DAQ color picker sends an RGB color code contained in a dictionary. An RGB code can be assigned to the bstick.set_color() to manipulate the color of the physical BlinkStick in real time.

The for-loop allows us to change each LED in the BlinkStick at the same time. The RGB color code is converted to a Hex code and returned to the output ID color-send. This empty div is used to store the Hex color code, and returned to the Dash DAQ Indicator color parameter, changing the color of the indicators in GUI.

Turning on the Dash DAQ Boolean rainbow switch sends a preset rainbow spectrum to the GUI and the physical BlinkStick, making use of the bstick.set_color() function. This callback follows the same methodology of the all_switch callback, above.

When nothing is selected, the physical BlinkStick is turned off by sending it an RGB color code of (0, 0, 0) or black. The eight indicators on-screen are also turned off.

In order to prevent conflicting control commands, we made many callbacks to disable Dash components unless certain conditions were met. The code below disables other commands when a command is already selected.

This app is a simple example of the possibilities Dash DAQ offers. Dash DAQ makes it simple and easy to create an interactive GUI to control all sorts of hardware.

Play with this app online, or purchase a Dash DAQ license to get the full source code for this app and the others!