Use the value from a pot connected to Arduino in Pd

(Update : a reader (dbfreq, see his messages in the comments) found a bug in the patch. There was a typo in the route object in the arduino abstraction. The file available for download has been fixed, but I haven’t changed the picture in the post.)

In this post i’ll explain how to use the value read from a potentiometer connected to an Arduino board in Pure Data (to control the length of a delay line).

I first made an abstraction called [pd arduino], in which i put all the stuff related to the [arduino] object. As you can see in the picture below, we can send 2 different types of messages to this object : “open 5” will connect to the arduino board (mine is on port 5, yours can well be on a different port), “close” will close the port (if you want to remove your arduino safely!), and “pinMode 19 analog” will open pin number 19 as analog input. The “pinMode” message can be adapted to open any pin for input or output on the arduino. This is where the power of Firmata lies : it implements two-way communication between Pd and the board in a very easy way. Please refer to the “arduino-test.pd” patch in the Pduino folder for a more complete explanation (it’s a good thing to play with this test patch with different things connected to the board – buttons, pots…).

Once the board AND the pin are open, data starts coming out of [arduino]’s output. We use a [route analog] object to only get analog data (we’de use [route digital] to only get digital data). Then we use [route 5] to only get the value of a pot connected to analog pin 5 on the board (the fact that we had to open pin 19 to read analog pin 5 is not very intuitive, but it’s actually pin 19 among all pins, and pin 5 among analog pins only). The remaining objects in this abstraction are only there to normalise the values from the pot between 0 and 1 (if you can turn your pot over it whole range you’ll get values between 0 and 1000 and it’ll be easier to normalize).


Ok, so now we have the value read from the pot coming out of the [pd arduino] abstraction by its outlet. Great, that was the hardest part. I’ve made a small patch around this abstraction to use the values from the pot to control the length of a simple delay line with feedback. I multiplied the output of the abstraction by 500 to have a range between 0 and 500 (to get audible changes in the delay length), and then used a combination of [pack 0 100] and [line~] to control a [vd~] object. The later reads from the delay line a length controled by an audio signal (to have smoother changes), so we needed the [line~] to change the raw values from the pot into an audio signal. I put 100 in the [pack] to have a slow ramp in [line~] every time the value changes to make things even smoother. The rest of the patch should be pretty straightforward (if it not please refer to this post).


Please note that in order to use this patch you’ll need :

  • An Arduino board…
  • To upload the Firmata firmware on it (it is available in the standard Arduino IDE),
  • A potentiometer connected to the board (see tutorial here),
  • The Pduino “package”, which you can download here,
  • To know on which port your board shows up. You can send a “devices” message to the [arduino] object to see a list of ports to which devices are connected. Your board should be on a port higher than 1. Or, you could very easily adapt the abstraction I made for a previous post to scan all the ports and automatically detect your board.

Here’s a sample of the sounds you can make with this very simple patch :

And here’s the patch.

This entry was posted in Interfacing Pd and tagged , , . Bookmark the permalink.

6 Responses to Use the value from a pot connected to Arduino in Pd

  1. dbfreq says:

    Hi there, I am trying to make this work but am having a slight issue. PD connects to the Arduino fine but there is something happening with the s firmware and r firmware commands in the arduino object. I can’t get any data from the pot’s analog input until I manually bang the pinMode 19 analog command (by clicking on it). I am using an Arduino Uno with Firmata 2.2 (Pduino .5) and I have tried this on both Ubuntu 12.04 and OS X 10.5 and get the same problem. It is like r firmware isn’t working… Any ideas?

  2. dbfreq says:

    Hi there again, I was wondering how one would go about adding another pot to control the amount of feedback in this patch?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s