Adafruit and the Wolfram Language Microcontroller Kit
Wolfram Research, Inc.
The Wolfram Language Microcontroller Kit, which is part of the recently released version 12 of Mathematica, automates the generation and deployment of code to microcontrollers from the Wolfram Language. It supports several Adafruit development boards. So now you can take controllers, filters, or other systems models developed using the Wolfram Language and see them in action in the real world. If you are new to the Wolfram Language, a fast introduction will quickly familiarize you with the language.
To showcase the Microcontroller Kit with an Adafruit development board, I will outline a basic analysis I did with a Force-sensitive Resistor (FSR). Since the functionality is designed with an unified interface, the workflow for another board, or input, or output channel follows essentially the same paradigm.
The following are the primary components I am using: 1. Adafruit Metro Mini 2. FSR 3. 10 kΩ resistor 4. LCD 5. LCD backpack The backpack is a nifty module because is drastically reduces the wiring and the number of microcontroller pins that need to be assigned to the LCD.
In the first part of this blog, I will create a model that converts the readings that come from the sensor to weights. And in the second part, I will deploy the model to the microcontroller that will read the sensor and display the weight on an LCD.
Obtain a model
The FSR is essentially a variable resistor whose resistance drops as the load on it increases. When there is no load the sensor theoretically has infinite resistance and acts at an open switch. If we connect one end of the sensor to a voltage
and the other end to ground through a pull-down resistor, the voltage v will be 0 when there is no load. Since this is a voltage divider configuration, the voltage v will increase as the load increases, and this analog signal will serve as a measure of the load on the sensor.
So I connected the FSR as shown in the above schematic, and measured the voltage v for various weights.
First, the expression’s structure causes division by 0 at 0 V.
Although the fitted weight should just be 0 .
So I adjust the expression.
And make sure I have adjusted it only structurally.
The second problem with the model is that the weight becomes unbounded as voltage approaches 5V.
The sensor has a max load capability of 10,000 grams. So even voltage readings slightly below 5V are meaningless.
I compute the voltage reading would be for the upper limit of 9500 g.
And create a systems model that will output -1 if the weight goes over the upper limit, and the fitted weight for readings below the limit.
The model also outputs the measured voltage.
And as a quick sanity check, I plot the response of the systems model and make sure it agrees with the fitted model.
Now, that I have the model, I can move on to setting up the microcontroller specifications.
Deploy the model
The FSR and the LCD backpack are connected to the microcontroller as shown below. The backpack needs to be wired or soldered to the LCD as described in the backpack tutorial.
Before I can deploy the model I also need to specify the input and output channels that connect to the model’s input and outputs.
The input channel specification is simple. It’s an analog signal from the sensor that is read on pin A3.
To get the output to the LCD, I am using the Adafruit_LiquidCrystal library. For the Microcontroller Kit to use the library, I have to give the full path to the library.
I then go about integrating the library code into the generated code.
First, I list the files that need to be included.
Next, I create an Adafruit_LiquidCrystal object. By default, the pads A0, A1, A2 on the backpack are not soldered which results in an I2C address 0.
Next, I specify that it is a 16x2 LCD, set it up to turn on the backlight, and print the terms ‘Voltage:’ and ‘Weight:’ in the two rows.
Then I get to the code that needs to run in the loop. The first output of the model sys is assigned to voltage and printed on the first row, while the second is assigned to weight and printed on the second row.
Together they comprise the two output channels.
And now I have the complete microcontroller specification.
I connect the microcontroller to my laptop and determine the port that it is connected to. (The following works on both Mac and Linux. On Windows the port name is listed under COM ports in Device Manager.)
And I upload the code to the microcontroller.
At this point, all the microcontroller needs is power and it will display a voltage and a weight reading on the LCD.
Observations and Conclusions
The FSR voltage reading tends to drifts upwards. Since the relationship with the measured weight looks like some kind of a probit function, a small variation in the voltage will result in a much larger variation in the weight reading. This can be monitored very clearly on an LCD where the voltage reading increases ever slightly causing much larger increases in the computed weight. The derivative of the fit will tell us the change in the weight. For example, at 3.8 V, a 0.01 V variation in voltage gives a (0.01 x 1200)12 gram variation in the weight.
Yet another issue with an FSR is inconsistency. The following are several samples of measured values and their fits.
The standard deviation of the computed weights are higher at higher weights.
So while an FSR is not an accurate weighing sensor, it is useful for more qualitative things. Such as adjusting an analog output based on the weight...
Or turning on a digital output based on a threshold value...
As you can see, it is the same idiom that is repeated for various inputs and outputs, and also the targets and systems models. I have found this to be very useful when I want to test out a different target, or input or output configuration as it helps me to get the code deployed from a very high level specification and with very little effort.
Before closing, let me also mention that we haven't supported all the wonderful boards out there yet. The 1.0 release of the Microcontroller Kit supports the following Adafruit boards:
We will prioritize the boards for the next release for which there is strong interest. Please share suggestions in the comments below or email email@example.com. If there are comments about this post or the Microcontroller Kit in general do post them as well.