Using Relays with grblHAL and the T41U5XBB or PicoCNC
I regularly get questions about using relays. So, this blog entry's goal is to make it easier to understand.
Relays are often used in CNC machines. Their basic job to allow low current/power to control higher current/power devices. Things like control cutting fluid, coolant pumps, dust collection systems, lights, vacuum holding systems and others. It is a simple concept - basically, a controlled switch. Some times they are controlled from within GCode programs and other times, they are controlled via front panels or program macros.
A simple example is the dust collection relay in the T41U5XBB. When the spindle is turned on by the S command in a GCode program, the Dust Collector relay is also turned on which can then be used to turn on power to your dust collector. This automates the use of dust collection, a big convenience. So, how do we make this work in a system we are building? First, we have to understand relays.
Types of relays
There are 3 general categories of relays than can be used (I am using the term relay in a fairly broad manner):
Traditional Mechanical Relays with a coil. These are very simple devices have been around since the 1800s. They consist of an electromagnet (coil) and a mechanical contact (switch). When the coil is energized, the switch moves and creates (or breaks) a connection. Because there is no electrical connection between the coil and the contacts, they are inherently isolated and thus don't create electromagnetic conduits into your controller. There is a huge range of available products - Mouser offers around 45,000 different ones with different coil voltages, number of contacts and reliability ratings. The T41U5XBB can directly drive a 5V or 12V coil. Care is needed in selecting a proper relay for your application. See the relay sizing section below.
Powered Relay Boards (PRB). These are boards that have one of more traditional relays and the circuitry to control them based on a logic (often called TTL) input. They can be controlled with a low voltage signal, usually 5V. These boards require separate power source to run. You can find very inexpensive boards but you need to take care in selecting as many have fairly low capacity relays and may not be appropriate for your application. See the relay sizing section.
Solid State Relays (SSR). These are fairly recent. They use Thyristors (TRIACs or SCRs) or similar to control electricity rather than the mechanical contacts of the traditional relays. They are more expensive than mechanical relays but have some advantages - they use less energy, are quieter, don't arc/spark, are more resilient to vibration and, if used properly, have a longer life span. SSRs use optical isolators to trigger so are also isolated. Similar to PRBs, these can be controlled with a 5V signal. Traditional relays cost less and can take a lot of abuse. But, either will work just fine in a CNC application. Many SSRs are very easy to install as they can be directly mounted on a base plate or a DIN rail.
Selecting and sizing relays
Selecting a relay for your application requires that you understand the power that you will need to control. Mainly, you will need to know the power, voltage and current draw of your device. Often a device has an inrush of current at start up that is higher than it's rated current. Selecting a relay for a motor needs to account for this inrush current. A complete description of this is beyond the scope of this blog. Though many relays have horsepower ratings that you can match to the HP rating of your motor. A warning about those ubiquitous powered relay boards you see on Amazon, eBay or AliExpress, they often exaggerate the ratings of the actual relays used. Try to get a data sheet for the relay - you can often see the manufacturer and part number in the photos. A good company will give derating information that takes into account environmental issues. Though, a photo on an ad is no guarantee for what you will receive. For bare relays, chose one from a mainstream manufacturer like Omron, Fujitsu, Schneider or Panasonic. Look for a data sheet, it will tell you the specifications of the relay.
Solid state relays are little more complex to select. To start with, look for a load current of at least double the expected current. Industrial mount SSRs are easy to mount and many are compatible with DIN rail kits. SSRs have several control voltage options - look for ones with a DC range that covers 5V. 3-32 VDC is a common trigger range. You will also see "Turn On" options - Zero Crossing or Random. For DC current control, you will want Random. For AC control, either will work but Zero Crossing will generate less interference so are like to cause you EMI problems.
Connecting to the Breakout Board
The T41U5XBB and other breakout board supports all of these relay types. It is fairly straight forward but slightly different for each type.
Bare relay coils. The screw terminal pairs labeled "Relays" are used for relay coils. It usually doesn't matter which screw terminal connects to which terminal of the relay coil. Simply run a pair of wires from the breakout board to the relay. Note that some specialty devices (solenoids, for example) may be polarized. There is +V next to positive terminal which would connect to the + terminal on the device. Note also, that the breakout board is capable of driving up to 500 mA of current in total with any one output limited to 300 mA. If you need to drive a device with more current, use a relay to turn on the higher current device. The board allows you to choose between 5V or 12V coils. 12V is preferable due to lower current draw but 5V relays work fine. However, we recommend you not drive 5V relays with USB power but rather use an external 5V power supply.
Powered Relay Boards. These are driven from the pin headers labeled TTL/SSR and operate at 5V. Make sure your PRB takes 5V input. Each output is a pair of pins, labeled Signal (Sig) and Ground (G). The Sig wire goes to the "+" input on your PRB and G goes to the ground or "-" input. Your PRB also requires separate power.
Solid State Relays. SSRs are connected similar to PRBs. The only difference is that many, if not most, do not care about control polarity, you may see a "~" near the connector on the SSR. Verify with the data sheet or labeling on the SSR.
Note that you can control PRBs and SSRs from the screw terminals but it requires a little extra work. See this blog for details.
Controlling Relays from grblHAL
There are 3 classes of relays that require somewhat different methods of control: Standard Grbl Relays, Auxillary Relays and the Dust Collector Relay. Note that the Relay output and TTL output for a given signal are both triggered at the same time. No special configuration of grblHAL is needed,
Standard Grbl Relays. There are three relays in this group - Spindle, Mist and Flood. These are controlled via commands in GCode. M3 or M4 will turn on the spindle relay. M3 is for clockwise direction and M4 is for counter (anti) clockwise though both raise the Spindle Enable output from 0v to 5V. M5 turns the spindle relay off and lowers the Spindle Enable output to 0V. The Mist Relay is turned on by M7, the Flood Relay is turned on by M8. Both are turned off by M9. These relays can also be controlled from a GCode Sender application such as UGS, ioSender, bCNC or similar.
Auxiliary Relays. The T41U5XBB has 3 additional relay outputs that may be controlled. These are controlled via the M62-M65 commands. M62 and M64 turn on the specified relay on while M63 and M65 turn it off. M62 and M63 commands do not take effect until the next motion command is executed by grblHAL. M64 and M65 take effect immediately when received. The command takes a parameter that specifies the auxiliary relay. The form is:
Where n is 0 for Aux0, 1 for Aux1 and 2 for Aux2. M63-M65 work the same.
Dust Collector Relay. This relay is not directly controlled but is connected to the Spindle Enabled signal. It turns on anytime the spindle is turned on. In addition, it can also be turned on by a switch connected to the Vac/DC header on the breakout board even if the spindle is off. This is convenient for using the dust collector for cleanup.
Macros can be used to control any of the relays and allow you to use their actual function in the name. Simply create a macro with GCode to control the specific relay. An example
You have the Aux0 relay controlling a vacuum pump for your bed. To control the Aux0 Relay, create 2 macros in your GCode Sender. One called VacOn and add this to it:
And one called VacOff, add this to it:
Then when you run the macros they will do what you want and you don't have to remember which relay output you use and how the M codes work.
Switching High Power Loads.
Sometimes you need to control currents that are too much for standard relays. Typically, anything above 15 Amps should be controlled with a contactor, a device commonly used to control HVAC devices. They are reasonably inexpensive and incredibly durable. It essentially a very strong relay but the coil current is usually significantly above typical general purpose relays. Normal practice is to use a small relay to drive the contactor's coil. Because the contactor coil current is higher, it also has much more powerful back-EMF - backwards voltage due to the collapsing magnetic field when the coil current is turned off. Back-EMF can damage electronic circuits. Using a relay to drive the contactor protects the main electronics.
And, as always, if you aren't trained in with working with high voltage, contact an electrician.