Control a Wireless Arduino Robot in Python

Control a Wireless Arduino Robot in Python

View the app user interface

View the app code on GitHub

We love our robots here at Plotly! This Dash DAQ app wirelessly controls Sparki, an Arduino-based robot 🤖

Dash DAQ gives you clean, intuitive virtual controls to build GUIs for your hardware. We’ll focus on the Dash DAQ Joystick here, but in the demo app you’ll see dials, buttons, and graphs.

This application allows you to control Sparki’s movement, ultrasonic sensor position and reading, piezometer frequency, and RGB LED light.

The imports used are shown below, and can be described as PySerial, NumPy, Pandas, Plotly, and JSON:

Sparki comes with a bluetooth module, so it can be controlled wirelessly from a laptop. We used PySerial to connect via Python to the HC-05 bluetooth module.

Dash DAQ’s versatile UI components make the app layout a breeze to put together. We implement a joystick and Dash DAQ Knob component below:

In order to control Sparki’s Arduino system in Python, we need to format a long string into ASCII and send it via PySerial. We’ll create a string-parsing system to accomplish this.

The initial command string looks something like this: “<command, case_number, beep_frequency, R, G, B, ultra, head>.” Once formatted, this is the only command being sent over the serial port, giving the user a simple and quick way to interface communication between the two devices.

For example, to move Sparki, the user would move up on the joystick, and this would initialize the command to “UP”, and case_number to “1” and be sent to the Arduino, causing Sparki to move. Case numbers were used to reduced redundancy in code on the Arduino side, where the case number is correlated with a case statement in the C++ code.

To move Sparki, the user would move the joystick in a direction — upwards, for example. In our app, this initializes the command to “UP” and case_number to “1”. The command is then sent to Arduino. (Sparki’s Arduino system uses case numbers to reduce redundancy in code. Case numbers are correlated with a case statement in the C++ code.)

Merging the capabilities of Dash DAQ, Plotly, Bluetooth, and Arduino, we were able to make Sparki swivel it’s head 180 degrees, measuring the distance of surrounding objects at every ten-degree angle. We captured 18 data points this way, and sent them from Arduino back to Python, where we graphed them on a Plotly polar chart.

We also did some funky data manipulation, using NumPy to transform the data array into a large string and delimit the string. We then used Pandas to organize the data in usable pieces, to be graphed.

Using the callback below, we take point-and-shoot ultrasonic sensor readings. Python sends a command string to Arduino and receives a serial data response. The line “ser.readline().decode(“ASCII”)” reads and decodes the serial data on the Python side.

Dash DAQ makes it easy to create an interactive GUI to control all sorts of hardware, like the Sparki robot.