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.
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 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.
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:
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.
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.
Below I structured the four scenarios in a 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.
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.
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.
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.
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.
Analog signal connected via software components to a servo
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.
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
Finally I connect an accelerometer to an analog pin.
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?
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://blog.wolfram.com/data/uploads/2017/11/ArduinoBlog.zip