Wolfram Computation Meets Knowledge

Using Arduinos as SystemModeler Components

Explore the contents of this article with a free Wolfram SystemModeler trial. With the new, free ModelPlug library for Wolfram SystemModeler, you can connect Arduino boards to simulations in SystemModeler. Arduinos interface easily with input and output components, so you can integrate them into SystemModeler models, for example, to operate lights, run servos, and monitor sensors, switches, and potentiometers. With the ModelPlug library, you can freely mix hardware and software components in your simulations and use the Arduino as a data acquisition board.

If you want to follow along, you can download a trial of SystemModeler. It’s also available with a student license, or you can buy a home-use license. All hardware used in this blog post can be bought for less than $50.

Set of icons

After downloading the library from the SystemModeler Library Store, installing it as as easy as double-clicking the package and accepting the license agreement. With the library, you can connect any Firmata-capable board to SystemModeler. This includes all Arduino boards.

I’m using an Arduino Uno board in this blog post. The easiest example I can think of is blinking the LED that’s on the board.

Arduino board
Arduino Uno board with internal LED highlighted

To do this, I construct a simple model with a Boolean signal that’s transmitted to the digital pin 13 on my board, where the LED is. The LED will blink with the same duration as the pulse I send in.

Blinking an LED
The model in SystemModeler using the ModelPlug components digital pin and the Arduino board

Next up I can place an LED on a breadboard and connect it to pin 9 on my Arduino:

Placing LED on breadboard
Schematic of connecting an external LED to port 9 on an Arduino Uno Board

When I connect a sine function to the analog output on my board, the real-valued signal is converted to the voltage needed at the pin where the LED is connected.

Dimming LED
A sine wave connected to a pin dimming an LED

I can now see how the LED varies between full light and no light with the sine wave. Without a single line of “Arduino coding” on my part!

If we take a step back and look at the big picture, there are basically four different scenarios we can image: one where we connect simulated input to simulated components, one where we connect simulated input to real hardware (as I did in the previous two examples), one where we connect hardware input to simulated components, and a fourth scenario where everything is in hardware.

Simulated vs. real

Below I structured the four scenarios in a grid.

Grid
Different scenarios where the ModelPlug library can be useful

The first case, where both input and components are modeled, is readily available in an out-of-the-box installation of SystemModeler, and for many uses this is all that is needed. That’s why I didn’t highlight it in the grid. When you are connecting hardware input to hardware components, it can be controlled and facilitated from SystemModeler, and signals can be filtered and processed in SystemModeler. The most interesting scenarios, in my mind, are where some parts are simulated and some parts exist in hardware. Let’s look at some examples where the components are simulated and the input comes from hardware.

Input devices and simulated model

Here I set up my Uno board to read the values from pin 14. There I’ve connected a light-dependent resistor that will read light levels.

Light-dependent resistor
Uno board with photoresitor connected to the analog input

In my model in SystemModeler, I now connect this analog signal from pin 14 to another component.

Connect analog signal to another component

This component will take the values from the light-dependent resistor and compare them to a threshold value. If the value is greater than 0.02 and the simulation time is larger than 5, it will terminate the simulation.

terminateSimulation
Compare the light value to a threshold and see if the simulation should be terminated

If I cover the light-dependent resistor with my hand for a short while, it will get the dark, the values will be lower than 0.02, and the simulation will end.

Here’s an example where both inputs and outputs are hardware: instead of using the signal from the light-sensitive resistor to terminate the simulation, I can filter it with a lowpass filter in SystemModeler. I then scale it to turn a servo between -90° and 90°. Whenever I hold my hand over the light-dependent resistor, the servo will turn, and when I release it, the servo will turn back.

Inputs and outputs are hardware
Analog signal connected via software components to a servo

This page requires that JavaScript be enabled in your browser. Learn how »

The previous example shows you that not only can you connect “hardware to hardware” via ModelPlug, but also how to realize functionality in components defined by equations. Instead of connecting resistors and capacitors in a circuit to lowpass filter the signal, or write a program that filters the signal, I use model components to do the filtering. This enables you to prototype very quickly.

To take this further, I’m going to use a bigger software model and connect it to analog input values. I’ll use a model of an inverted pendulum pushed on a cart. The complete system, including motor, gear, and 3D components for pendulum and the cart, are software models. To this model I’ll connect signals generated by an accelerometer connected to the Arduino. In the model below, the analog signal will enter from the left. The first highpass filter will filter out stationary trends in the signal, and the lowpass filter will smooth out the signal.

Inverted pendulum system with filters

At the tip of the pendulum, I’ll connect a force component that will convert the analog signal to a force pushing the top of the pendulum.

Mechanical system with force attached
Mechanical system with force attached

Finally I connect an accelerometer to an analog pin.

Accelerometer connected to inverted pendulum
Accelerometer connected to inverted pendulum

Now when I quickly move my accelerometer, a disturbance is generated and the control system for the pendulum will have to try to adapt. Can you move it fast enough to knock the pendulum down?

This page requires that JavaScript be enabled in your browser. Learn how »

The ModelPlug library is free and available for download from the SystemModeler Library Store. It works on Mac, Windows, and Linux. Try it out and let us know what you think, in the comments or in the SystemModeler group on Wolfram Community.https://content.wolfram.com/sites/39/2017/11/ArduinoBlog.zip

Download the SystemModeler Modelica (.mo) file via compressed (.zip) file.

Comments

Join the discussion

!Please enter your comment (at least 5 characters).

!Please enter your name.

!Please enter a valid email address.

6 comments

  1. This blog post really grabbed my attention. I’ve been thinking about investing in System Modeler for my school, and this may have pushed me over the edge. I teach a course for physics majors that incorporates Mathematica, Arduino, and physics experiments. Years ago it was really a course to learn LabVIEW but we never really utilized the full potential of that platform. We switched to Arduino because we felt that students could see the ease of quickly measuring signals and controlling motors/voltages/??? with the arduino platform. One big drawback is not really utilizing something like Mathematica to dynamically deal with the data in an iterative fashion. We’ve played around a little with having Mathematica talk to the arduino via the serial port, but it’s been difficult to get working flawlessly. This approach is intriguing to me, as it’s a mix of graphical programming like LabVIIEW, heavy-duty data analysis and control, and simple board interfaces. My question for other readers is whether this would be better than bullet-proofing the Mathematica/serial port/arduino approach. #NaBloCoMo

    Reply
  2. The videos are inaccessible both here and on Vimeo.

    Reply
  3. This is amazing! I will definitely start using this as soon as possible!

    Reply
  4. Amazing Post….its gonna help me a lot in my next project

    Reply
  5. Thanks for reaching out. Unfortunately I don’t have a Mega 2560 to test it. However, since ModelPlug uses Firmata, it will support as many pins the Firmata supports. By looking at the code of Firmata, seems like it supports all the pins of the Mega. A total of 54 digital + 16 analog pins https://github.com/firmata/arduino/blob/master/Boards.h#L202

    Reply