BLOG > Tutorial 6 for Arduino: Serial Comm and Processing

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.

 

252 comments

  1. When try to do what you did in your video in (3:37-6:58), I’m still not getting back a 1 and 0.
    My code is this:
    void setup() {
    Serial.begin(9600);
    }

    void loop() {
    while (Serial.available() == 0);
    int val = Serial.read() – ‘0’;
    Serial.println(val);
    }
    When I send a 1, I get:
    49
    10

    and when I send a 0, I get:
    48
    10

    Please explain why your code is not working.

    1. Hi,
      i am not sure but looks like you are using the wrong “single brackets”?
      For a char it’s neither ´ nor `, but ‘
      On german keyboard layout it’s over the #, so by SHIFT+# you should get the right one.
      As far as i could see by google search, on US keyboards, it should be 2 keys to the left of the enter key;
      and not the key in the top left corner.

  2. Hi, is there any chance someone could please tell me where I went wrong? I had the same issue with it not sending me back 0 and 1, and now it’s the same when trying to switch the LED on and off. Here’s my code:

    //Simple program to send one character to the Arduino, and have it send one character back
    //To confirm that the Arduino got it so we can use it to do something more interesting

    int ledPin = 13; //set to pin13

    void setup()
    {
    //Create Serial Object
    Serial.begin(9600); // Allows you to communicte with the Arduino via serial
    //9600 is the ‘bod rate’, the communication speed, this is a basic number to use

    pinMode(ledPin, OUTPUT); //the led is the output, makes the pin an output
    }

    void loop()
    {
    //Have the Arduino wait to recieve inpit
    while (Serial.available() == 0);//wait until we get something from the computer, checks to see if there’s something waiting for the Arduino, loops the line until it gets something from computer

    //Read the Input sent
    int val = Serial.read() – ‘0’; //once it does get something it is read into a variable, the 0 is telling the computer to use “char” language instead of another (reference ascii table)

    if(val == 1)
    {
    Serial.println(“Led is on”);
    digitalWrite(ledPin, HIGH);
    }
    else if(val == 0)
    {
    Serial.println(“Led is off”);

    digitalWrite(ledPin, LOW);
    }

    else
    {
    Serial.println(“Invalid!”);
    }

    }

    Thanks for any help with this!

  3. If I put “1” in to the serial monitor, it comes back with “49 10”

    Here is the first code without all the notes btw:

    void setup()
    {

    Serial.begin(9600);

    }

    void loop() {

    while (Serial.available() == 0);

    int val = Serial.read(); – ‘0’;

    Serial.println(val);

    }

    1. Okay nevermind sorry, I just found out about flush…

      although with the copy and pasted the downloaded code, it is now saying invalid in between everything even though it still works haha

    2. Here is my code(loop part) with flush.
      When I send 1 it returns
      1
      -38
      When I send 0 it returns
      0
      -38
      I don’t know from where this -38 comes!?

      while (Serial.available() == 0);

      val = Serial.read() – ‘0’;
      Serial.flush();

      Serial.println(val);
      delay(100);

      1. Try to set the option “no line-end” at the bottom of the console (left side of baudrate).

  4. Okay nevermind sorry, I just found out about flush…

    although with the copy and pasted the downloaded code, it is now saying invalid in between everything even though it still works haha

    1. Although I add flush it works same way just you mentioned :( Can you share your right code please?

  5. Try to set the option “no line-end” at the bottom of the console (left side of baudrate).

Leave a Reply to Marios Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Advertisement