LinuxCNC · The HAL, chapter 1
The LinuxCNC HAL, chapter 1: the electrical cabinet inside your computer
The HAL decides what your machine does when LinuxCNC asks for something: which output switches on, which input stops the machine, what waits for what. It looks like black magic and it isn't. It's an electrical cabinet, with its devices, its terminals and its wires, only it lives inside the computer. In this first chapter I show it to you with the real configuration of my EMCO F1.
Published on 30 November 2026.
The first time I opened the .hal file that PnCconf generates, I stared at it for a good while. Hundreds of lines with names like hm2_7i76e.0.7i76.0.0.input-03, arrows pointing one way and the other, strange words. I closed it and got on with my life. Until I wanted my mill to do something PnCconf can't do, and I had no choice but to understand it.
And when I understood it, it made me smile, because I spend my days among electrical cabinets: at the lift factory we build lift controllers, which are cabinets full of relays, timers, terminals and numbered wires. The HAL is exactly that. If you can read a wiring diagram, you can already read a HAL, even if you don't know it yet. And if you've never seen a diagram in your life, don't worry: I'll explain it with everyday things.
This is a series of chapters, and this first one is the foundation: what each thing is, how it connects and in what order it runs. At the end we'll read together a real piece of my F1's HAL that makes the mist coolant pulse, and you'll see you already understand all of it.
The idea: an electrical cabinet inside the computer
HAL stands for Hardware Abstraction Layer. The name is scary, but the idea is simple: LinuxCNC knows nothing about your machine. It doesn't know whether you have a Mesa or a parallel port, which terminal you wired the X limit switch to, or whether your spindle has a VFD or a servo. It only knows how to do its own things: calculate toolpaths, interpret G-code, say «spindle to 3000 rpm now» or «coolant now».
The HAL is the cabinet in the middle. On one side are LinuxCNC's devices, which ask for things and report. On the other, your card's, which switch outputs and read inputs. And between them, the wires you run. If you run the coolant wire to output 1, the coolant comes out of output 1. If you want it to go through a timer first, you put a timer in between. That simple, and that powerful: with the devices that come with LinuxCNC you can build almost any logic, without soldering anything.

There's one detail that sets it apart from a real cabinet, and it's very important: HAL devices don't work on their own. In a cabinet, a relay reacts as soon as current reaches it. In the HAL, each device stays still until its turn comes. Someone walks past all of them, one after another, always in the same order, and each time they pass, each device looks at its inputs and updates its outputs. That someone is a thread, and we'll look at it calmly further down, because it's the key to almost everything.
The six words you need
The whole HAL can be explained with six words. Here they are next to their counterpart in the electrical cabinet and a real example from my machine:
| In the HAL | In the cabinet | Example on my F1 |
|---|---|---|
| Component | A device: a relay, a timer, a contactor | td_mist, a timer |
| Pin | A terminal on the device | td_mist.out, its output |
| Signal | A wire, with its number painted on | coolant-flood |
| Parameter | The adjustment dial you set once | spindle_near.difference |
| Function | What the device does when its turn comes | pid.x.do-pid-calcs |
| Thread | The round that goes past every device | servo-thread, every millisecond |
Let's go through them one by one, no rush.
The component is a black box that does one thing. An AND gate, an inverter, a timer, a PID, LinuxCNC itself (which is just another component, called motion) or the driver for your Mesa card. You don't need to know what's inside, just as you don't open a relay to use it: it's enough to know which terminals it has and what it does.
The pin is a terminal on that device. Each component comes with its own, with a fixed name. The timer td_mist has an input td_mist.in, an output td_mist.out and a couple of pins for the times. Look at the name: first the device, then a dot, then the terminal. It's like a wiring diagram, where you write «KA1 terminal 13».
The signal is the wire. In wiring diagrams, each wire carries a number so you know what it is; in the HAL it carries a name you choose, like coolant-flood or x-neg-limit. One wire can go to several terminals, like a terminal strip. And something that took me a while to see: two pins are never joined directly, there is always a signal in between. Just as in a cabinet you can't join two terminals without a piece of wire.
The parameter is a setting. The stairwell light timer has a little dial to say how many minutes it stays on: you turn it once when you install it and forget about it. That's a parameter. On my F1, spindle_near.difference is 0.5: it's the tolerance for deciding that the spindle has reached its speed.
The function is the job the device does each time its turn comes. Almost all components have just one; some, like the Mesa one, have two: one to read the card and another to write to it.
The thread is the round. We'll see it in its own section, because it deserves one all to itself.
What travels down the wires
Not every wire carries the same thing. One may carry a «yes or no», another a speed with decimals, another a number of turns. The HAL has four types and won't let you mix them, just as you wouldn't plug a hose into a socket:
| Type | What it carries | Around the house | Real example |
|---|---|---|---|
| bit | True or false | A light switch | A limit switch, the coolant |
| float | A number with decimals | A car's speedometer | The position of X, the spindle speed |
| s32 | A whole number, signed | The counter on a knob that turns both ways | The steps of the pendant's handwheel |
| u32 | A whole number, unsigned | The odometer, which never runs backwards | The seconds on the cycle timer |
bit is the one you'll use most at first: it's the world of relays. float is for measurements and speeds; in the HAL it's a 64-bit number, with more than enough precision for any machine. Integers come up less: s32 goes from −2 147 483 648 to 2 147 483 647 and u32 from 0 to 4 294 967 295. There are a couple more types, very special ones, that you can forget about for now.
If you try to connect a bit pin to a float signal, the HAL gives you an error at startup and LinuxCNC won't open. It's one of the good errors: it warns you before anything strange happens.
Who talks and who listens
Every pin has a direction. There are input pins (in), which listen to what comes down the wire; output pins (out), which write to the wire; and a few two-way pins (io), which do both.
And here is the golden rule of the HAL: only one can write to each signal. Listening, as many as you like. Think of a WhatsApp broadcast channel: the admin writes and everyone else reads. If you let two write at once, one would say «on» and the other «off»: who do you listen to? The HAL won't even let you try: if you connect two output pins to the same signal, it gives an error at startup. I triggered it on purpose and this is the message: signal 'dos-escritores' already has output or I/O pin(s), in other words, «that signal already has someone writing to it».
My F1 has a good example of a wire with one writer and two listeners. The X limit switch, on the negative side, works as both a limit and a home switch:
net x-neg-limit => joint.0.neg-lim-sw-in joint.0.home-sw-in
Input 3 of the card writes to the wire x-neg-limit, and two LinuxCNC terminals listen to it: the limit one and the home one. One switch, two uses, not a single extra wire in the machine.
Now, careful, because this confuses everyone, me first. The card's physical outputs, the ones that switch a real relay, are input pins in the HAL. It sounds like a contradiction and it isn't: the pin output-01 belongs to the Mesa component and listens to what comes down the wire, then switches the physical terminal. Look at the coolant on my F1:
net coolant-flood => hm2_7i76e.0.7i76.0.0.output-01
The one writing is LinuxCNC (iocontrol.0.coolant-flood, which goes true with an M8) and the one listening is output 1 of the card. With physical inputs it's the other way round: input-03 is an output pin in the HAL, because it writes to the wire what it reads from the limit switch. The clue so you don't get tangled: always ask yourself who is telling what to whom.
The arrows <= and => you see in the lines are only there for us humans to read. The HAL ignores them: you could remove them and it would work the same. I always put them in, because when you come back to a file six months later, you're glad to know which way everything goes.
While we're at it: Mesa cards also give you each input inverted, with -not at the end of the name (input-05-not is the opposite of input-05), and each output has an -invert parameter to flip it. It's like being able to choose between a relay's normally open and normally closed contact without changing a single wire. On my F1, the Z limit switch and the probe are read with -not, and the outputs that enable the axis drivers use -invert.
The round: threads and order
This is the most important part of the chapter, so let's take it slowly.
Imagine a security guard doing a round of a factory. They have a list of doors and always check them in the same order: the first, the second, the third. When they finish, they wait and, after an exact interval, start again. If a door isn't on their list, they'll never go past it, however much it's in the building.
The HAL thread is that guard. On a machine with a Mesa there is one, the servo-thread, and on my F1 it makes a complete round every millisecond: a thousand rounds a second. On each round it goes through every function on its list, in order, and each device does its job: it reads its inputs and updates its outputs. Then the thread rests until the next millisecond.

servo-thread on my F1, with the functions PnCconf puts in: first read, then think and finally write. A thousand times a second.The list for the round is made with the addf command, and the order you write it in is the order it runs in. Here's how mine starts on the F1, just as PnCconf left it:
addf motion-command-handler servo-thread
addf motion-controller servo-thread
addf pid.x.do-pid-calcs servo-thread
addf pid.y.do-pid-calcs servo-thread
addf pid.z.do-pid-calcs servo-thread
addf hm2_7i76e.0.write servo-thread
It makes all the sense in the world, and it's the same thing you'd do in the kitchen: first you check what's in the fridge, then you decide what to cook and finally you serve it. Read, think, write. First the card is read (limit switches, positions), then LinuxCNC calculates where each axis has to be, then the PIDs correct, and finally everything is written to the card in one go.
And if you get the order wrong? Nothing breaks, but you're late. If a device runs before the one that gives it its data, it uses the data from the previous round: one millisecond of delay. For a lamp it doesn't matter; for an axis position loop, it matters more. That's why the functions that write to the card always go last.
And the most typical slip of all: loading a component and not putting it in the round. The device exists, it has its pins, the wires are properly connected, but its output never changes, because the guard never goes past it. It's like mounting a relay on the rail and never powering its coil. If something ever «does nothing» and everything looks properly connected, the first thing I check is whether it has its addf.
One last thing about threads. If you use a parallel port, you'll see another thread, the base-thread, much faster (every 25 microseconds, for example), which generates the step pulses in software. With a Mesa it isn't needed, because the card itself generates the pulses, and my F1 doesn't have one. The servo-thread works with decimals; the base-thread, to go faster, doesn't. If you want to know more about how punctual the computer is with that heartbeat, it's in the latency article.
If you've ever seen a PLC, you already know this: it's a PLC's scan cycle. Read the inputs, run the program from top to bottom, write the outputs, and start again. The LinuxCNC documentation uses that very comparison.
The four commands that build everything
A .hal file is a list of commands that LinuxCNC runs just once, at startup, from top to bottom. It's the assembly of the cabinet: once it's built, the thread does the work. There are more commands, but with these four you can build practically everything:
| Command | What it does | In the cabinet |
|---|---|---|
loadrt | Loads a component | Taking a device from the store and putting it on the rail |
addf | Puts its function into a thread | Writing it on the list for the round |
setp | Sets a fixed value on a pin or parameter | Turning the dial, or tying a terminal to a fixed value |
net | Creates a signal and connects it to pins | Running a wire, with its number, between terminals |
loadrt loads a realtime component. You can load several units of the same one and give them names, which is what I recommend. On my F1:
loadrt and2 names=and_mist,spindle_ready_gate,and_spindle_not_ready
The second line loads three AND gates in one go, each with a name that says what it's for. If you don't name them (with count=3), they're called and2.0, and2.1 and and2.2, and six months later nobody knows which is which. Use names. Your future self will thank you.
addf puts that component's function into the round: addf td_mist servo-thread. You saw it above.
setp sets a fixed value. It works for parameters (setp spindle_near.difference 0.5) and for input pins that aren't connected to any wire. On my F1, output 5 of the card, which will go to the spindle servo's alarm reset, is currently fixed to false with setp hm2_7i76e.0.7i76.0.0.output-05 false. What you can't do is setp a pin that already has a wire, or an output pin: it would be like forcing a terminal that is already fed from somewhere else. The HAL tells you plainly: pin 'and2.1.in0' is connected to a signal in the first case and pin 'and2.0.out' is not writable in the second.
net is the star. It creates the signal if it doesn't exist and connects it to every pin you list after it. You can write it on one line or on several with the same signal name; the result is the same wire:
That line says: «run a wire called mist_blink from the timer's output to the inverter's input and to one input of the AND gate». One writer, two listeners. Now let's see what for.
A real example: the mist coolant flasher
My F1 has a mister for cooling with air and oil. I didn't want it blowing non-stop, but in bursts: one second on, one second off. PnCconf has no option for that, but with three HAL devices you can build it in no time. It's my favourite example to start with, because it has a bit of everything and fits in a dozen lines:
loadrt not names=not_mist
loadrt and2 names=and_mist
addf td_mist servo-thread
addf not_mist servo-thread
addf and_mist servo-thread
setp td_mist.on-delay 1.0
setp td_mist.off-delay 1.0
net mist_blink td_mist.out => not_mist.in and_mist.in1
net mist_blink_fb not_mist.out => td_mist.in
net mist_req iocontrol.0.coolant-mist => and_mist.in0
net mist_pulsed and_mist.out => hm2_7i76e.0.7i76.0.0.output-00
In the real file on my F1 these lines are a bit spread out, because the same loadrt also loads other gates, but they are exactly these. Let's go device by device.
The timer (timedelay) is like a time-delay relay: its output follows its input, but with a delay. It has two dials: on-delay, how long the input has to be true for the output to switch on, and off-delay, how long it has to be false for it to switch off. The stairwell light timer is this kind of timer, with an off-delay. Here both are set to one second.
The inverter (not) flips things: if true goes in, false comes out, and the other way round.
The AND gate (and2) only gives true if both its inputs are true. It's like two switches in series: the bulb only lights if both are closed.

Now the trick. Look at the wire mist_blink_fb: the inverter's output goes back to the timer's input. It's a loop, and it runs by itself:
- The timer's output is off. The inverter flips it and feeds true into its input.
- A second passes with the input true and the timer switches its output on.
- Now the inverter feeds false into its input. A second passes and the timer switches its output off.
- And back to the start. Forever.
It's an oscillator: one second on, one second off, like the flasher relay of a car's indicators. And that oscillator is beating from the moment LinuxCNC starts, whether there's mist coolant or not. To be sure, I built it exactly like this with halrun on the F1's computer, without the card and playing the M7 myself with a setp: the timer's output changes every second like clockwork.
The one deciding whether that beat reaches the valve is the AND gate. One input gets the beat (mist_blink) and the other gets LinuxCNC's request for mist (mist_req, which goes true with an M7 and back to false with an M9). Only when both are true does output 0 of the card open the valve. Without M7, the oscillator carries on by itself and the valve never notices.
A detail you can see in the timing chart, which I mention so it doesn't catch you out: since the oscillator runs freely, the first burst after an M7 can last less than a second, depending on where in the beat it catches it. For a mister it makes no difference at all. If one day you need the first pulse to always be complete, it can be done, but that's for the recipes chapter.
And now look at what we've done: a new function for the machine, without soldering anything, without touching LinuxCNC's code and without extra wires in the cabinet. This is what I want you to take away from this chapter: the HAL is your electrical cabinet, and you're the boss of it.
How to read the names
Long names are less scary when you split them at the dots. They go from general to specific, like a postal address: country, city, street, number.

With the components you load yourself, the name is whatever you put in names= plus the terminal name: td_mist.on-delay, and_mist.in0. LinuxCNC's own ones have fairly clear names: joint.0 is the first joint (X on my mill), spindle.0 the spindle, iocontrol.0 the coolant and tools, halui external controls and buttons.
Signals, on the other hand, are named by you, and here I ask you a favour: give them names that make sense. x-neg-limit or spindle-at-speed explain themselves; s1 or cable3 don't. In wiring diagrams, the wire number is sacred; in the HAL, so is the signal name.
Try it without a machine
The best way to understand the HAL is to play with it, and you don't need a machine. If you have LinuxCNC installed, even on a laptop for simulation, open a terminal and type halrun. You get a halcmd: prompt where you can give commands one at a time, as if you were building the cabinet live. Let's build two AND gates with their own test thread, but only one of them goes into the round:
loadrt threads name1=prueba period1=1000000
loadrt and2 count=2
addf and2.0 prueba
start
The second line creates a thread called prueba («test») that makes a round every millisecond (the period is in nanoseconds). The third loads two gates, and2.0 and and2.1, but only the first gets its addf. With start the guard begins the round. Now «close both switches» on each gate and look at their terminals:
setp and2.0.in1 true
setp and2.1.in0 true
setp and2.1.in1 true
show pin and2
I did it on my F1's computer, with LinuxCNC closed, and this is what comes out:
8 bit IN TRUE and2.0.in0
8 bit IN TRUE and2.0.in1
8 bit OUT TRUE and2.0.out
8 s32 OUT 34 and2.0.time
8 bit IN TRUE and2.1.in0
8 bit IN TRUE and2.1.in1
8 bit OUT FALSE and2.1.out
8 s32 OUT 0 and2.1.time
Each line is a terminal, with its type, its direction and its value. Both gates have both inputs true, but only the first has set its output to TRUE. The second is still FALSE, because the guard doesn't go past it: it's the forgotten addf, live. Notice also the time pin, which we hadn't seen: each function writes there how long it took on the last round, measured in processor cycles. The gate that doesn't run shows zero.
Try setting one input to false and look again. A warning that will save you some time: the same component can't be loaded twice, so if you later try another loadrt and2, it will tell you and2: already exists. Load all the units you'll need in one go, with count= or names=. There's an option to get around that limitation in the machine's files, and we'll see it in chapter 2. When you're done, type exit and everything is taken down. If a halrun is ever left half-done and won't let you start again, halrun -U cleans up whatever was left loaded.
This doesn't touch anything on your machine: it's a test cabinet on the bench, without the card or LinuxCNC loaded. But do it with LinuxCNC closed, so it doesn't get mixed up with the real HAL.
Where all this lives on your machine
A real machine doesn't have a single .hal file but several, and the .ini file says which ones are loaded and in what order, in its [HAL] section. If the configuration comes from PnCconf, you'll see something like this:
- The main file, named after your machine (on mine,
f1CNC_PB.hal). PnCconf generates it, and if you go through PnCconf again it overwrites it. If you've edited it by hand, as I have, make a copy first. custom.hal, for your own additions. PnCconf creates it empty and never touches it again.custom_postgui.hal, loaded after the screen opens. This is where connections to the screen's own buttons and lamps go, because they don't exist until the screen starts.
My F1 loads, in this order, the main file, the one for the WHB04B wireless pendant, custom.hal, the one for the Wi-Fi display I made for the machine and, after Probe Basic opens, Probe Basic's own. And a lovely thing: signals belong to the whole cabinet, not to each file. The mist_req wire we've just seen in the main file is also listened to by the Wi-Fi display from its own file, to show on the display whether mist coolant has been requested. One wire, two files, zero complications.
How these files are read from top to bottom, why mine needs an option called TWOPASS and what happens if you load a device after running a wire to it is exactly the topic of chapter 2.
Frequently asked questions
What is the LinuxCNC HAL?
It's the layer that connects LinuxCNC to your machine's hardware. It works like an electrical cabinet inside the computer: components that act as devices, pins that act as terminals and signals that act as wires. You decide what connects to what.
Do I need to know how to program to use the HAL?
No. HAL files are lists of very simple commands, mainly loadrt, addf, setp and net. It's more like drawing a wiring diagram than programming. Only creating a new component requires writing some code, and that's optional.
What's the difference between a pin and a signal?
The pin is a terminal on a component, with a fixed name that comes with the component. The signal is the wire that joins pins, and you choose its name. Two pins are never joined directly: there is always a signal in between.
Why can't I connect two outputs to the same signal?
Because only one pin can write to each signal; if two wrote, one could say true and the other false at the same time. The HAL checks this at startup and gives an error. Listening, on the other hand, is open to as many pins as you like.
My component is connected properly but does nothing. What's wrong?
Almost always the addf is missing: the component is loaded, but its function isn't in any thread, so it never runs. Add an addf line with its name and servo-thread.
Why are the Mesa outputs input pins?
Because the HAL looks at things from the component: the Mesa's output pin receives through the signal what it has to do and then switches the physical output. Physical inputs, the other way round, are output pins, because they write to the signal what they read.
Can I practise with the HAL without having a machine?
Yes. With LinuxCNC installed, the halrun command opens an interpreter where you can load components, create a test thread and see pin values, without touching any hardware. Do it with LinuxCNC closed.
What's next
You now have the vocabulary and the idea. In the next chapters we're going to use them for real:
- What the HAL is: this chapter.
- Reading the HAL of a real machine: my F1's, line by line, and why the order of the files matters.
- Looking inside without touching anything: halshow, halmeter and halscope, the HAL's multimeters and oscilloscope.
- Recipes: the spindle that won't start if the servo isn't ready, on-screen warnings, timers and pendants.
- Your own component, with halcompile, for when none of the devices in the store will do.
- The typical mistakes and how to find them without going mad.
And all of this will also be on video, on the channel. Meanwhile, open your machine's .hal, look for the loadrt, addf, setp and net lines, and you'll see it isn't black magic any more. If you have questions, or a HAL setup you're proud of, tell me in the channel comments: that's how we'll put together the recipes chapter.
Sources
- LinuxCNC 2.9: introduction to the HAL (concepts and the comparison with an electrical cabinet and a PLC)
- LinuxCNC 2.9: basic HAL (commands, types and connection rules)
- LinuxCNC 2.9: HAL tutorial with halrun
- LinuxCNC 2.9: halcmd manual (net, setp and the arrows)
- LinuxCNC 2.9: the timedelay component
- LinuxCNC 2.9: the hostmot2 driver for Mesa cards
- The real configuration of my EMCO F1 (
f1CNC_PB.haland its.ini), read from the machine on 30 November 2026.