Anonymous commenting enabled / on the bottom of the page is a vote for next tutorials

Wednesday, February 2, 2011

Arduino to Pure Data

Arduino can output Serial data ( analog data ) from sensors connected to the arduino board.


01| CONNECTING SENSOR TO ARDUINO

1. Connect 5Volts to the Breadboard



2. Put a 1M Ohm resistor that leads the 5v to the channel where the light sensor will be.



3. When the resistor is in place, put the Light sensor ( or any other sensor ) behind the resistor, leaving 1 space at least.



4. Connect a cable from your Arduino's A0 Analog port to the space between the resistor and the light sensor. The cable will measure the data from the light sensor since the resistor is a Pull-up wich will pull current to itself. through the (white)cable.





5. Behind the Light sensor connect a last cable to the GND(ground)






02| MAKING THE ARDUINO PATCH

6. First connect your Arduino, go to Tools -> Serial port and check what port is selected. You will need to use this port in Pure Data later on.



this sketch reads the analog data from the A0 pin, which would be in a (0 - 1023) range. I divide it by 4 to get a value between (0 - 255) which is standard for serial communication.

void setup() {
Serial.begin(9600);
}

void loop() {
int sensorValue = analogRead(A0);
int serialValue = sensorValue / 4;
Serial.write(serialValue);
}


7. Upload the sketch to your arduino board and shutdown Arduino software.


03| MAKING THE PURE DATA PATCH

8. open Pure Data and make a New File

9. Make a new object "comport 9600" (9600 is the serial speed as defined in arduino)



10. make a message "devices" which you connect to the "comport" object.



11. when u exit edit mode in pure data (CTRL+E) and you click the devices message, you will see al the ports printed in the output window of Pure Data. Remember the port you selected in Arduino.



12. in Pure Data make a new message that states "Open #numberOfPort" in my example "Open 4". when you click this message in run mode (CTRL+E) you will see a message saying that the port is opened.



13. Now it's time to output the data that Arduino sends, so in the outlet from the "comport" attach a Number object. Normally you should see values changing in the number box. if not, check if arduino is turned on and the sketch is uploaded to your arduino



With this done you can add any object you want to the outlet of the number.
here's an example (low quality)

CLICK HERE FOR YOUTUBE VIDEO

23 comments:

  1. Thanks, just needed this simple but clear explanation on serial ports ;)

    ReplyDelete
  2. Thanks - Very Nice.

    ReplyDelete
  3. This is a nice tutorial. But what if i need more than one input? I don't know how to write more than one digitalRead into an array that Pd could read as an array. When i use an array in Serial.write, i only get each value after the other in Pd, and not a real list that i could unpack.

    ReplyDelete
    Replies
    1. There's a library named "SimpleMessageSystem" and you can download it from the internet then add this library to your Arduino board. there's an example .pd file inside that library which can achieve simple functions such as sending and receiving data. (for more than one input, analog and digital)
      you can use it as a reference. or just see how it works in pd.

      Delete
  4. YOU ARE A GOD AMONG NERDS!

    ReplyDelete
  5. 1 M Ohm?
    you mean 1 kOhm? right?

    ReplyDelete
  6. what can I do if I want to have more than one sensor, please I really need help!!!

    ReplyDelete
    Replies
    1. Looking for the same, someone please help!

      Delete
    2. If you are still looking for help with this... I was struggling with the same thing and I think I found a simple work-around:

      Just map each sensor value to a given set of values:


      //arduino code
      int val1;
      int val2;
      int val3;

      void setup() {
      Serial.begin(9600);
      }

      void loop() {
      val1 = analogRead(0);
      val2 = analogRead(1);
      val3 = analogRead(2);

      val1 = map(val1, 0, 555, 0, 100);
      val2 = map(val2, 0, 555, 101, 200);
      val3 = map(val3, 0, 1023, 201, 300);

      Serial.write(val1);
      Serial.write(val2);
      Serial.write(val3);

      //This is for printing values to serial monitor...
      //Serial.print(val1);
      //Serial.print(" ");
      //Serial.println(val2);
      //Serial.print(" ");
      //Serial.println(val3);

      delay(100);
      }

      ///////////////////////////////////////
      Then, in Pd, connect a series of [split] objects to the [comport] object to poll for those values...

      [comport 9600]
      | | |
      [split 0 100] [split 101 200] [split 201 300]

      I hope this helps. If you have any questions, feel free to email me at colin.zyskowsi@gmail.com

      Cheers

      Delete
    3. that's nonsense. ho will you deal with garbled jumping values in Pd then?
      You should use probably something like:
      [comport]
      |
      [serialize N]
      |
      [unpack f f f f f f f f .....]
      ||||||||

      etc...

      Delete
  7. I get my values in the arduino serial monitor fine but when I try and read from pure data the serial window give me odd data eg a value of 144 might show on the monitor as 1141441411. On pure data I get values between 10 and 54 that seem to (roughly) repeat.

    Once I close the port via pd all is fine in the serial monitor window again.

    Does anyone have an idea what I could be doing wrong or if it's a bug? I am using the arduino client 1.02.

    ReplyDelete
  8. For anyoen that had trouble with the value displaying correctly in the Serial Monitor, but not behaving properly in Pure Data, make sure it's Serial.write(valueHere); instead of Serial.println(valueHere); it only works for me in Pure Data when I use the former!

    ReplyDelete
  9. What if the value being read into puredata is not a number but alphanumeric?

    ReplyDelete
  10. Hello, I would like to get some values in puredata from arduino(using the piezo sensors). I'm wondering how I can pass the data of several piezosensors separately to puredata. I was thinking I will need to use separate analogue pins on arduino, is it right? any advices?

    ReplyDelete
  11. HI, I´m trying to send NOTEON (MIDI out) through some sensors connected with arduino to send midi information to Qlab software.
    Today i know that is not possible. I need a middle program like Puredata.

    Somebody can help me if i can send the signal of anagogic and digital inputs in arduino to send difference valours of Note ON for qlab

    Thanks a lot

    ReplyDelete
  12. I am sure that PD could not be as developed as it is without a patch for receiving ASCII data from comport. I have antique clock tick-tock data picked up by Arduino to by processed by PD for beat error. I could build a raw data to ASCII translator but I have only had PD for two days and would rather not take the time to re-develop what is probably already out there. If someone could point me in the right direction I would appreciate it.
    Thank you.

    Bill

    ReplyDelete
  13. Big trouble:
    [comport] ** WARNING ** port 9600 not valid, must be between 0 and 98
    [comport] opening serial port 9600 failed!
    Why is this happening? Even pduino is not working now, although everything was fine few weeks ago..

    ReplyDelete
  14. Super relevant but can't see the pictures

    ReplyDelete
  15. Are you tired of seeking loans and Mortgages,have you been turned down constantly By your banks and other financial institutions,We offer any form of loan to individuals and corporate bodies at low interest rate.If you are interested in taking a loan,feel free to contact us today,we promise to offer you the best services ever.Just give us a try,because a trial will convince you.What are your Financial needs?Do you need a business loan?Do you need a personal loan?Do you want to buy a car?Do you want to refinance?Do you need a mortgage loan?Do you need a huge capital to start off your business proposal or expansion? Have you lost hope and you think there is no way out, and your financial burdens still persists? Contact us (gaincreditloan1@gmail.com)

    Your Name:...............
    Your Country:...............
    Your Occupation:...............
    Loan Amount Needed:...............
    Loan Duration...............
    Monthly Income:...............
    Your Telephone Number:.....................
    Business Plan/Use Of Your Loan:...............
    Contact Us At : gaincreditloan1@gmail.com

    ReplyDelete