JeremyBlum.com

v3.1

Tutorial 6 for Arduino: Serial Comm and Processing

| 147 Comments

This tutorial was featured on the official Arduino blog on 3/9/2011

The next four episodes are all about communicating between the Arduino and other devices. In this video, we’ll focus on communicating with the computer via a standard serial connection. We’ll be using a new programming language called “processing” to visualize some information from the Arduino on the computer screen. If you are new to processing, they have an excellent set of tutorials to help you get started.

The Arduino Contest has been extended! You only have 1 more week to submit your project!

EDIT:
Serial.flush() has changed in Arduino 1.0. You can use the following instead:
while(Serial.available()>0) Serial.read();

You can download the files associated with this episode here:

GNU GPL License Distributed under the GNU General Public (Open-Source) License.
Please Attribute and Share-Alike.

 

147 Comments

  1. Great job on the tutorials. The processing help is awesome for beginners. Showing how to collect data (like temp), display in a graph (on processing), and save to a file would be a really cool tutorial if you are looking for ideas! I would love to see something like that.

  2. hi jerry in the processing window i just get a warning like this
    RXTX Version mismatch
    Jar version = RXTX-2.2pre1
    native lib Version = RXTX-2.2pre2

  3. hi jerry i have a doubt if i m gonna use a joystick it has two axis x and y how will i giv the input via serial communication any ideas pls.. :(

  4. Jeremy,
    I have been stuck on the multiple invalid glitch for a week.
    I’ve tried both combinations “(Serial.flush & while(Serial.available()>0) Serial.read();” and copied and pasted the whole code right from your pde file but I still get repeats.
    I can’t move forward till I understand and correct this problem.
    Could it be that Arduino 1.0 is playing well on my Duemilanove?
    Thanks for your help.
    Jim G.

    • did you solved this problem? please reply Ive been stuck with this too

      • Yes i have the same problem myself. I’d like to add that for the first part of the tutorial (with serial print val that goes to 48 and after correction to 0) it prints two numbers for me.
        for 0 it prints “48″ and just under “10″
        After correction and still for zero: “0″ an just under “38″.

        My code is absolutely the same as the tutorial..?

    • I am having the same issue with my seeduino. Any help would be great

      Devin

    • Serial.flush() has changed in Arduino 1.0. You can use the following instead:
      while(Serial.available()>0) Serial.read();

  5. did anyone solved the problem that Jim G. and anorexis had? i have this problem too… and tried both combinations too :-(

    • I’m stuck myself. I’m a total newbie at this and have not been able to resolve the Serial.flush() issue with Arduino 1.0

      • Try the code with an older version of Arduino, and see if that works for you. I was able to use the replacement code without an issue.

        • I have solved the Flush problem by adding “delay(50)” after “int val = Serial.read()- ’0′;” and replacing flush with “while(Serial.available()>0) Serial.read();”.

          But I was still very curious about the reason, would you please explain this.

          Many thanks.

  6. Pingback: [troubleshooting] analog input from arduino to processing

  7. does it work on mega adk ?
    how ?

  8. For those with the Serial.flush() -> while Serial.read() problem, try adding “Delay(30);” after every digitalwrite, the first while loop, and the Serial.read() loop. That may fix your problem.

  9. Hi, great videos, thanks. Question how did you get the com box you are typing into which displays the results?
    Thanks,
    Steve

  10. hi Jeremy, you was great things. i have a question for you, if i buy this card, can i connect with matlab.
    can we define glabal varriable on this program, and then we can chance other platform this varriable. also matlab is use for guide applications right?

  11. Hi Jez,
    thanks for the tutorials,
    did anyone solve the Serial.flush(); not working?
    Bob.

  12. ‘added’ I have 1.0.1

  13. answer Serial.flush() has changed in Arduino 1.0. You can use the following instead:
    while(Serial.available()>0) Serial.read();

  14. The processing shows me en error on the line -
    port = new Serial(this, “COM3″, 9600);
    the error :
    error inside Serial.()

    And I did everythis right , please help me

  15. could help me with a 3-axis accelerometer + -1.5/6g MMA7361L is that I find no tutorials or information on how to connect, let alone program it arduino please

  16. could help me with a 3-axis accelerometer + -1.5/6g MMA7361L is that I find no tutorials or information on how to connect, let alone program it arduino please

  17. I got the same problem as everybody else. According to the Arduino website, Serial.read reads in the first ‘byte’ of data. So if you input 124, it will first read in 1, then continue with the program. So the 1 should turn on the LED. However, the 2 and 4 don’t get flushed. I think the reason is because the baud rate is much slower than the arduino clock speed. So 2 and 4 are not yet available to be flushed after reading the 1 (so the program loops back to the beginning). If you add a delay(10) before trying to flush the bits, then the Serial communication has enough time to load all of the bytes into the buffer so it can be flushed (btw, I’m assuming there’s a buffer here). In this case 1 will be read, and 2 and 4 will be flushed. However, 124 is supposed to give an invalid input. I found that this can be wored out if you use Serial.parseInt().

  18. I am trying to implement a DAQ using Arduino, so i require higher data transfer speeds. I found out that the Arduino has a maximum baud rate of 115200. Is there any other way to get much higher speeds??

    • Hi, I have the same problem as you do. Did you find out any good solution for it? It would be really nice if you can post it up here!!!

  19. I have two questions here!!
    1. How does Arduino connect to the processing ? I mean we just write these two programs(one in Arduino, one in Processing), and we run the program in Progressing and it is all done?

    2. You mention in the video that we can use other programming languange to ”link “ to Arduino, right? Then how about Java, if I want to use Java to link to Arduino, how can I do, almost the same with the progressing?

    • They are talking over serial. So, if you are running both programs, and the arduino is connected to the right serial port, then they will talk.

      You can use java as long as you can find a Java library that allows you to access a serial port.

  20. After enough struggle I managed to sort my flush problem. Notice that after the delay we flush.

    void loop() {
    while(Serial.available() == 0);
    int data = Serial.read()-’0′;
    int pos = map(data, 0, 7, 0, 180);
    pos = constrain(pos, 0, 180);

    delay(20);
    while(Serial.available()>0) Serial.read();
    Serial.println(data);
    Serial.println(pos);
    S.write(pos);
    }

  21. Hey Jeremy,

    I wanted to input words, have them read and then have them spit back out. Here is my program but when I try to verify it it says “invalid conversion from int to char*”

    char* Input = “” ;

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

    }

    void loop()
    {
    while(Serial.available() == 0) ;
    Input = Serial.read() ;
    Serial.println(Input) ;
    }

    Thanks!

  22. I want to see my graph with line.. not point.. how to do that.. ? my sensor is a accelerometer.. can you help me.. ?

    import processing.serial.*;
    import cc.arduino.*;

    int[] xvals; // Definición de array para almacenar el canal X
    int[] yvals; // Definición de array para almacenar el canal Y
    int[] zvals; // Definición de array para almacenar Z
    int xval; // Valor leído en el canal X
    int yval; // Valor leído en el canal Y
    int zval; // Valor leído en Z
    int arrayindex = 0;

    Arduino arduino;
    int xpin=0; // Designación del PIN para X
    int ypin=1; // Designación del PIN para Y
    int zpin=2; // Designación del PIN para Z
    void setup() // Configuración
    {
    size(500,720); //Tamaño de ventana
    xvals = new int[width];
    yvals = new int[width];
    zvals = new int[width];
    arduino = new Arduino(this,Arduino.list()[1],57600);

    }

    void draw()
    {
    background(0);

    // desplaza la posición del array un lugar a la izquierda
    for(int i=1; i<width; i++)
    {
    xvals[i-1] = xvals[i];
    yvals[i-1] = yvals[i];
    zvals[i-1] = zvals[i];
    }
    // añade el nuevo valor al final del array
    // lee el valor de la entrada (0..1024), divide por 4 (0..255)
    // para mantenerse dentro de límites lienzo de dibujo
    xval = arduino.analogRead(xpin);
    xvals[width-1] = xval/4;
    yval = arduino.analogRead(ypin);
    yvals[width-1] = yval/4;
    zval = arduino.analogRead(zpin);
    zvals[width-1] = zval/4;
    // dibuja el array
    for(int i=1; i<width; i++) {
    stroke(255);
    point(i, 255-xvals[i]); //flip y coordinate so 0 is at bottom
    point(i, 512-yvals[i]); //flip y coordinate so 0 is at bottom
    point(i, 712-zvals[i]); // DUDA!!!!!!
    }
    textAlign(LEFT);
    text("X: "+xval, 200, 30);
    text(5.0*(xvals[width-1]/255.0)+"V",200,60);
    text("Y: "+yval, 200, 256+30);
    text(5.0*(yvals[width-1]/255.0)+"V",200,256+60);
    text("Z: "+zval, 200, 510+30);
    text(5.0*(zvals[width-1]/255.0)+"V",200,510+60);
    }

  23. Hey Jeremy,

    I keep ketting this error message in processing, do you maybe know whats wrong?
    This line is marked: port = new Serial(this, “/dev/tty.usbmodem1a21″, 9600);

    I’m using the code from your tutorial.

    The error message:
    java_home: unrecognized option `–request’
    java.lang.UnsatisfiedLinkError: /Library/Java/Extensions/librxtxSerial.jnilib: no suitable image found. Did find: /Library/Java/Extensions/librxtxSerial.jnilib: no matching architecture in universal wrapper thrown while loading gnu.io.RXTXCommDriver
    Exception in thread “Animation Thread” java.lang.UnsatisfiedLinkError: /Library/Java/Extensions/librxtxSerial.jnilib: no suitable image found. Did find: /Library/Java/Extensions/librxtxSerial.jnilib: no matching architecture in universal wrapper
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1827)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1716)
    at java.lang.Runtime.loadLibrary0(Runtime.java:823)
    at java.lang.System.loadLibrary(System.java:1045)
    at gnu.io.CommPortIdentifier.(CommPortIdentifier.java:83)
    at processing.serial.Serial.(Serial.java:170)
    at processing.serial.Serial.(Serial.java:116)
    at sketch_121025b.setup(sketch_121025b.java:30)
    at processing.core.PApplet.handleDraw(PApplet.java:2103)
    at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:190)
    at processing.core.PApplet.run(PApplet.java:2006)
    at java.lang.Thread.run(Thread.java:680)

    thanks

  24. Pingback: Processing Troubles « Tesla UIs

  25. Hi Jeremy,
    I have an array of values that I am sending from Arduino via serial.
    Problem is I don’t know how to read them in Processing.

    Can you help me out with that please? Here you can see the Arduino code:

    // They’re used here to
    // set pin numbers:
    int buttonPins[] = {
    2,4,7,8,12} ; // the number of the pushbutton pin

    int buttonStates[5]; // variable for reading the pushbutton status
    int i;
    void setup() {
    for (i=0; i<5; i++){
    pinMode(buttonPins[i], INPUT);
    }
    Serial.begin(9600);
    }

    void loop(){
    for (int i=0; i<5; i++){
    // read the state of the pushbutton value:
    buttonStates[i] = digitalRead(buttonPins[i]);

    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonStates[i] == HIGH) {
    Serial.print("ON");
    Serial.println(buttonPins[i]);
    Serial.print ("VAL");
    Serial.println (buttonStates[i]);
    Serial.write (buttonStates[i]);
    }
    else {
    Serial.print("OFF");
    Serial.println(buttonPins[i]);
    Serial.print ("VAL");
    Serial.println (buttonStates[i]);
    Serial.write (buttonStates[i]);
    }

    }
    delay(15000);
    }

  26. I want to activate a 5×5 matrix led using Arduino and I also open serial to monitoring the output. The output in serial monitor was exactly what I wanted. However, the output shown on the matrix led was a little different. When I keep the serial open, the led flickered a little bit. And there is a very tiny light spot shown which was not my intention. Then when I turned off the serial, it didn’t flickering anymore but that tiny spot became completely lightened.

    I look forward to see your reply.

    Thanks ahead.

  27. many many many ……thanxxxx to my friend for video tutorial 6 about Serial port and prodessing.

    it really works . god bless u.

  28. many many thanxxxxxx to my dear friend for video tutorial. it really works. god bless u.

  29. great job…. im kinda new to this audrino thing.. I and working on building a 737 home cocpit and was wondering how i can get this card to interface with fsuipc or microsoft fsx. example. can it be used liek this….. if i turn on a toggle switch can it write the data to fsx and also light up an led at the same time? thanks

  30. Thank You for explanation. That helped me a lot. Hats off!!!

  31. Thanks for doing these video tutorials!

    Your method(s) for relaying the information in my opinion is spot on!
    These really help explain the foundation of arduino process.
    By far the best video tutorials i have seen!

  32. hi…can you help me how to send msg using gsm-gprs shield please forward the program needed….tnx

  33. Hi Jerry, thanks for great videos. May I have your opinions about how to have a localization system for several modular robots with servos controlled by arduino uno???

  34. Great work on the Tutorials there Jeremy, it is great just to sit back and pick up on something I might not of fully understood, Currently I have a project for myself and you might be able to show me the way, I need to send a special character the key via serial TTL I am having a hard time finding out first if it can be done, second how would I send the character, are you able to help as this is the last piece of the puzzle.
    Cheets paul

  35. Pingback: Control Seurat with Arduino | Circle and Square

  36. Hi,

    I’ve been learning from your tutorials a lot, but I have truble connecting my multiple button debounce to processing. Could you help me out please?

    Here is the link for the forum question I made at processing.org:
    https://forum.processing.org/topic/control-this-code-with-arduino

    Thank your very much!

  37. Another way to solve the issue with a 1 being 49 is is by changing int to char. This also works with letters as well and all characters I think

  38. Hi,
    I am trying matlab to turn the led light in arduino, I was wondering if anyone knew the data type of the serial information sent by the serial monitor to the arduino.

    So far I have the following code in matlab

    clear;
    s=serial(‘com3′);
    fopen(s);
    try
    fwrite(s,’1′, ‘uint8′);
    %a= fscan(s);
    fclose(s);
    catch err
    fclose(s);
    disp(‘dint work’)
    end

    I know that arduino is receiving some signal from matlab , but not able to understand it !!!

    any help , would be appreciated

    thanks

  39. fyi …the pin 13 light blinks, when i send 1 signal in matlab

  40. Pingback: Arduino I2C and Processing -Arduino for Projects

Leave a Reply

Required fields are marked *.