MAXMSP - Serial data from Arduino to max

One of our most popular videos. Ever. Fact.

Sending data from Arduino into Maxmsp.

On paper you would imagine its a trivial task but it turns out its something that a lot of people come into a number of hurdles with.

Its also a nice quick one!

The general overview of the video is we set up a bog standard - my first Arduino patch - that creates a random number when a button plugged in is pressed.

The whole Arduino section fits between 0:00 - 6:19. The magic moment is when we introduce Serial.Println() at 5:25.

Serial.print() is exactly what it sounds like on the tin. The printing of a message on to the Serial Port. What the serial port is is a bit of a deeper issue for all you computer historians out there - Link. Essentially its a historical physical port for transferring data to devices such as printers. Now a days we can internally use it for our mad science.

6:20 - so at this point in the video we have a physical board setup with a button connected to pin 2 on our Arduino, we have some code that puts a random number on something called the Serial port every time that button is pressed.

What now?


All of the magic in max comes from its ability to natively read from the Serial Port.

Using the Serial Object we can pull data straight some the serial port as quickly as Arduino puts it there. (for that we match the baud rates - in this case, we match the 9600 rate set in Arduino)

8:48 we have the serial set up and are printing data into the max window to test what we have. But as we can see its jibberish. This is due to Arduino posting the numbers in ASCII format. A standard for characters across software packages. There are many reasons to do this, but simply it means we can guarantee that what we receive is what we sent.

Using Serial.write vs Serial.print in the original Arduino code would have the same effect, but can get funky if we wanted to send words, punctuation or really anything but a number!

9:50 - To address this we separate each message into their single lines (we use the 'sel' object to search for line ending and carriage return commands - according to ASCII these are 13 & 10)

In English - we scan the messages we are getting from the serial port and when we see a '13' followed by a '10' we can be certain we have reached the end of the sent message (this is why the Serial.println() is important in Arduino, as it adds the new line carriage return).

10:11 - after knowing the length of the messages arriving we can collect the data, and decode it.

using the ZL package in max (its built in by default, don't worry). the 'group' function allows us to collect data between bangs (we send one on '13', one on '10') or when it reaches a character limit - so we set ours to something arbitrarily large (in this case 1000)

At this point we have a collected bunch of ASCII characters pouring our of our zl group object, all of which make a single number as put onto the serial port.

The brilliant 'ITOA' object turns these numbers back into the readable version of the ASCII code (numbers into text)

Finally, 'FromSymbol' turns it from being text into an understandable number! From here you can do whatever you need to with the data that is in Max, in perfect sync with Arduino, as it intended.

An example of the whole process would be:

Arduino-Serial Port-Max in-IOTA-from symbol

502-53 48 50- 53 48 50-"502"-502

You can pick up the completed setup here, all you'll need to do is follow the Arduino setup diagram and you should be golden!

Join the PfP mailing list