Visualize Robot Status Using a Programmable blink(1) RGB LED

ROS2JsGuy
6 min readJun 30, 2021

--

This article introduces the programmable blink(1) USB RGB LED and develops a simple cross platform TypeScript/Nodejs program that can be used to visually communicating a robot’s state on a microcontroller such as the Raspberry Pi.

blink(1) on a Raspberry Pi

Recently I began developing a small self balancing robot powered by a Raspberry Pi 4. As I have added more features and robustness to the design I found that I needed a way to visually identify various states of the robot’s operation such as power on, initializing, running, error, and obstacle detection. Initially I planned to use several cheap LEDs. But the amount of free space on my breadboard had become pretty tight. While considering my options I remembered the old blink(1) RGB LED that my company gave me a few years ago as part of an internal project. The blink(1) is colorful, very bright and noticeable and easy to control programmatically LED — the perfect solution for my project and I hope your projects as well.

blink(1) RGB LED

If you’ve never seen a blink(1) it is a cool programmable RGB LED that plugs into a USB port and works on most devices and OSSes. ThingM, the company behind the blink(1) device, describes it like this:

blink(1) packs three dimensions of information (color, brightness and pattern) into a single tiny package that fits into any USB port on nearly every device. It makes it incredibly easy to connect any data source in the cloud or on your computer to a full-color RGB LED so you can know what’s happening…

The blink(1) is available from ThingM, Amazon and several other regional distributors for approximately $30 USD.

Controlling a blink(1) LED from TypeScript & Nodejs

Let’s look at how easy it is to program a blink(1) LED by developing a small program that uses colored light patterns to communicate the state of a robot system. We will walk through the steps for creating an example program named blink1-status-led using TypeScript and Nodejs coding technologies. I’m using these technologies as this is the coding environment in which I have implemented my robot control logic and device drivers. The program will run on Linux, Mac and Windows OSes and most hardware. For example, I evolved this program and deployed on a Raspberry Pi 4 running Ubuntu Linux 20.04 and a blink(1) device.

Prerequisites

  • blink(1) LED device
  • Nodejs version 12 or greater
  • Computer or microcontroller running Windows, Mac or Linux
  • Ensure blink(1) operates correctly.
    Before starting to code, ensure that your blink(1) LED is functioning properly. The Blink1Control software utility from ThingM can be helpful in this task. There are versions for both Windows and Mac. Also be sure to close any program that may be accessing your blink(1) before attempting to run our blink1-status-led program.
  • Linux only configuration.
    If you plan to interact with a blink(1) on a Linux system, the system may require USB and HID configuration. installations your OS may need configuration. For more information see Linux Users section here.

TypeScript/Nodejs Project Setup

Our first task is to create a Nodejs project configured with TypeScript tools and JavaScript dependency libraries.

From a terminal begin by creating a new folder named blink1-status-led. This folder will hold all of our project files and source code. The shell commands that follow should work on Mac, Linux and Windows.

mkdir blink1-status-led

Change the active directory into the new folder.

cd blink1-status-led

Next let’s create a Nodejs project initialized with defaults and TypeScript tools.

npm init -y
npm install typescript @types/node @tsconfig/node12 --save-dev

Now install the node-blink1-async module.

npm install @ros2jsguy/node-blink1-async

I created the node-blink1-async package to support coding blink(1) LEDs using a modern async JavaScript api and to simplify multi-step display patterns such as blinking the led. If you would rather use a callback style api take a look at the node-blink1 JS library from which node-blink1-async is based.

Now let’s setup our project’s TypeScript compiler options. Create a file named tsconfig.json and add the following code to it.

{
"extends": "@tsconfig/node12/tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": [
"blink1-status-led.ts",
"index.ts"
],
"exclude": [
"dist/**"
]
}

The contents of your blink1-status-led folder should look similar to this:

blink1-status-led/
node_modules/
package.json
tsconfig.json

Let’s Code

Our program will be divided across 2 small TypeScript files that we will create in this section.

blink1-status-led.ts

From the blink1-status-led folder open your preferred TypeScript coding tool (e.g. Visual Studio Code, …) and create the file blink1-status-led.ts. In this file we will create the TypeScript class, Blink1StatusLedthat will command a blink(1) LED to display a specific light color and pattern for each state such as:

  • power on —solid red light
  • initializing — medium blink yellow light
  • running — solid green light
  • error — fast blink red light
  • power off — no light

Our design can control multiple blink(1) devices by identifying the led device to control when creating a Blink1StatusLed instance. If you have a single blink(1) device then it it unnecessary to provide it’s serial-number as a parameter to the Blink1StatusLed constructor as it will be automatically detected and used as the default blink(1) device.

Note that the Blink1StatusLed methods are asynchronous and use the node-blink1-async library. You can find documentation for the node-blink1-async here.

Our design is very simple. When a state is set on a Blink1StatusLed instance, the instance remains in that state with constant visual output until the state is changed externally. We will see how this works when we implement the index.js file next.

Of special note is the delay(millis: number) method which is provided as a convenience to support synchronous-like execution of multiple async Blink1StatusLed methods. For example if you needed to limit the duration of a blinking pattern to 5 seconds then do something like this:

async function shortYellowBlink(){
const blink1StatusLed = new Blink1StatusLed();
await blink1StatusLed.blink(255, 255, 0, BlinkRate.FAST);
await blink1StatusLed.delay(5000); // 5000 milliseconds == 5 secs
await blink1StatusLed.off();
}

index.ts

Next we will create the index.ts file to drive our Blink1StatusLed through it’s various states.

Create a file named index.ts with the following src code.

This main() function systematically runs the Blink1StatusLed instance through each of its states. As the program executes you should observe your d blink(1) device visually illuminate and transition from state to state as it outputs red, yellow and green color light in solid and blinking patterns.

Build and Run

Before we can run our program we need to compile our 2 TypeScript files using the TypeScript compiler, tsc. Run the following command in your terminal:

npx tsc

The TypeScript compilation process should take only a few seconds to complete. Upon completion you should see a new folder named dist containing JavaScript files with the same name as our TypeScript files. Here’s the full list of our project files:

blink1-status-led/
blink1-status-led.ts
index.ts
dist/
blink1-status-led.js
index.js
node_modules/
<...dependency modules...>
package.json
tsconfig.json

Now run the program using Nodejs and observe your blink(1) device color and blink pattern display. If you are on Windows use the \ path separator.

#linux
node dist/index.js
#windows
node dist\index.js

In addition to the blink(1) visual output here’s the console output from the program.

Wrapping Up

That’s it! I hope you’re inspired to add more visual state information to your robot and that a blink(1) RGB LED becomes a new option for your projects. There are many more uses for a blink(1) than the small set we have demonstrated in this article. For example, a blink(1) could be used to indicate navigation information with an orange blinking pattern that increases in blink rate and intensity as the robot nears an obstacle or waypoint.

Lastly if you like this article please give it a Thumbs Up and a share on twitter. You can reach me on Twitter at @ros2jsguy and find code that I’ve developed or working on at my GitHub account: https://github.com/ros2jsguy Looking forward I’m planning a part-2 of this article describing how to use a blink(1) with the Robot Operating System (ROS). Until then happy coding!

References

  1. node-blink1-async npm information
  2. node-blink1-async GitHub repository
  3. node-blink1-async TypeDocs
  4. ThinkM blink(1) information

--

--

ROS2JsGuy
ROS2JsGuy

Written by ROS2JsGuy

“ROS 2 for JavaScript & TypeScript Developers” tutorials from a guy named Wayne

No responses yet