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. Question: How can I connect 3 potentiometers to the Arduino and let them control the RGB colors one by one with processing?

  2. May i know how to wirte the source code for appilcation using vb.net to communicate with Arduino Uno? Thanks

  3. Jeremy this tutorial is amazing, i want to adquire an Arduino Uno, but i´m only know vb.net, could you show a tutorial or something related interfacing the arduino Uno with vb.net, like this same tutorial?
    Thanks!

  4. Amazing tutorial series! Finally, someone willing to show us how to go beyond the basics. Thank you.

  5. I want to make a MAME cabinet. Is it possible to use an Arduino Uno for all the button controls? (ie like 12 buttons) Can the Serial handle the speed that I might need to play games with?

  6. thanks for the help,can i ask to do a PHP to Arduino tutorial,it will be very
    helpfull,i need to conect each other to control a light in physical space and
    reverse,thanks again

  7. Already done it ,thanks.I need to make a switch in my site with php to control a light connected with arduino,do i need processing in the middle?I am new with php…any help is
    very important.Thanks for the responce.(I think you are using wordpress here and its leaving user information in the Reply form)

    1. It depends where php is running. Is the PHP server on the same computer that the arduino is connected to? If so, you don’t need an intermediary. If it’s a remote server, you’ll need to make a PHP script on the remote server that saves data to a txt file or a database. A separate program, written in something like python or processing will need to run on your local computer and poll the remote data to update the arduino via serial. You can also skip the local computer completely by getting an ethernet shield for the arduino.

  8. I think i will start with local computer setup and then move on ethernet shield(which is great solution!)A remote server is best for my project,i better start coding,is a long way..Thanks for the advise Jeremy and keep on

  9. Hi Jeremy,

    Im currently combining your tutorial on motors with serial comms. Im trying to read the the value of the motorPin and println to serial. This seems to be working baring the fact that the value is only read at the end of the loop cycle. How can I make read and println the value continuously as it changes?

    Thanks

  10. Thanks for your response but I have managed to solve my issue as well as a couple of others with a days tinkering. Thank you anyway :)) Your tutorials are a huge help!

  11. Hi jeremy. Great tutorials. im sorry if i commented here i dont know where is the topic for ethernet shields. my question is Can i use arduino uno and an ethernet shield because i saw a project which i want to do but it uses a arduino duemilanove but its not available at element 14 only uno? i ask you because this is my first project and i dont want to waste money on my first project. if you dont mind can you give me the idea on this project its a simple on and off but web based so you can control it anywhere at first a want to test it on led then scale up to 220v light and i will use solid state relays. thanks

      1. thanks for your response. im sorry i have another question how can i connect my arduino to a rented web server so i can control my arduino anywhere with internet access and i want to use php because i will make a log in for my project and that log in requires database and if i have database i can do many things to my project like an estimated cost of the electricity bill. thanks!

          1. do you mean the mac address of the webserver? Is it possible to connect my ethernet shield for example if i rent a server at hostgator, can you please give me some source codes? I saw your project the roboclaw and web controlled my project is something like that but not a robot only lights and temperature sensors. thanks again for the reply it helps me alot.

    1. If you want to use your arduino directly as a server, you don’t need to rent one as well. You can actually build a very simple web server on an embedded platform like the arduino. Then serve webpages from it directly, and control your device like that. That’s how I made the roboclaw.

      1. I can make the arduino a web server but how to serve the webpages on the arduino anywhere not only on the local network then control it anywhere via internet.?thanks!

      2. You’ll need to give the arduino a static IP internal to your network. Then forward external port 80 on your router to port 80 on your ARduino’s IP address. Last, use a service like dyndns to assign your ISP-assigned external IP to a URL.

  12. Wow Thanks ! i will message you again if i have problems thanks again but is there way for me to use php for my pages like hosting them on a free hosting because the arduino webserver doesnt support Php? but I will try html first im just asking so when i want to enhance my pages i could use it. THanks!

  13. Hi and realy a nice tutorail, but i have a question.

    I am making an app for my android cellphone so i use processing.
    The app has to do following when i press it a screens goes open and i have a colorwheel. The value that i press has to be send via internet to the arduino ethernet shield. And from there via the PWM outputs to the RGB led. But only my cellphone may make connection to the arduino ethernet shield. How to make this??

    Thanks!

    1. I don’t understand how you’re using processing on your android phone. Why not just build a mobile website that you can visit from your phone that sends data to the arduino?

  14. yes but i have made a server on the arduino via the ethernet shield. and i make connection with that, but i dont know how to send my signal like when i press the value of rgb ( 0, 245,15) or something like that to my arduino ethernet shield. Well i dont know how to make the code that when i press it that that goes to my PWM outputs. If you know what i mean.

    1. Send it as a GET variable to your arduino server, like this: , where 192.168.1.200 would be replaced with the actual local IP address of your arduino server.

  15. thanks,
    And i found out how to do it, I am using the RESTduino, thx to that i just have to set in my processing a line with link(IP adress/port number/variable)

  16. Would you happen to know the best way to interface a scanner with the ardunio, i want to control its on/off functionality using a switch, but i”m having trouble interfacing the scanner with the ardunio

    1. Like a photo scanner? If you just want to turn the scanner on/off with a switch, an arduino is un-needed. just wire a switch inline with the scanner power.

  17. sorry i might be a bit confusing but i mean a gui type switch like an embedded application that enables a user to touch a button on the screen and activate the scanner

    1. You mean to initialize the scan? The scanner probably uses a proprietary protocol for that. You’d have to do some reverse engineering of their software if you wanted to initialize the scan. Some programming languages might have hooks built in for interfacing with scanners.

  18. Hey Jeremy! I really love this tutorial (and all of your others) Very Clear and Helpful. I was just wondering what the next step would be with this project to change the color of a screen that is being hosted

  19. like if i wanted to change the color of a website with the pot, and it would be displayed live on the internet so that people could visit the site and see it being changed by me.

  20. Sir I want material or a link or a tutorial that can help me how to control robot using android app.
    I am very much new to arduino plz help me.

  21. i am writing a c code to send values to an arduino decimilia which will control a motor
    the problem is that everytime i run the program it accepts only one input value after which it crashes with the error message: write access violation
    i am using visual studio .net 2003 to build the application and run it
    any ideas as to what might be wrong?

    thanks

  22. hey I am trying to use camera to detect color!!!
    so I want arduino to have serial communication with camera.
    But somehow is doesn’t work. could it be code problem?

  23. WARNING : RXTX Version mismatch
    Jar version = RXTX-2.pres1
    native lib Version = RXTX-2.2pres2

    This show me when I run the program, everything is ok , but the screen doesn’t chance the color.

  24. Hey jeremy I need move a servo with bend sensor or flex sensor. You have a example? I need a code and connection.

  25. hi Jeremy. Can’t you please help me create a program to control 2 stepper-motor from my computer using serial. thank you

    1. Great work! If you’re willing to post the source code and schematics as open-source online, let me know and I’d be happy to host it on my site.

  26. Jeremy you do amazing tutorials!! thank you a lot for that!
    Well, I got a question, I’m trying to connect : Arduino->Processing->MySQL->PHP.

    I want to upload the data received from the Arduino to my webpage.

    So, Do you know any example code using processing and PHP at the same time? I’m having trouble doing this. Thanks a lot!!

  27. Jeremy Thanks for the tutorials. I am having more fun than my wife can handle with the Arduino Uno. My knowledge of C or C Plus Plus is limited. But your tutorials are really helping me to learn..

    Just a thanks

  28. I need help, I typed in all the code completely right but when I run it I get an error saying that serial port 9600 or COM 23 is in use. How ca I fix this?

  29. Hi Jeremy, how can I replace user value from 0 and 1 to A and B..??? In this tutorial you just write “int val = Serial.read() – 1;”..but when I replaced all 0 to A and 1 to B and once I press A it work with LED ON but if I pressed B it write “Invalid!”..Could you help me how to off the LED with B..

    int ledPin = 13;
    char A;
    char B;

    void setup()
    {
    //Create Serial Object
    Serial.begin(9600);

    pinMode(ledPin, OUTPUT);
    }

    void loop()
    {
    //Have the arduino wait to receive input
    while (Serial.available() == A);

    //Read the Input
    int val = Serial.read() – ‘A’;

    if (val == B)
    {
    Serial.println(“Led is On”);
    digitalWrite(ledPin, HIGH);
    }
    else if (val == A)
    {
    Serial.println(“Led is Off”);
    digitalWrite(ledPin, LOW);
    }
    else
    {
    Serial.println(“Invalid!”);
    }
    Serial.flush();
    }

    1. Change serial.read and serial.available lines back to what they were in my original code, and ensure the letters are treated as chars by putting them in single quotes for your if statements.

  30. hi jeremy, hope you are doing great…, thanks for your contribution to get a great carrier in robotics as an beginner… hope such really nice presentations for future also…

  31. hi..this is sumanth……i am doing a project on “Burglar alarm system” ….can u please suggest me how to interface the web cam on arduino (uno) board plz……it would be helpful for me……
    Lookin forward for your reply…..Thank you

  32. Hi jeremy,your tutorials are really some piece. Here’s what I need your help in – I need a processing code that will be used to generate 4 tab like applets & which will be used to control 2 motors via Arduino . I’am new to processing & hence not getting any single idea about it. I need your help here . Looking forward for your reply. I’ve also tried visiting the processing .org for free tutorials, but didn’t seemed to help me exactly .

    1. I can’t write out programs for everybody unfortunately, and I’m not an expert a processing myself. I suggest you start playing with it, and ask specific questions on the processing forum.

  33. Hi Jeremy,

    I am from Germany and my English is rather poor.
    I just started to use Arduino and now I found your tutorials.
    You make a very great work!
    I dont speak very good English, but I understood nearly everything.
    It is a pleasure to follow your instructions – especially for beginners like me.

    Thank you very much!

  34. Hi Jeremy,

    I’ve been trying to do a serial comm between the arduino and processing but … it keeps getting exception on serial comm is already in used.

    Any solution for it ???
    *The baud rate and port is set to the same for both

    Thanks alot !

  35. hi jeremy, can u please guide for my project?…i am trying to build LED matrix using arduino clone and interface it using visual basic…the problem is i don’t know hot to connect arduino and VB…can u please guide me in constructing the code?…

    thanks a lot…

  36. Hi, jer. Is it possible to use arduino microcontroller that already has motor driver . Romeo all in one 328, how can i attach rc
    Receiver with spektrum . Is it just direct plug receiver signal into the serial communication tx.

    Any code that you .Have encountered. If there is 2 motors for differential drive should we need to use 2 channel meaning 2 tx assuming i power the receiver directly.

    Appreciate your views on this

    1. I haven’t used that, but if it’s serial, you’ll just need to plug it into the arduino’s UART interface. Keep in mind that you will need to unplug it while programming and vice versa.

  37. Great tutorials! Wow where do you find the time?
    I want to use the arduino to read the data from a USB mouse then output this data to the computer.
    Can I use the UNO of will I have to go to the Mega?
    Thanks
    Bill

  38. Hi Jeremy,

    I’m trying to use this method to use two potentiometers and a button to make an Etch-A-Sketch programme, with the potentiometers controlling the X and Y points of the line and the button to clear the canvas.
    I can make a drawing programme in Processing with the mouse controlling the line and have also managed to write one that displays the value of the potentiometers. However, I’m really don’t know how to map the X and Y values to the potValue from the Arduino sketch. Would you be able to provide some guidance please?
    Thanks

  39. Hi Jeremy,
    I’m doing a project in arduino using ultrasonic sensor.
    I just want to ask how can I connect it to vb.net so that vb.net can read the output of Arduino? Arduino Sends 1 & 0 in serial monitor.Just want to vb.net to post it in its window..
    Hoping for your positive reply..
    Thanks ..

  40. Hi jeremy,

    I tried to get some sensor data displayed from my arduino using processing. But am not getting the same values which i get in the serial monitor of arduino in the processing graph..could u help me on this?Could you pls guide me on this..!

  41. Hi Jeremy,
    I want to communicate between two Arduino Uno boards through XBee. The first board senses an analog signal through the analog input pins and converts it into a PWM and transmits through the Xbee. In the receiver end, i want to receive that PWM and convert it into the same Analog voltage variations. is it possible to do so? if it is, then can you please give me the link for the code of the receiver end?

  42. Hi Jeremy

    How do I use the serial port to send the value 0 or 1 without pressing enter?
    I want to control two motors left and right, both together t moves forward
    And one at the time to turn.
    So far I updated you code from this tutorial, and it work fine but I have to press enter all the time.
    I will appreciate all the help I can get.
    Thank in advance

  43. sir jeremy,can u give me some knowledge about our project,,pur project is Automatic Vehicle Identification using RFID,im a bit confuse on what should be writtin in Vb 2006 to control tHe gate barrier(SERVO MOTOR)

  44. hi Jeremy,
    can you show me some example how to use void SerialEvent()? I wrote an application like this as follow, but it doesn’t work, i don’t know why.

    boolean bInput = false;

    void setup()
    {

    Serial.begin(9600);
    }

    void loop()
    {

    if (bInput)
    {
    Serial.println(“has input”);
    bInput = false;
    }

    }

    void serialEvent(){

    while (Serial.available())
    {
    bInput = true;
    }
    }

  45. Hi Jeremy! Our group is working on an SMS controller which can turn an appliance ON and OFF. It is composed of a GSM module (SIM900D), microcontroller (Gizduino), relay card, and power analyzer. Don’t mind the relay card and the power analyzer, our problem lies on the communication of the microcontroller and the GSM module. Here’s how it works: the user will send a command via SMS using his mobile phone (ON, OFF or ASK), microcontroller will digitalWrite the pins connected to the relay and power analyzer HIGH if it receives a text “ON” and will digitalWrite the pins LOW if an “OFF” message is received. We were told that we need to interface AT commands in Arduino, can you help us with this? You may email me at [email protected] Thanks a lot! :)

  46. Pingback: Must-See Beginner Tutorials For Arduino | dev.SquareCows.com
  47. Pingback: Ellentriek » Blog Archive » Arduino, Processing and sensors Part 2

Leave a Reply to Jordan 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