1. Set Up
First you need to install the Arduino IDE. You can find it here.
Once installed open it and you should see something like this:
Connect your Arduino board to your computer and choose your board from the list shown in the menu Tools/Board/… as shown below:
Make sure you have also chosen the correct port at the menu shown in Tools/Port/… as shown below:
If you can see your port, then you have successfully installed Arduino IDE and are able to see your board correctly. If you can’t see your board here, then check the web for solutions.
2. Testing
After setting up the board as shown in the previous section, we need to test an upload. To test things are working properly we are going to load the classic “blink” program. To do this, go to File/Examples/01.Basics/Blink as shown below:
This program should look something like this:
To test whether you can upload things properly hit the right pointing arrow as shown here. This will upload the program. If it uploads correctly, you should see a “Done Uploading” message in the bottom part of the console. Then you should see an LED, mounted on your Arduino board and labeled “L”, blinking on and off every second.
In order to test whether you can edit the program, let’s change the code in the loop() section, by changing the argument to the delay() function from 1000 to 200.
Click upload again and after it is done uploading you should see the same LED flashing faster than before, now every 200ms.
If you are able to see this, then you have successfully tested that you can edit and upload code to your board. This is also a great way to test that things are uploading correctly when you need to troubleshoot.
3. Libraries to read Arduino in Pd
3.1 Arduino_Pd
There are several libraries out there to read Arduino data, but we are going to use a nice and strong one by the great Alexandros Drymonitis, be sure to check out his work!
The library we are going to use is found at: https://github.com/alexdrymonitis/Arduino_Pd
To download the library, go to Code/Download ZIP as shown below:
Unzip the file.
3.2 Comport
Open Pure Data, then go to Help/Find Externals
When the window opens, search for the comport library. This library allows Pd to read serial ports in your computer.
Your computer will show you the appropriate versions for your operating system. Find the latest version and click on it to install it.
4. Reading Arduino digital inputs in Pd
Let’s try a button program. All the code is a set of modifications of the Arduino and Drymonitis examples.
4.1 Connections
Connect your Arduino to your breadboard, button and resistor in the following way. If you are not familiar with using breadboards, follow this tutorial.
If you want to test whether your connections are correct, you can upload the code for button in File/Examples/02 Digital/Button. If your LED “L” lights up when you press the button, your connections are correct.
4.2 Arduino Software
In your Arduino IDE, create a new file with File/New. Paste the code below and upload.
// In this section, you declare your constants:
const int buttonPin = 2;
// In this section, you declare your variables:
int buttonState = 0; // variable for reading the pushbutton status
void setup() { // put your setup code here, to run once:
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// Open the serial port:
Serial.begin(9600);
}
void loop() { // put your main code here, to run repeatedly:
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
Serial.println(“digital 2 1”);
}
else {
Serial.println(“digital 2 0”);
}
}
4.3 Pd Software
In Pd, we need to create a patch using comport and serial_print, so make sure you followed the steps in section 3. For this patch, we need to use the serial_print abstraction found in the folder of Arduino_Pd that you downloaded from Drymonitis, so you will need to tell Pd the path, or copy the abstraction to your folder.
In order to use this patch, you need to obtain the list of devices from comport with the message devices. See what devices are listed in the Pd console. Generally, you’ll only see the device you opened with serial.begin(9600) in your arduino code.
In my case, it was device 32, so I sent the number 32 to the open $1 message box to send the message “open 32” to the object comport. As soon as you do that you should see output printed in the console. You will notice that symbols come out of the rightmost outlet of serial_print and numbers and lists come out of the left most outlet. For that reason, we prepend the symbols and use them to separate digital from analog.
5. Reading Arduino analog inputs in Pd
Now we will try . These are loose modifications of the Arduino Examples.
5.1 Connections
Connect your Arduino to your breadboard and potentiometer or pot in the following way:
5.2 Arduino Software
In your Arduino IDE, create a new file with File/New. Paste the code below and upload:
//constants:
int sensorPin = A0; // select the input pin for the potentiometer
//variables:
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// Open the serial port:
Serial.begin(9600);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// write the sensor value to the serial port:
Serial.print(“analog 0 “);
Serial.println(sensorValue);
}
5.3 Pd Software
In Pd, we need to make small adjustments for this to work:
6. Challenge
Make a two button and two pot patch and arduino code.