Max or Arduino – Relays

This lesson covers how to use an electrical relay both in Arduino code and then with Max/MSP.

What is a Relay?

A relay is a swtich “They commonly use an electromagnet (coil) to operate their internal mechanical switching mechanism (contacts). When a relay contact is open, this will switch power ON” from www.swe-check.com.au

Students will usually have a relay in their kit which looks like this:

This will allow you to break the flow of current going to a device or component. The device can be a motor, a lamp or anything that can be connected to power.

Relays can also come with more than 1 switch allowing you to control several devices seperately through your Arduino.

Connecting a Relay

The following image shows how a relay is connected. On the right you connect the relay to Arduinos GRD, 5v and a digital PIN that you have selected. On the left you see the connection you want to switch on/off, in most cases you will switch the GRD.

There are multiple ways to use a relay. Depending on what you want to control, or what power you want to control you would use a different set up but in general connecting the relay is the same. In the following example we connect a DC motor (powered by the same arduino) to a Switch and control it via our Arduino:

Here you can see that we are controlling the relay via digital PIN 8 and we are using it to switch on/off the GRD of the DC Motor.

In the next diagram we want the power to come from a sepearate source than from the Arduino itself. There may be multiple reasons for this, mainly because the device needs a seperate power source or voltage:

Using only Arduino Code

To flip the switch on the relay I simply need to send a message to PIN 8 that says HIGH or LOW. There are many ways to interact with the relay once you have connected it to a pin. You can use an IF-Statement so that it turns on when something triggers it. Or you can have it trigger with time.

The following code will turn the motor on/off at 1 second intervals:

void setup() {
  pinMode(8, OUTPUT);

void loop() {
  digitalWrite(8, HIGH);
  digitalWrite(8, LOW);

Using Max/Arduino Module

You can control the relay with Max/MSP module simply by toggling the digital PIN 8. Again, you can use anything in Max to trigger the toggle that controls the relay. You can use If-Statements or simply use the inputs coming in from your Arduino to Max to control the toggle.

NOTE: Dont forget you need the specific Arduino code for connecting to the Max/MSP Module.

Connecting 230v devices with a multi-channel Relay

If you want to control a vacuum cleaner or any hi-voltage device make sure your relay is meant for that output. To control it it is just as simple as the others except each channel is a different Digital PIN on your arduino.