In the last two articles on Forth, I’ve ranted about how it’s beautiful but strange, and then gotten you set up on a basic system and blinked some LEDs. And while I’ve pointed you at the multitasker, we haven’t made much real use of it yet. Getting started on a Forth system like this is about half the battle. Working inside the microcontroller is different from compiling for the microcontroller, and figuring out the workflow, how to approach problems, and where the useful resources are isn’t necessarily obvious. Plus, there’s some wonderful features of Mecrisp-Stellaris Forth that you might not notice until you’ve hacked on the system for a while.
Ideally, you’d peek over the shoulder of someone doing their thing, and you’d see some of how they work. That’s the aim of this piece. If you’ve already flashed in our version of Mecrisp-Stellaris-plus-Embello, you’re ready to follow along. If not, go back and do your homework real quick. We’ll still be here when you’re done. A lot of this article will be very specific to the Mecrisp-Stellaris flavor of Forth, but given that it runs on tons of ARM chips out there, this isn’t a bad place to be.
If you catch a grizzled old radio amateur propping up the bar in the small hours, you will probably receive the gravelly-voiced Wisdom of the Ancients on impedance matching, antenna tuners, and LC networks. Impedance at RF, you will learn, is a Dark Art, for which you need a lifetime of experience to master. And presumably a taste for bourbon and branch water, to preserve the noir aesthetic.
It’s not strictly true, of course, but it is the case that impedance matching at RF with an LC network can be something of a pain. You will calculate and simulate, but you will always find a host of other environmental factors getting in the way when it comes down to achieving a match. Much tweaking of values ensues, and probably a bit of estimating just how bad a particular voltage standing wave ratio (VSWR) can be for your circuit.
In the last episode, I advocated a little bit for Forth on microcontrollers being a still-viable development platform, not just for industry where it’s usually seen these days, but also for hackers. I maybe even tricked you into buying a couple pieces of cheap hardware. This time around, we’re going to get the Forth system set up on that hardware, and run the compulsory “hello world” and LED blinky. But then we’ll also take a dip into one of the features that make Forth very neat on microcontrollers: easy multitasking.
To work!
Hardware
Mecrisp-Stellaris Forth runs on a great number of ARM microcontrollers, but I’ll focus here on the STM32F103 chips that are available for incredibly little money in the form of a generic copy of the Maple Mini, often called a “STM32F103 Minimum System Board” or “Blue Pill” because of the form-factor, and the fact that there used to be red ones for sale. The microcontroller on board can run at 72 MHz, has 20 kB of RAM and either 64 or 128 kB of flash. It has plenty of pins, the digital-only ones are 5 V tolerant, and it has all the usual microcontroller peripherals. It’s not the most power-efficient, and it doesn’t have a floating-point unit or a DAC, but it’s a rugged old design that’s available for much less money than it should be.
Programmer Connected, Power over USB
Similar wonders of mass production work for the programmer that you’ll need to initially flash the chip. Any of the clones of the ST-Link v2 will work just fine. (Ironically enough, the hardware inside the programmer is almost identical to the target.) Finally, since Forth runs as in interactive shell, you’re going to need a serial connection to the STM32 board. That probably means a USB/serial adapter.
This whole setup isn’t going to cost much more than a fast food meal, and the programmer and USB/serial adapter are things that you’ll want to have in your kit anyway, if you don’t already.
You can power the board directly through the various 3.3 and GND pins scattered around the board, or through the micro USB port or the 5V pins on the target board. The latter two options pass through a 3.3 V regulator before joining up with the 3.3 pins. All of the pins are interconnected, so it’s best if you only use one power supply at a time.
You find them everywhere from 3D printers to jet airliners. They’re the little switches that detect paper jams in your printer, or the big armored switches that sense when the elevator car is on the right floor. They’re microswitches, or more properly miniature snap-action switches, and they’re so common you may never have wondered what’s going on inside them. But the story behind how these switches were invented and the principle of physics at work in the guts of these tiny and useful switches are both pretty interesting.
If you measure a DC voltage, and want to get some idea of how “big” it is over time, it’s pretty easy: just take a number of measurements and take the average. If you’re interested in the average power over the same timeframe, it’s likely to be pretty close (though not identical) to the same answer you’d get if you calculated the power using the average voltage instead of calculating instantaneous power and averaging. DC voltages don’t move around that much.
Try the same trick with an AC voltage, and you get zero, or something nearby. Why? With an AC waveform, the positive voltage excursions cancel out the negative ones. You’d get the same result if the flip were switched off. Clearly, a simple average isn’t capturing what we think of as “size” in an AC waveform; we need a new concept of “size”. Enter root-mean-square (RMS) voltage.
To calculate the RMS voltage, you take a number of voltage readings, square them, add them all together, and then divide by the number of entries in the average before taking the square root: . The rationale behind this strange averaging procedure is that the resulting number can be used in calculating average power for AC waveforms through simple multiplication as you would for DC voltages. If that answer isn’t entirely satisfying to you, read on. Hopefully we’ll help it make a little more sense.
There are times when you might want an odd-value resistor. Rather than run out to the store to buy a 3,140 Ω resistor, you can get there with a good ohmmeter and a willingness to solder things in series and parallel. But when you want a precise resistor value, and you want many of them, Frankensteining many resistors together over and over is a poor solution.
Something like an 8-bit R-2R resistor-ladder DAC, for instance, requires seventeen resistors of two values in better than 0.4% precision. That’s just not something I have on hand, and the series/parallel approach will get tiresome fast.
Ages ago, I had read about trimming resistors by hand, but had assumed that it was the domain of the madman. On the other hand, this is Hackaday; I had some time and a file. Could I trim and match resistors to within half a percent? Read on to find out.
In an earlier article, I covered Fire Hazard Tests that form an important part of safety testing for electronic/electrical products. We looked at the standards and equipment used for abnormal heat, glowing wire and flame tests. A typical compliance test report for an appliance, such as a toaster, will be a fairly long document reporting the results for a large number of tests. Among these, the section for “Heat and Fire” will usually have the results of a third test – Tracking. It’s a phenomena most of us have observed, but needs some explanation to understand what it means.
What is Tracking ?
Tracking is a surface phenomena on an insulating material. When you have two conducting terminals or tracks at a high voltage (higher than 100 VAC) separated by an insulator, a combination of environmental factors such as dust, moisture and thermal cycling could cause minute leakage currents to flow on the surface between the conductors. Over time, the deposits carbonize and the surface current increases. Eventually, a carbon track forms over the surface of the insulator making it conductive at a particular “tracking” voltage. Finally, a short circuit is created between the two conductors which may also lead to fire. Worse, it’s possible that the tracking current could be lower than the rating of the protective fuse in the appliance, which will prevent the electrical supply from being cut off, creating a fire hazard. Tracking can be avoided by using the right kind of insulating materials and adequate creepage and clearance distances. One of the reasons for adding a slot between adjacent high voltage terminations or tracks on a PCB is to take care of tracking.
Test Standards
It’s impossible to conduct such tests according to real world conditions, so a standardized procedure is needed which can produce results that allow different materials to be compared. The IEC’s Technical sub-committee 15E was previously entrusted with the work of creating and maintaining tracking index methods and standards. Considering the importance of this standard and its wide implications, this work is now handled by TC 112 — Evaluation and qualification of electrical insulating materials and systems.
TC 112’s document IEC 60112 defines a “standardized method for the determination of the proof and the comparative tracking indices of solid insulating materials” for voltages up to 600 VAC, and provides information on how to design a suitable test equipment. The ASTM has an equivalent document — ASTM D3638 as does the UL — UL 746A-24. A more severe test is covered under IEC 60587 — “Electrical insulating materials used under severe ambient conditions – Test methods for evaluating resistance to tracking and erosion”. This test is often referred as the inclined plane tracking and erosion test and specifies test voltages up to 6 kV. But for now, let’s just look at the low voltage test as per IEC 60112.
Procedure
A sample of at least 20 mm x 20 mm with a minimum thickness of 3 mm is required for testing, with a set of five samples being tested each time. If the test product cannot provide a sample of these dimensions, then tiles of the insulating material need to be specifically produced using the same moulding process as used in actual production. The sample is supported on a horizontal glass platform. Two chisel-edged platinum electrodes are placed over the sample, separated by a gap of 4 mm. A voltage adjustable between 100 to 600 VAC is applied to these electrodes. The electrodes weigh down on the sample with a force of 1 N via dead weights.
The electrical supply to the electrodes needs to be current limited. For all voltages between 100 V to 600 V, the short circuit current across the electrodes must be limited to 1 A. This is usually done by means of a series variable resistor (rheostat). In some equipment designs, the Variac (variable auto-transformer) for adjusting the voltage is mechanically coupled to the rheostat ensuring the short circuit current is always limited to 1 A. An additional, smaller value rheostat is used for minor trimming. The standard further specifies that after setting the open circuit voltage, the measured voltage at 1 A current should not drop by more than 10% (load regulation). This makes transformer design a bit tricky. At low voltages, there isn’t enough magnetic coupling between the windings, causing higher drops at lower voltages. One solution is to use two secondary windings of about 350 V each which are connected in parallel for test voltage below 300 V, and in series for higher voltages. But there are other ways of satisfying this requirement too. It’s just one example of how the designer needs to look at every requirement in the standard and then figure out how to implement it in the test equipment.
The short-circuit current is just a limiting requirement of the electrical source connected to the electrodes. The more critical setting is the “tripping” current which needs to be set to 0.5 A above which the source must be disconnected from the electrodes. The tripping sensor needs to have a time delay of two seconds before it trips and the reason for this setting will become clear a bit later.
Environmental contamination is simulated by a salt solution — usually ammonium chloride having a concentration of 0.1%. An alternate solution is prescribed for more stringent testing. While applying the test voltage across the electrodes, one drop of the electrolyte is dropped over the test sample between the electrodes every 30 seconds for a total of 50 drops. The size of each drop needs to be adjusted such that 50 drops weigh roughly 1.075 grams and 20 drops weigh 0.430 grams. This can be achieved by careful selection of the needle diameter used for the drops as well as the delivery mechanism. Some designs use a gravity feed, solenoid operated device while others use a peristaltic pump. Another way is to use an air pump which forces the liquid out of its container by forcing air in to it. The test sample passes if it survives 50 drops without triggering the over current sensor. The sample fails if the over-current sensor gets triggered or if it catches fire, at which point the electrical supply needs to be disconnected immediately.
When a drop falls over the sample across the electrodes, most of the electrical current flows through the liquid since it is conductive. This causes a current spike that quickly boils off most of the salt solution, and generally lasts for a second or two. During this two-second duration, the over-current device is programmed not to trip. With most of the water having evaporated, some of the salt is left behind as a deposit over the sample, which causes “tracking” current to flow over its surface. A while later, you will also notice some scintillation effect (sparking) as the leftover salt crystals burn out when the current flows through them.
The results of a tracking test are reported in two different ways. A Proof Tracking Index test (PTI) is usually carried out at 175 V to confirm that the sample can survive 50 drops. On the other hand, a Comparative Tracking Index test is performed over a range of voltages, incrementing the test voltage by 25 V for each succeeding test. The number of drops is always set at 50. The CTI value is determined as the highest voltage at which the sample withstands 50 drops. In some cases, the sample must also pass the test at 25 V less than the CTI voltage for a duration of 100 drops. Depending on the CTI value, the insulator is assigned a Performance Level Category with PLC0 being the highest and PLC5 being the lowest.
It’s always fascinating looking at a sample undergoing the Tracking Index Test — check out the video below. When you look at data sheets for plastic materials, the Tracking Index value will always be reported under it’s electrical properties. Paper Phenolic, which was the PCB substrate used before the advent of fibreglass, usually has a very low tracking index value (depending on its composition), ranging between 100 V to 175 V. On the other hand, depending on composition and filler materials, fibreglass substrates such as FR4 can have CTI values ranging from 175 V up to about 300 V or higher.
If you have ever seen a PCB (not the components on it), give off Magic Smoke, then you’ve seen the effects of Tracking in action. With good design, taking into consideration proper creepage and clearance distances, it is one of the failure modes which can be prevented.