Modal Title
Culture / DevOps Tools / IoT Edge Computing

Off-The-Shelf Hacker: Simulate Sensor Data with an Arduino

May 25th, 2019 6:00am by
Featued image for: Off-The-Shelf Hacker: Simulate Sensor Data with an Arduino

Capturing real-world measurements with a sensor and an Arduino microcontroller, then pushing those readings over to a Linux computer for display on an analog gauge program or other usage is fairly common now.

We read the sensor, scale the value to something practical, then print the data out to the USB port. A companion program, on Linux display machine, can suck in the data and do something like spin the needle of an on-screen analog gauge according to the sensor readings.

Occasionally the sensor isn’t available yet. It could be on the proverbial slow boat (or plane) from China or something. The good news is that you can “simulate” data with a minimal bit of code on the Arduino sensor device. That frees you up to develop your gauge or another program on the Linux machine while you wait for the sensor to arrive. Your gauge program thinks it’s reading real sensor data.

This week we’ll look at a few simple ways to simulate data for on-screen gauges, readouts, dials and other uses.

Super Fast Hardware Data Simulation

You can actually build a bare-bones hardware data simulator on an Arduino in a matter of minutes. Write some code to print a string of values to the serial port, upload it to any old Arduino you have lying around and when it reboots start reading the data on the other end of the USB cable. Here’s some brute-force code that sweeps values up and down, from zero to 100, in increments of 10, at 1/2 second intervals.

This literally took five minutes to write, compile and upload. Better yet, you only need an Arduino and a USB cable.

Arduino NG generating simulation data and sending it to the Linux notebook

Checking simulation data operation is easy, on the Linux computer side, using the cat command. We’ve used this technique often. Out of habit, I run the stty command first to make sure the USB port is acting right. Execute these in a Linux terminal.

While testing the thermocouple project last week, I could only change the temperature a few degrees, by grabbing the end of the probe or sticking it in some hot water. The device can measure temperatures up to 900 degrees Fahrenheit. A more comprehensive test might have provided data at the upper and lower limits, rather than just a narrow range at the lower end. I could have used a hardware data simulator.

Slightly More Sophisticated Approach

A slightly more sophisticated simulation single-steps from zero to 100 at half-second intervals. This creates a much smoother gauge needle sweep. When the value hits 100 it just jumps back to zero after a one-second delay then repeats, over and over.

This code is simple and effective, as well.

Certainly, we’d need to scale the sim data and the Processing gauge program to handle the 0 to 900 thermocouple temperature range, along with an appropriate change to the dial face on the display machine side. You get the idea.

Expanding the Model

There are lots of cool things you can do with “sim data” capability.

Maybe you have multiple gauges that read multiple sensors. You could package the values on a single output line, using commas as a data separator. Of course, on the gauge display program side, we’d need to parse each data line into their individual values and send them to their appropriate gauge routines.

You could use smaller or larger steps for different gauge resolution. Smaller values would sweep the needle more smoothly and slower. Maybe you’d want to drop the delay values or range of values for faster response. How about using a trig function to control the “i” value for a wave-like data stream?

And, we aren’t limited to simulating number data. We can just as easily send text test data. Simply enclose the text in double quotes, in the Arduino code. For example:

Next Steps

We’ve only scratched the surface of using an Arduino to send simulation data over the USB cable. Readers could easily take an Arduino Nano and build a tiny “sim” or diagnostic device that fits in a little case. Nanos are pretty inexpensive. You might even use a couple of potentiometers and switches to change the output data without having to re-upload the Arduino firmware each time. Lots of possibilities here.

Catch Dr. Torq’s Off-The-Shelf Hacker column, each Saturday, only on The New Stack! Contact him directly for consulting, speaking appearances and commissioned projects at or 407-718-3274.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Shelf, Torq, Simply.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.