Node-RED Challenge Round 1: Battery Level Reporting

When I discovered Node-RED and its vast library of community contributions, I checked for several pieces of functionality that might enable several different projects ideas on my to-do list. One of them was the ability to query battery discharge level on a computer.

This was a follow-up to my earlier project assigning a Samsung 500T tablet to display an ISS track around the clock. When I last left that project, I modified the tablet so it runs on my small solar power array. But the arrangement was not optimal: the tablet is constantly pulling power from the array storage battery, instead of contributing its own battery to help store solar energy.

I had the thought to integrate that battery in an automated fashion. There are several ways it could go, but an important data point is battery discharge level. It is the key piece of information in any algorithm’s decision to charge the tablet during the day and let it run on battery at night.

This is something I knew I can accomplish by writing a bit of code. I need to find the APIs to query battery level, then I need to write code to extract that information and do something with it. None of that were technically challenging tasks, but I never allocated the time to do it.

Now I have Node-RED and its library of tools, which included node-red-contrib-battery purported to report battery information in a common way across Linux, MacOS, and Windows. If it works as advertised, all that battery query coding I kept putting off could be as simple as hooking up some nodes. It’s definitely worth trying.

Node-RED Community Contributions

Evaluated in isolation as a novel way to program computers, Node-RED scores high. I got up and running with my serial communication projects much more quickly than writing code in a UWP application, it was easy to plot a graph of data fed by my Mitutoyo SPC connector project. While I did have to climb the learning curve of a new way of thinking and a new set of vocabulary, but once I climbed it (pretty shallow, all things considered) I understood another huge attraction of Node-RED: the collection of community contributions.

I got a brief glimpse of this when I installed the Node-RED Dashboard extension, where I went in to the extension menus to search for Dashboard. The fact there was a search function implies a sizable number of extensions, so I made a note to check it out later. This was affirmed when I went to search for the serial port node, but again I put it off to later.

Returning to browse the “Flows” directory of Node-RED, I’m very excited by the extensive toolbox people have shared and made easily usable by others. This is a clear sign of a virtuous cycle at work: an attractive and useful tool attracts a seed group of users on its own merits. These users share their improvements, making the tool more useful and attractive to other users, and the cycle repeats until we have a big toolbox with contribution by people everywhere.

I queried for functionalities that I knew I would need for many projects on the hypothetical to-do list. Majority of queries came back with something that looked promising. After a few successful hits in a row, I was half expecting to find a Node-RED extension to control a webcam attached to a 3D printer carriage. Alas, no such extension existed to trivialize my current project. Fortunately, there’s a community contributed battery information node that could pick up where a past project left off, so I’ll try that first.

Arduino Interface for Mitutoyo SPC Data Port

I started looking for an inexpensive electronic indicator with digital output port, and ended up splurging for a genuine Mitutoyo. Sure it is over five times the cost of the Harbor Freight alternative, but I thought it would be worth the price for two reasons. One: Mitutoyo is known for high quality precision instruments, and two: they are popular enough that the data output port should be documented somewhere online.

The second point turned out to be moot because the data output port was actually documented by pamphlet in the box, no need to go hunting online. But I went online anyway to get a second opinion, and found this project on Instructables. Most of the information matched up, but the wiring pinout specifically did not. Their cable schematic had a few apparent inconsistencies. (Example: one end of the cable had two ground pins and the other end did not.) They also had a “Menu” button that I lacked. These may just be the result of different products, but in any case it is information on the internet to be taken with a grain of salt. I took a meter to my own cable to ensure I have the pinout described by the pamphlet in my own instrument.

Their Arduino code matched the pamphlet description, so I took that code as a starting point. I then released my derivative publicly on GitHub with the following changes:

  • Calculate distance within numeric domain instead of converting to string and back.
  • Decimal point placement with a single math expression instead of a list of six if statements.
  • Their code didn’t output if value is inch or millimeter, I added units.

A limitation of their code (that I did not fix) is a recovery path, should the Arduino falls out of sync. The Mitutoyo protocol was designed with a recovery provision: If the communication gets out of sync, we can sync back up using the opening 0xFFFF. But since there’s no code watching for that situation, if it falls out of sync our code would just be permanently confused until reset by the user.

For debugging I added the capability to output in raw hex. I was going to remove it once I had the distance calculation and decimal point code figured out, but I left it in place as a compile-time parameter just in case that would become handy in the future. Sending just hexadecimal data and skipping conversion to human-readable text would allow faster loops.

Note that this Mitutoyo Statistical Process Control (SPC) protocol has no interactive control — it just reports whatever is on the display. Switching units, switching direction, zeroing, all such functions are done through device buttons.

Once it all appears to work on the prototyping breadboard, I again soldered up a compact version and put it inside a custom 3D printed enclosure.

This image has an empty alt attribute; its file name is mitutoyo-spc-arduino-compact.jpg

Mitutoyo 543-783B Indicator with SPC Data Port

Freshly encouraged by data gathering via Node-RED with serial communications, I investigated getting another set of data points. In the packing bubble squish experiment I could see pressure data from the load cell, which showed me the bubble relaxing (and thus reducing pressure) over time. What I could not see was the physical displacement corresponding to that reduction in pressure. I assume the Z-axis carriage did not move, so the reduction likely took the form of flex in the acrylic plate. How might I measure that kind of data for future experiments?

An answer could be found from the field of machining, indicators are used to measure linear displacement precisely. How precise? In the world of machining, I can have any precision I want, but precision costs money. How precise do I want to afford for this project? I started by looking at cheap electronic indicators like Harbor Freight item #63613. ($30) But while the manual hinted at a data output port, there’s no further information about it.

I started looking further and further up the food chain and, while I could find indicators with digital output, they have the similar problem of either a poorly documented or proprietary undisclosed format. Eventually I passed the $100 mark and I started getting discouraged. I was not willing to spend that kind of money on an instrument made by a company I have not known for quality precision.

And that’s when a brand I have known for quality precision popped up in my search: Mitutoyo. I know that name well from my machining course and other precision contexts, but they have all been very expensive at several hundred dollars and up. I didn’t know they made a low-end model (with corresponding lower precision) available at around $150. Certainly many times more than the Harbor Freight item, but it is a name I trust to be precise, and popular enough that details of their data port (called SPC or Statistical Process Control port) would be documented somewhere.

For extra reassurance I decided to pay a little extra to buy from known vendor McMaster-Carr, and when it arrived I got my first surprise: the data port interface instructions were in the box! This was a great good start to a successful project connecting Mitutoyo SPC data port.

Failed Attempt At Carriage Tool Bracket

My project to squish a packing material air bubble was a simple Hello World type of exercise done with what I already had on hand. Part of this meant pushing on the bubble with bare metal plate of the retired Geeetech A10 X-axis carriage. This (probably stamped) piece of metal used to hold a 3D printing nozzle, but that component is absent when I received this gift. I don’t know the history, only that I can see a few pieces of plastic remained.

While using this plate directly was enough for an air bubble exercise, I knew I’ll eventually need to attaching something more functional to this carriage. What would that something be? I have no idea! It will likely depend on the specific project at hand, and thus highly variable.

Which naturally led to the thought of a modular system where I have a fixed base bolted to this carriage and a set of quick-switch accessories for a wide variety of tasks that can be easily swapped out as needed. I thought I could accomplish this by a little dovetail that accessories could grip onto.

Things did not go well. I made a mistake in measurement, so the bottom screw holes didn’t fit. But even ignoring that, the dovetail turned out to be far too small and my test placeholder accessories were too wobbly. There’s a lot more to an interchangeable tool head than just printing a dovetail, perhaps I should adopt an existing open source tool changer for the next draft rather than try to reinvent this particular wheel.

Tracking History of a Node-RED Project

Exploring Node-RED has been a lot of fun, working with both the freedoms and limitations of a different way of programming. It was novel enough that a very important part of software development workflow didn’t occur me until I had a nontrivial program: Without source code text files, how do I handle source control? Thankfully it turns out there are still text files behind the scenes of graphical flows, and I can use them with git just like any other software development work.

There are two options: doing it automatically via integrated git client, or manually. The procedure to setup integrated git client is outline in Node-RED documentation but I’m not sure I’m ready to trust that just yet. There’s an additional twist: it requires that I have my git credentials sitting on the computer running Node-RED, which isn’t something I’m necessarily willing to do. I keep a strict watch over the small set of computers authorized with my git credentials for software work. In contrast, a computer running Node-RED is likely only running Node-RED and not used for any other development. (This is especially true when Node-RED is only running in a FreeBSD Jail on my home FreeNAS server, or a Raspberry Pi.)

As a side note, the git integration instructions did explicitly confirm something I had suspected but hadn’t found confirmation before: “Node-RED only runs one project at any time.” This makes the FreeBSD Jail approach even more interesting, because running multiple isolated Node-RED projects on a single physical machine can be done by keeping each in their own jails.

Back to source control: for the immediate future, I’ll use a manual workflow. Under Node-RED’s main menu is the “Export” option. I can click “all flows” to include every tab, click “formatted” to make it readable, and click “Download” to receive a text file (in JSON, naturally) representing my Node-RED project.

By doing this on one of my machines configured for git access, I can put this file in a git repository for commit and push operations. Regretfully, the file is not terribly readable in its native form, and the GitHub text diff mode is cluttered with a lot of noise. There are many generated id fields linking things together, and those ID tend to change from one download to the next. However, it is far better than nothing and at least all the important changes are also visible within the noisy churn.

To verify that I could restore a project, I set up Node-RED on another computer and imported the flows. All the nodes in my visible flow appear to have survived the transition, but I’ve run into some sort of problem with the configuration nodes. Serial communication nodes lost their COM port information like baud rate, timeout, and line termination. This is odd, as I could see that information in the exported JSON. Similarly, my dashboard layout has been lost in the transition. Hopefully this is only a matter of a beginner’s mistake somewhere. For now it is relatively easy to manually restore that information, but this would quickly become a big headache as a project grows in size.

[I have no idea why anyone would want it, but if someone desires my air bubble packing material squish test flow, it is publicly available on GitHub.]

Packing Bubble Squish Test Data

I didn’t expect much out of a silly “Hello World” test of a machine that squishes packing material, but I underestimated how much of a geek I am for data. Raw numbers out of the load cell didn’t mean much, partly because it was so noisy. But since it was trivial to send raw HX711 readings to a Node-RED chart for visualization, I plotted load cell pressure data over time and was surprised at what I could see in that graph!

The most obvious thing is that we can definitely see each downward stroke of the machine represented as a sharp downward spike in the graph. After that initial shock, though, the air bubble started to relax and we can see a reduction in pressure transferred to the plate. This is a trend that I couldn’t see just looking at raw numbers flying by, and a good visual (numerical?) representation of what happens with “items may have settled in shipping”.

What I did not expect ahead of time, but was pretty obvious in hindsight, is the visible trend from one stroke to the next. The bubble bounced back incompletely when the machine released. Therefore each stroke resulted in a lower transmitted force than the last, with a degradation curve across multiple strokes that echoes the pressure reduction visible within each stroke.

So this packing bubble squish data actually turned out to be far more interesting than I initially expected, all from the happy accident of sending noisy load cell data to a Node-RED graph just because it was easily available. If I had to write my own code to graph the data, I probably would not have done it, and missed that interesting insight into the pressures of life as a packing bubble. This is a win for Node-RED.

The next challenge is to figure out how I could have captured, analyzed, and extracted that data programmatically. Human visual insight is very useful, but it requires that we think of the right way to graph data in a way that is useful. This is hard when we don’t necessarily know what we are looking for. I stumbled across this happy accident today, how might I make sure I don’t miss interesting insights tomorrow? Something to ponder…

In the meantime I have a more mundane question to answer: how do I maintain a record of work I’ve done in a Node-RED program?

Packing Bubble Squish Test

I wrote down my first impressions of Node-RED Dashboard, here I describe the project I used to explore and exercise my new tools in the Node-RED toolbox. It is a silly little thing that tests the squishiness of a plastic air bubble used as packing material. The bubble isn’t important, the main objective was to build my first nontrivial Node-RED flow and have it interface with my two recent hardware projects: the 3-axis motion control chassis of a retired Geeetech A10 printer, and the load cell kit mounted where the printer’s build plate used to be.

Both of these hardware components use USB connections that show up on the computer as serial ports, which made it easy to interface with Node-RED via node-red-node-serialport. Once installed, I had three new nodes on my palette. “Serial in” is what I needed to read the stream of data coming in from the Arduino handling the HX711 reading the load cell strain gauges. “Serial request” is what I needed for bidirectional data transfer with the 3D printer control board: sending G-code commands and reading status like position. (The third one, “Serial out”, is not applicable to this project.)

To keep the project simple, my X- and Y-axis motion are hard coded and I only hooked up a few Dashboard buttons to control my 3D printer motion in the Z-axis. This allowed me to fine tune the height of the carriage. I added buttons to remember two set heights A and B, and a toggle to automatically cycle between those positions.

I set my plastic bubble test subject down on the platform and used my flow to make the machine repeatedly press down on the bubble. Pressure reported by the load cell is sent to a Node-RED chart to graph its change over time. I was more interested in the exercise than any real results, but the graph actually turned out to be interesting.

Fast and Easy UI via Node-RED Dashboard

In addition to JSONata, there’s another very important project that is technically not part of core Node-RED. But it is rare to see one without another, and I’m speaking of the Node-RED Dashboard add-on module.

Every single Node-RED project I’ve seen makes use of the Dashboard module to build a HTML-based user interface. Which was why when I started learning Node-RED tutorials I was confused there were no mention of how to build an user interface. It took some time before I realized the Dashboard is not considered part of the core system and had to be installed separately. Once installed, there were additional nodes representing UI and additional editor interface. (Some UI to build UI…)

Once I finally realized my misconception and corrected it, I was able to build a functional interface in less than ten minutes, an amazingly short time for getting up and running under a new user interface construction system. Basic input controls like buttons and sliders, basic output controls like gauges and charts, they all worked just by connecting nodes to feed data.

However, the layout options are fairly limited. While it is extremely easy to build something closely resembling what I had in mind, I see no way to precisely adjust layout details. The rest of Node-RED reminds me of snapping LEGO pieces together, but the Dashboard exemplified the feeling: I can quickly snap together something that resembles the shape I had in mind, but if a distance is not an even number of LEGO stud spacing, I’m flat out of luck.

But even if I don’t see options for custom layout, I found instructions for building my own display widgets. Node-RED Dashboard is built on AngularJS a.k.a. Angular 1.x. I’m not sure I want to invest the time to learn AngularJS now. I can probably pick up enough AngularJS to do a custom widget if all I want is that single widget and reuse everything else. But AngularJS is currently on long-term support status receiving only security updates. Fortunately the fact Node-RED Dashboard is an add-on means people can (and have) built their own dashboard using other UI frameworks by hooking into the same extensibility mechanisms used by Dashboard. So if I want precise control over layout or other custom mechanism, I can do that while still using Node-RED as the underlying engine. I’m impressed we have that extremely powerful option.

But those dreams of grand expansion and customization are for the future. Right now I still need to build experience working with the system, which means putting it to work on a simple test project.

JSONata Reduces Need For Node-RED Function Nodes

Node-RED is built on sending messages from one node to the next. While there are some recommended best practices, it is really a wide-open system giving users freedom on how to structure the relationship between their nodes. As for the messages, they are JavaScript objects and there’s a convention to structuring them within a Node-RED project. While I can always fall back to writing JavaScript to work with messages, for the most part I don’t have to. Between the Cookbook and the Best Practices document, there are many methods to accomplish some commodity programming flow control tasks without dropping to a JavaScript function node.

But the built-in nodes have limited data manipulation capabilities. So I thought anything that requires actual data manipulation requires dropping to code. I was pleasantly surprised to find I was wrong: simple text changes and similar manipulation can be done without dropping to a JavaScript function node, they can be done with a JavaScript-based query and transformation language called JSONata.

JSONata appears to be an independent project not directly related to Node-RED, but it fits in very well with how Node-RED works and thus widely supported by many property fields of nodes. Any JavaScript string or data manipulation that could fit within a line or two of code can probably be expressed in JSOSNata, and thus accomplished directly in a standard Node-RED field without requiring dragging in a fully fledged JavaScript function node.

JSONata is yet another novelty in my exploration of Node-RED. I can vaguely sense this can be tremendously powerful, and I look forward to experimenting with this capability for future projects. But there’s another technically-not-core Node-RED feature that I will definitely be using, and that is the Node-RED Dashboard.

Node-RED Recommended Best Practices

Learning a new programming language, especially one with an entirely different paradigm, is confusing enough without having to worry about best practices. But after climbing enough of the learning curve, things quickly start getting chaotic and a little structure would help. I found this to be even more true for flow-based programming in Node-RED because a growing collection of nodes and wires connecting them can quickly grow into spaghetti code in a more literal sense than what I’ve been used to. A blank and pristine Node-RED flow doesn’t stay neat and pristine for long.

Fortunately, Node-RED documentation has a section called Developing Flows to help poor lost souls like me. It collects some basic recommendations for keeping flows manageable. And just like the Cookbook, it made more sense for me to read them after getting some hands-on experience building a bird’s nest of crossed wires and scattered nodes.

I felt sheepish to learn that I can have multiple tabs in the editor workspace. I should have noticed up top with a shape surrounding the name “Flow 1” and the plus sign to its right, but I had missed it completely. Each tab is a flow and when the project is deployed, all tabs (flows) execute simultaneously in parallel in response to their respective messages. This inherent parallelism does indeed remind me of LabVIEW.

Obviously multiple tabs make it easy to have unrelated features running in parallel, but what if they need to communicate with each other? That’s where I can use the link-in and link-out nodes. The set of link-in and link-out nodes with matching names act as wires connecting those nodes together.

They can also be used to declutter wires within a single flow. They still act the same way, but when one of the nodes is clicked, a dotted line representing the wire is visible on screen to make it easy to trace flow. Once unselected, the dotted line disappears.

A set of nodes can be combined together into a single “subflow“. In addition to decluttering, a subflow also aids in code reusability because a single subflow can be used multiple times in other flows and they all execute independently.

And finally, multiple adjacent nodes within a flow can be associated together as a group. The most obvious result is visually identifying the group as related. The editor also allows moving all the nodes in the group as a single unit. Beyond that, I don’t know if there are functional effects to a group, but if so I’m sure I’ll find them soon.

As an ignorant beginner, my first thought to flow organization most closely resembled groups. Which is why I was a little surprised to read it was added only very recently in 1.1.0. But once I read through the best practices recommendation in this Developing Flows section, I learned of all the other aspects of keeping flows organized, and I can see why groups hadn’t been as critical as I originally thought.

On the other side of the coin, as I explored Node-RED I found several other software modules that are deeply ingrained in many Node-RED projects, but aren’t technically a part of Node-RED. JSONata is one of these modules.

Node-RED Cookbook Was More Useful After Some Experience

The Node-RED User’s Guide helped me get started as a beginner on a few experimental flows of my own, slowly venturing beyond the comfort of JavaScript functions. But it was a constant process of going back and forth between my flow (which is not working) and the user’s guide to understand what I was doing wrong. I had fully expected this and, as far as beginner learning curves go, Node-RED is not bad.

On the Node-RED Documentation page, a peer of the User’s Guide is the Cookbook. I thought its subtitle “Recipes to help you get things done with Node-RED” was promising, but as a beginner I could not make use of it. It listed some common tasks and how to perform those tasks, but they were described in Node-RED terminology (‘message’ ‘flow’) which I as a beginner had yet to grasp. I couldn’t use the recipes when I didn’t even understand the description of the result.

Continuing the food preparation analogy: If I didn’t understand what “Beef Wellington” was, I wouldn’t know if I wanted to cook it, or be able to find the recipe in the book.

So to make use of the Node-RED cookbook, I had to first understand what the terms mean. Not just the formal definition, but actually seeing them in practice and trying to use them a few times on my own. After a few hours of Node-RED exploration I reached that point, and the Node-RED cookbook became a powerful resource.

I don’t know how the Node-RED Cookbook could make this any easier. It’s the kind of thing that was opaque to me as a beginner, but once I understood, everything looks easily obvious in hindsight. I stare at the cookbook descriptions now, and I don’t understand how I couldn’t comprehend the same words just a few days ago. I wish I could articulate something useful and contribute to help the next wave of beginners, because that would be amazing. But for now I can only be the beginner, consuming existing content like a Best Practices guide.

Node-RED Function Nodes Are A Comforting Fallback

Node-RED beginners like myself are given some hand-holding through two tutorials, creatively titled Creating Your First Flow and Creating Your Second Flow. After that, we are dropped into the User’s Guide for more information. The Using Node-RED section of that page covers fundamentals to get up to speed on how to work in a Node-RED project. Within that section, the page I found most instructive and informative is Using the Function Node.

Part of this might just be familiarity. A function node is a node that encapsulates a JavaScript function for doing whatever the author can write JavaScript to do. Because I’m familiar with languages like C and Python, I’m comfortable with the mentality of writing functions in source code to do what I have in mind. So seeing the function node and all I can do within it is comforting, like seeing a familiar face in a new crowd.

And just as in real life, there will be some level of temptation to stay in the comfort zone. It is probably possible to write any Node-RED program with just three nodes: an input node, a single Function node with a lot of JavaScript code, and an output node.

But writing all my logic in a single JavaScript function node would be ignoring the power of the platform. Flows allows me to lay out my program not in terms of functions calling one another, but in terms of messages flowing from one node to the next. Each node is an encapsulated representation of a feature, and each message is a piece of information that was generated from one node to inform another node on what to do next.

This is a different mentality, and it’ll probably take a bit of practice for me to rearrange my thinking to take advantage of the power of the platform. But while that transition is taking place, I expect to get occasionally stuck. But I know I can unblock myself by resorting to little pieces of JavaScript programming inside a big data flow program, and that’s a good confidence builder for me to proceed building some hands-on experience with Node-RED. I needed that experience before I could understand additional Node-RED resources like the Cookbook.

Brief Look At National Weather Service Web API

I’ve started exploring Node-RED and I like what I see. It’s a different approach to solving some problems and it’s always nice to have tools in the toolbox that would serve specific needs better than anything I had available before. The second tutorial introduced interacting with REST APIs on the web by querying for earthquake data, which was fun.

But while interesting and informative, there’s nothing I do differently after seeing earthquake data. Weather data, on the other hand, is a different story. As of this writing we’re living through a heat wave, and knowing the forecast daily highs and lows does affect my decisions. For example, whether to use my home air conditioning to pre-cool the house in the morning, which supposedly helps reduce load on the electric grid in the peak afternoon hours.

So I went looking for weather information and found a lot of taxpayer funded resources at the National Weather Service (NWS). Much of which is available as web service APIs. But in order to get data applicable to myself, I first need to figure out how to identify my location in the form of their grid system.

After a few false starts, I found my starting point (literally) in the points endpoint, which returns a set of metadata relevant to the given longitude and latitude. The metadata includes the applicable grid type as well as the X and Y coordinates corresponding to the given latitude and longitude. There are a lot of ways to get usable lat/long values, I went to the Wikipedia page for my city.

Once armed with the gridId, gridX, and gridY values, I could use them to query the remaining endpoints, such as asking for weather forecast for my grid. There’s a wealth of information here that can be a lot of fun for a future project, possibly for a smart home concept of some sort, but right now I should set aside this distraction and return to learning Node-RED.

New Exploration: Node-RED

While researching LabVIEW earlier, I came across several forum threads from people asking if there’s a cheaper alternative. I haven’t come across any answers for a direct free open source competitor, but a few people did mention that LabVIEW’s data flow style of programming had some superficial similarities to Node-RED. With the caveat they are very different software packages targeting different audiences.

Still, it sounded interesting to look into. This was reinforced when I saw Node-RED in a different context. An enthusiastic overview on Hackaday, with a focus on home automation processing data distributed via MQTT. My current project is focused on a single machine and not distributed across many network nodes, so I’m not going to worry about MQTT for the time being, but the promise of an easy way to consume, process, and visualize data is quite alluring. I’ll use my newly assembled load cell as a data source and learn how to integrate it with Node-RED.

But before that can happen, I need to install Node-RED and run through the beginner tutorials. There are many options but the easiest way for me is to install Node-RED is a community-contributed plugin for FreeNAS. This gives me an one-click procedure to install Node-RED into a FreeBSD Jail on my FreeNAS home server. And if I decide I didn’t like it, it is also a one-click cleanup.

The simplicity of setup, unfortunately, also means a lack of choice in basic configuration. For example, I have no idea how to properly secure a Node-RED instance installed in this manner.

But that’s not important right now, because the one-click plugin install has fulfilled the purpose of having Node-RED up and running for me to try beginner tutorials elaborately named “Create your first flow” and “Create your second flow“. Though partway through tutorials I got distracted by the National Weather Service web API.

Compacting Load Cell Electronics

After being pleasantly surprised by the performance of a low cost strain gauge load cell built from a kit sold on Amazon, I decided it was worth the effort of making a more compact version of the circuit. Small enough so it can be installed on the Y-axis carriage of a Geeetech A10 chassis alongside the strain gauges being read.

First of all, the prototyping breadboard had to go. It is far too large and bulky and serves no purpose once the wiring scheme has been confirmed and would actually be a source of failure if jump wires fall out. I don’t need the Arduino Nano mounted on that breadboard, either. It has two full rows of pins which I won’t need. I could spend the time to desolder those pins, but it is much easier to pull a new unit out of its box as they come without the pins. I can solder wires directly to the vias matching what I need for power, ground, data, and clock.

I did, however, need to desolder the four pins on the HX711 interface board, they are no longer necessary. Once they were removed, I could put the Arduino Nano and the HX711 board side by side and the four short wires between them.

Finally, a small 3D-printed bracket whose only purpose was to hold the two PCBs together, removing any strain from the four wires connecting the two PCBs. The idea is that I may want to explore different ways to mount this assembly, but I always need to have the two boards next to each other. Thus the motivation for a separate bracket for actual mounting to Y-axis carriage.

The Y-axis carriage clip didn’t work as well as I had hoped, but for the moment I’m not going to worry about redoing it. A little tape is enough for me to move on to the next step: feeding its data output to a computer system.

Surprising Precision and Consistency from Load Cell

I got far enough with my low cost load cell project to start receiving readings. Advertised to measure up to 200kg, I doubted it would be very precise when measuring the light weights I expect to be placed on my former 3D printer. I would have not been surprised if it consistently returned “less than 1kg” and no further. Every measurement instrument has an optimal range where it works best. Grossly exceeding that range can sometimes result in irreparable damage, but the situation usually isn’t as dire for going under. However, we shouldn’t expect very useful answers.

The test setup was far from helpful for this. I 3D-printed four rectangular blocks of plastic to hold the four strain gauges, and a thin sheet of acrylic is placed on top of them to act as surface. It was crude, but like everything else in the setup, it was just to get an idea of feasibility.

I didn’t even worry very much about accuracy. The HX711 library has various capabilities for calibration and scaling, but I skipped all of that and just dumped the raw value without conversion to any actual units. This is all I need to start characterizing behavior of this load cell. Plotting those values out on a graph, I was not surprised to see the value was pretty noisy measuring analog values within a very narrow range. However, for the most part it does stay within a certain range.

I placed the small PCB ruler I used earlier on the surface, wondering if its addition would be lost in the noise. I was surprised to see its presence was clearly visible. This item was only several grams and I did not expect it to be so clearly visible on a 200kg scale! Bringing my kitchen scale into the picture, I tried various household items to get a better feel of its sensitivity.

Empirically, values must be at least 5 grams before it would stand out from the noise, and differences should be at least 15 grams before they are reliable. This is not bad at all. However, I foresee a lot of challenges with trying to correlate raw ADC values to real units because of its sensitivity to other factors. Strain gauge readings are affected by temperature and this is especially noticeable in the middle of a heat wave. As my home heats up in the afternoon and cools down in the evening, the strain gauge average value moves in sync.

That problem might be solvable, but it’s only the first of many problem with this low cost load cell. I also observed that, at unpredictable times, the reading would be wildly (several orders of magnitude) out of range. I don’t have a good explanation, but I’m willing to tolerate it given the low price point. This is helped by how extreme those values are, making it a simple matter to ignore them.

Happy with the performance of the load cell, and satisfied that the problems I see up front is manageable, I proceeded to move the circuit off of a prototyping breadboard and onto something smaller and more permanent.

HX711 Library on Arduino Nano via PlatformIO

I’m building a strain gauge load cell kit that used a HX711 chip, and found publicly available code to interface with a HX711 in the form of a PlatformIO project. This motivated me to investigate PlatformIO. Installation was straightforward from with Visual Studio Code. I brought up the extensions marketplace, searched for PlatformIO, clicked install, and a few minutes later I was ready to go. This was a very promising start.

But while I’ve found PlatformIO to largely live up to its advertised ease of use, there were a few bumps climbing the learning curve. I typed in the simple Arduino introductory tutorial to blink the on board LED and hit Build All. That took almost half an hour as PlatformIO downloaded a whole bunch of tools and then executed them, even though they seemed completely irrelevant to my project.

After the excessively long procedure completed, I scrolled back to investigate what happened. I eventually figured out building everything meant building my Arduino sketch for every piece of hardware PlatformIO supported for use with Arduino framework. So it didn’t just build for the ATmega328P chip on my Arduino Nano, it also downloaded tools and built for SAMD-based Arduinos. Then downloaded and built for ESP32 Arduino. Then NXP, etc. So on and so forth.

And to add insult to injury, it didn’t even build for the specific Arduino I wanted to use. A little web sleuthing found this forum thread, where I learned I needed to add platform descriptor for an Arduino Nano with the “new” bootloader. But once I figured it out, I could build just for my board (taking only a few seconds this time) and upload for a blinking LED.

With that procedure figured out, I moved on to the HX711 project. Adding an entry for Arduino Nano with “new” bootloader, I was able to get it up and running to read values from my load cell.

There are a lot of other PlatformIO features I want to come back and explore later in more depth. The most exciting of which is debugger support, something sorely lacking in Arduino IDE. It also has support for ESP32, a dev board I want to spend more time exploring. Not just compile and upload, either, but infrastructure like unit test and debugging, the latter as long as I have a JTAG adapter and I don’t use the associated pins for something else.

But that is in the future. For now, this is enough of a detour into PlatformIO. With the HX711 talking to the Arduino, attention returns to the machine work surface project because I want to better understand all this data now flowing from the HX711.

HX711 Interface Library as Introduction to PlatformIO

Whenever the goal is to find something quick and easy to talk with a piece of hardware, the standard operating procedure is to search for an Arduino library for that hardware. Hence after I soldered connectors for a HX711 board my search landed at the page for an Arduino HX711 library.

There was, however, a minor twist: this Arduino library is not in the form of an INO sketch or a downloadable ZIP file for the “Libraries” folder of the standard Arduino IDE. It uses an alternative in the form of a PlatformIO project. Normally requiring a new piece of software would make me hesitate and maybe continue searching for an alternative, but PlatformIO had been on my to-do list for some time and I thought this is a good place to dip my toes in.

PlatformIO is available in several different forms, the most appealing for me is as an extension to Visual Studio Code. I’ve already been using VSCode for a few projects, even a few Arduino projects! In a strictly workaround sort of way, that is. There have been a few instances where an Arduino project got too annoying to use in the limited Arduino IDE so I copied the source file into VSCode, did my work, then copied it back into Arduino IDE for compilation and upload.

With PlatformIO installed as a VSCode extension, I shouldn’t need to do that convoluted workaround. I can build and upload directly from within VSCode. That sounded really promising earlier, but not quite enough for me to pause my project the first time. Now that PlatformIO and I have crossed paths again, I’ll take a pause for a closer look.

Connecting HX711 Amplifier ADC Board

After I finished wiring the strain gauge array for a load cell, I pulled the bundled circuit board out of its anti static bag. According to the product listing, this is built around a HX711 amplifier and analog-to-digital (ADC) converter. All that information I read earlier about putting excitation voltages into a Wheatstone bridge to interpret small changes in strain gauge resistance? All that magic is done inside this chip.

The bundle included some classic 0.1″ pitch pins to solder to the circuit board, but I thought I had a better idea. I pulled out my JST-XH connector kit and used the six-position wire-to-board unit for my strain gauge array connection. JST-XH is polarized to help ensure I don’t plug it in backwards. However, it is bigger than plain unadorned headers so it didn’t fit with the surface mount components already on the board, requiring that I mount it to the flat backside instead.

I didn’t perform the same JST-XH replacement for the digital data connection, because I wanted the flexibility to use jumper wires to connect this board to something I can use to read data from a HX711. Looking around for software libraries online, I found a HX711 library for Arduino so I pulled out my prototyping breadboard with an Arduino Nano already on board. This is as good of a starting point as any.

Four jumper wires were needed: power, ground, data, and clock. The hardware is ready to go, so I switched gears to software and today’s little plot twist of PlatformIO.