:lang: en
:toc:

[[cha:wiring]]
= Best Wiring Practices

== Electrical Noise

Electrical noise in a system is caused by Electromagnetic Interference (EMI), where signals appearing in one
electrical circuit interfere with an adjacent circuit, either through electromagnetic induction, electrostatic
coupling or conduction. EMI can cause problems with the daily operation of a CNC machine, and can manifest itself in
various ways such as false triggering of limit switches, prematurely interrupted tool probing operations, corruption
of a serial data link to a VFD or erratic behaviour of the CNC control systems and software.

When current passes through a conductor a magnetic field is created. As the current increases the magnetic field gets
stronger, and then collapses again when current ceases to flow. If this alternating magnetic field happens to cross
another conductor it can induce an unwanted voltage into it, which presents itself as noise.

There are several methods that can be employed to minimise the effects of EMI in any electrical system. The most
effective of these is obviously to prevent the noise from occurring in the first place. In reality the method of
controlling the effects of EMI is usually by applying measures to prevent noise from contaminating wanted signals in
the CNC system.

== Ground, Earth and Common

Confusion can arise when using terms such as earth, ground and common. In some cases they may be used to describe the
same thing; that being the point in an electrical system to which all voltages are referenced to. For the purpose of
this article, the terms 'earth' and 'ground' refer to the point at which the incoming mains supply is earthed to,
whereas 'common' is the return or negative terminal on a DC supply.  In some cases it is permissible to ground the
common on a DC supply, thereby making the negative terminal on that supply the same potential as the incoming AC
earth, but for the purposes of this discussion the terms 'earth' and 'common' must be made distinct from each other
to avoid confusion.

== Wire Selection and Use

Wire comes in many types, sizes and configurations. Wading through all the wire available is a monumental task of its
own, but for the purposes of this article it is only necessary to consider the types of wires typically used when
wiring a CNC controller. Additionally, how the wire is to be used can have some effect on the overall system. What
follows are some tips that may prove helpful.

=== Single Conductor Wire

Wire comes in two forms: solid conductor and stranded. Solid core wire is generally cheaper than stranded, but more
likely to break if used in applications where repeated bending is expected. Fortunately, the prevalence of stranded
wire on the market means that its use should be encouraged wherever possible.

Wires should be terminated such that all strands in the conductor are neatly and securely located into the mating
receptacle. This may be accomplished by either twisting the strands together before inserting in the termination,
or using a compression crimp such as a spade or bootlace terminal. Care should be made to ensure that no strands of
wire end up outside the termination to prevent accidental shorting with adjacent terminations.

If using a compression crimp on the bare wire, avoid soldering the strands together before crimping. Crimping the
lug onto a soldered wire can result in the lug working loose over time as the soldered strands lose their
compressibility once the crimp has been applied. For this same reason, soldered wire should not be installed in a
terminal block where the screw stud bites directly onto the wire when tightened.

When stripping the wire ready for a termination, only remove the minimum amount required to keep the termination
covered when complete. Stripping too much insulation off will expose some of the wire that something can short
against.

The circuit that the wire is intended for should also be considered; the voltage the circuit operates at and the
amount of current it carries have a bearing on the choice of wire to be used. The thin insulation on a piece of
recycled CAT5 ethernet cable is insufficient to withstand the voltages that can appear at the output terminals of a
Variable Frequency Drive, nor is the cross sectional area of the conductor sufficient to carry several amps of
current without overheating and potentially causing a fire. Conversely, while it is perfectly permissible to wire a
limit switch circuit using 2.5 sqmm cable, it creates needless bulk in the wiring loom. Consult any manufacturer's
documentation and your local country's electrical wiring codes for minimum suggested wire gauges for power and
control requirements.

=== Shielded Wire

There are two types of shielded wire. One has a bare wire braid that surrounds the wire inside, and the other has
metal foil that surrounds the wire inside. The type of shielded wire selected will depend on the amount of noise you
are trying to combat.

.Foil Shielded Wire

Foil shielded wire has a thin aluminum or copper foil that is usually bonded to a film of plastic that surrounds
the wire. The enclosed wire is usually 100% covered. Attaching the foil to earth can be difficult, especially if
the foil is constructed from aluminum or laminated to a plastic backing material. For this reason, it is
usual to find a bare metal stranded wire enclosed inside the cable which is in contact with the foil for the
full length of the cable. This is called the drain wire and is used to make the connection to earth with.

.Braided Shielded Wire

Braided shielded wire has a woven copper braid that surrounds the wire. It is more bulky than foil and does not
provide 100% coverage, but is more flexible than foil shielded types. Coverage is typically 70% to 95% depending on
how tight the braid has been constructed. Despite the lower coverage of braided shield, the effectiveness is
greater than foil shielding due to the increased bulk of the braid, and copper being a better conductor than
aluminum.

For very noisy environments, a further subset of the above two shielding methodologies may be employed, whereby both
braid and foil shielding is used simultaneously. Individual wires in a multi-conductor cable may also be shielded
along with an overall shield being applied to the entire cable jacket.

== AC Line Voltage

The incoming mains AC that powers the CNC system can pick up and carry noise into the power supplies and other
equipment. For example, if the incoming supply is also used to feed large motors, electrical noise may be
generated on the line feeding the CNC components. Although most modern electronic devices feature built-in mains
filtering to help minimise the susceptibility to mains-borne interference, the custom and modularised nature of a
CNC system can mean that components used come from a wide variety of sources with differing degrees of inherent
noise immunity.

Inline filters may be installed on the incoming mains supply feeding the CNC control system to help reduce any
induced noise. Running the CNC system from a different mains circuit to any large electrical sources of noise may
also help minimise any potential sources of mains-borne interference.

[NOTE]
Be aware that in many countries, the installation and alteration of mains circuits can only be carried out by
licensed electricians.

== Power Supply Units

=== AC Ground

A typical CNC machine may have several different Power Supply Units (PSUs) installed in the system. Any device
powered from the incoming mains designed to be earthed must be properly and permanently terminated to mains earth.
Ideally this should be made to the same point in the system, which may be a threaded post or bolt, a copper/brass
termination strip or a large metallic mounting plate within the control enclosure.

The prevalence of high-frequency switchmode PSUs used in CNC systems increase the likelihood of RF noise
being coupled from them to adjacent circuitry. Many of these PSUs have a metal case which, if connected
to mains earth, will help screen the coupling of high frequency EMI into other electrical components.

From a safety standpoint, it is important that these mains earth connections also be mechanically strong and
unlikely to break free, and the wire used has a cross-sectional area sufficient to carry the anticipated fault
current should a short to earth occur. It is also imperative that mains earth is never used as a current-carrying
conductor for other components in the system. Earth shall be used for one purpose only: safety earthing.

Note also that the colour of the jacket used to make a termination to earth may be prescribed by the wiring code
for your country, and the conduction of other unrelated signals in that same wire colour may be prohibited.

=== DC Common

Commoning of a DC PSU is somewhat dependent on the electrical operating requirements of the CNC system. For example,
a stepper motor driver operating with a 24VDC motor supply and a 5V logic supply may have optically-isolated signal
input lines which provide complete electrical separation of the driver's input and output circuitry for safety and
noise immunity purposes. Tying the stepper motor and logic control supply commons together in this case may have a
detrimental impact on the operation of the system.

In general it makes most sense to keep the commons of the various DC PSUs used in the CNC system separate from each
other, and separate from the AC mains earth unless there is a specific requirement to tie them together. In most
cases the common points of the heavy-duty power sections of the CNC system (eg, stepper motor or servo motor
drivers, spindle motors etc) will be segregated from common points of the electrically-sensitive sections of the
CNC (control interface boards, limit switches, tool probe circuitry etc) to prevent cross-contamination of the two
systems.

Should it be necessary to connect several common points of different PSUs together, or to connect a common of a
PSU to AC main earth, it should be done at a single point only and as close to the common terminal of the PSUs as
possible.

In CNC machines where the hardware drivers and interfacing circuitry are pre-assembled, the decision as to which
DC commons are tied where is usually taken out of the hands of the end user.

== DC Supply Feeds

In situations where a DC circuit is run with the common point disconnected from the mains earth (ie, the supply is
'floating'), it can be helpful to run DC supplies using twisted pairs of wires, whereby each pair of wires in the
circuit (eg, the positive and negative leads) is physically twisted together in a helix pattern. The twist in the
wire allows both conductors to share the same 'real estate' as closely as possible. Any EMI that passes across
them will therefore be largely canceled as both conductors will receive the same degree of EMI. For additional
protection use twisted wire that is housed in a shielded jacket with the shield terminated to mains earth.

Note however that twisted pairs of wires are less effective at combating the effects of EMI if one of the two
wires is referenced to mains earth, as the conductor at earth potential is less able to be influenced by EMI than
the un-earthed conductor. In these instances the twisting of the wires has less of an impact on the overall noise
immunity, and shielded cable will be intrinsically more effective at reducing noise pickup.

== Signal Wires and Control Lines

The wires that are used to transmit logic signals to and from various peripherals in the CNC (eg, stepper motor
controller inputs, axis limit switches etc) are the most susceptible to noise interference. The reason for this is
the low level voltages that are used to convey the information. When a limit or home switch is engaged, or a tool
probe has made or broken contact, this signal is used to signify the event has taken place. Typically this is done
by using input pins on the computer interface card or parallel port which, dependent on the application, may be
signaled using as little as 3.3V. Evidently a 2V noise spike has the potential to corrupt the validity of a
signal if the useful range is only 0-3.3V.

If possible, isolate the common point of the PSU supplying the logic peripherals from the rest of the system.
For example, keeping the common of the low voltage power supply isolated from the common of the stepper motor
supply will reduce the chances of large currents flowing in the stepper motor return line contaminating the common
of the low voltage supply.

If the controller uses differential signaling, use twisted pairs to carry the signal. Shielded cable is preferred
when the control lines are single-ended, or if the distances traversed are long or through electrically hostile
environments. When grounding the shield in the cable, terminate to the mains earth.

If the controller and interfacing devices can withstand higher control signals, consider altering the wiring and
power supply requirements to use a bigger voltage for signaling (eg, 12V or 24V). The same 2V EMI noise spike
that could corrupt a 3.3V limit switch signal will be far less likely to cause issues with a limit switch
operating with a 24V signal.

== Stepper or Servo Motor Drivers

The metal housing of the driver should be connected to the local mains earth in the CNC system. Some driver
enclosures will indicate a specific terminal as being the earthing point, in which case this point must be
connected to earth via a dedicated wire.

Control and power wiring should be segregated as much as possible. Route signal input wires well away from power
supply and motor drive output lines.

It is recommended to run both driver input and motor output wiring in shielded cable with the shield terminated to
mains earth. The shield on the input lines helps reduce the amount of interference they can receive, while the
shield on the output lines reduces the amount of noise they can radiate.

== Variable Frequency Drives

If at all possible the Variable Frequency Drive (VFD) should be mounted in a separate enclosure or cabinet to reduce
the risk of it radiating noise into adjacent wiring. If the VFD enclosure is metallic it must be earthed as per any
recommendations in the manufacturer's documentation.

Because the VFD is a high power, high frequency electronic switching device, the output is notoriously prone to
EMI radiation, and it is advisable to run the VFD output to the connected motor in a shielded cable, with the
shield terminated to mains earth.

== Routing Conductors

=== Routing Movable Wires

Any wire that will be moved about during normal operation of the CNC falls into this category. For example, wires
running from stepper drivers through a cable management system (drag chains) and then to the stepper motors
mounted on a movable gantry.  Cables and wires operating in these circumstances should be rated for extra
flexibility. This precludes the use of solid-core wires and cables, as the constant flexing will lead to fatigue
and eventual failure of the conductors.

If running cables in a cable track/carrier, tie them down at both ends of the cable track. If not, ratcheting can
occur and fatigue the cable prematurely. Care should also be taken to ensure that mechanical rubbing of conductors
against other parts of the machine is prevented.

In a cable track/carrier observe the neutral axis idea. Have the wire run as close to the neutral axis as
possible. Make sure the wire is not in tension in the longest neutral axis situation.

=== Routing Stationary Wires

As discussed earlier, running different signal classes (high voltage and low voltage) in proximity to each other
has the tendency to exacerbate EMI interference. Separate conductors by as much distance as possible. If two
conductors must cross over each other make the crossing as close to a 90 degree angle as possible.

Avoid long loops of excess wire at any peripheral devices - they are great antennas for receiving or transmitting
noise. Where possible, run wires in close proximity to large earthed structures. If the controller enclosure
features a large metallic back plate that is earthed, secure all control wiring against this surface as much as
possible while wiring between two points.

== Mechanical Noise

Very few mechanical switches (eg, an axis limit switch or tool probe input) will close or open perfectly when
operated. More often than not the switch contacts will physically bounce against each other several times within a
very short space of time when operated. This may be interpreted by the machine controller as multiple operations
of the same signal when in reality only one clean state change was expected. Sometimes it doesn't matter, but in
many circumstances it is desirable to ensure that any state change is as 'clean' as possible and does not
interfere with the operation of the machine. This is accomplished by debouncing.

Debouncing is achieved by permitting a state change on a mechanical switch to only register with the controller
after a fixed period of time to allow any bouncing in the switch contacts to settle. Time delays of 5-15
milliseconds are usually sufficient. This can be done with the addition of some hardware to the signal circuit or
in software within LinuxCNC.

=== Hardware Debouncing

Several schemes exist to implement debouncing of switches and relay contacts with hardware, ranging from the
addition of a single capacitor across the signal and common lines, to dedicated debouncing integrated circuits
such as the MC14490 or MAX6818. Several hardware debouncing schemes can be found via the link below:

https://electrosome.com/switch-debouncing/

=== Software Debouncing

The Hardware Abstraction Layer (HAL) of LinuxCNC includes a debounce component. This component has a single input
pin and a single output pin. Its job is to monitor the input and to send an output after the input has activated
for a programmed delay period. More information can be found for the debounce component by visiting the following
page:

link:../man/man9/debounce.9.html

== Documentation

The importance of documenting the installed wiring and components cannot be over-emphasised. Should the user want
to modify the CNC system further down the track, or if trouble should arise that needs correcting, then complete
and concise documentation of the wiring and equipment can save many hours of head scratching and frustration.

=== Hardware Documentation

At a minimum, make sure to save any documentation associated with the installed hardware in a safe place. Stepper
controllers, break out boards, power supplies, VFDs, interfaces and controllers, servo and stepper drivers and any
associated device settings are all critical components of the system and their documentation should be kept at
hand for easy reference.

=== Wiring Schematics

As the CNC machine is wired, make sure to draw up a schematic that can be referenced to later. The schematic does
not have to be all that neat, but it should be understandable in such a way that it could be easily interpreted at
a later date, ideally by anyone who may need to service the equipment. Include details such as wire colours used,
pin numbers, part numbers and any other notes that will help explain particular details not immediately apparent
from first glance at the schematic.

=== Wiring Identification

Take the time to identify each wire in the system. When a bundle of wires has been cable-tied in place it can be
very difficult to look at them and know for sure which wire goes where. Label the motor wires with the joint or
axis they are associated with, or identify each signal wire so that it is easy to identify what that signal does.
It will also help if this information is transferred to the wiring schematics.

// vim: set syntax=asciidoc:
