Tutorial on Arduino Serial Communication

Control Boards, Controllers, Tethers, Ect.
Post Reply
User avatar
Bored_Engineer
Posts: 16
Joined: Dec 18th, 2010, 5:35 pm
Location: Texas

Tutorial on Arduino Serial Communication

Post by Bored_Engineer »

Hello all,
This is a tutorial for programming an arduino to communicate via serial connection. The arduino in most cases uses a USB connection, this is actually a virtual serial connection and a power supply (not really a USB connection). On Windows machines this serial connection shows up as COM1 or in my case COM3, but theoretically it can be any number precede by COM. In the Arduino IDE, you need to tell the IDE where to upload the code to; so go to the Tools drop-down and look for the serial port option, this should show which serial port the arduino is on (again, in my case COM3). Now select that port and you're ready to go. Paste the following code into a new page of the IDE:

Code: Select all

// This is an example of the Arduino side of serial communications for an ROV,
// but instead of pumps, it turns LEDs off and on.
// The LEDs are attached to pins 2 through 5, and the code can be modified to 
// support up to pin 13 on the digital side, and the analog pins. 
// Pins 0 and 1 however are part of the 
// serial communication protocal, so these aren't used.
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
   // initialize the LED pins:
      for (int thisPin = 2; thisPin < 6; thisPin++) {
        pinMode(thisPin, OUTPUT);
      }
}

void loop() {
  // read the sensor:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    // do something different depending on the character received.  
    // The switch statement expects single number values for each case;
    // in this exmaple, though, you're using single quotes to tell
    // the controller to get the ASCII value for the character.  For
    // example 'a' = 97, 'b' = 98, and so forth:

    switch (inByte) {
    case 'a':    
      digitalWrite(2, HIGH);
      break;
    case 's':    
      digitalWrite(3, HIGH);
      break;
    case 'd':    
      digitalWrite(4, HIGH);
      break;
    case 'f':    
      digitalWrite(5, HIGH);
      break;
    default:
      // any other key pressed will turn the lights off..
      for (int thisPin = 2; thisPin < 6; thisPin++) {
        digitalWrite(thisPin, LOW);
      }
    }
  }
}

After you paste it, Compile it using the Verify button (the top left button with the arrow.), after it finishes compiling, plug in your arduino if you haven't already, and press the upload button (the second to the right with the right-facing arrow). When the program is finished uploading, open up the serial monitor (the right most button).

You need to plug some LEDs in to pins 2 through 5 with some 220 ohm resistors going from the LED's shorter leg to ground.
(see the image for wiring..)

Now you can start sending the arduino commands to turn the LEDs off and on. This program uses 'a', 's', 'd', 'f', but you can modify this to use however many pins are needed and whatever keys to control whatever and however many pins you need. To send a command, simply click in the empty input box to the left of the Send button in the Serial Monitor window, and press 'a' and send (or just hit enter) to turn on pin 2, send 's' to turn on pin 3 and so on.. if you send any other character other than 'a', 's', 'd', 'f' it will turn all the pins off.

Hope this helps ya'll with setting up an arduino :D . After I figure out how to make a GUI in Python for the computer side of Serial communications I will upload a tutorial on that :ugeek: .

Cheers
Attachments
The LED set-up.
The LED set-up.
ArduinoSchematic1.png (12.64 KiB) Viewed 4216 times
User avatar
SoakedinVancouver
Posts: 117
Joined: Dec 31st, 2010, 9:38 pm

Re: Tutorial on Arduino Serial Communication

Post by SoakedinVancouver »

Thank you very much for a very useful Christmas present!

Stephen Young
User avatar
Bored_Engineer
Posts: 16
Joined: Dec 18th, 2010, 5:35 pm
Location: Texas

Re: Tutorial on Arduino Serial Communication

Post by Bored_Engineer »

You're welcome! I'm still having trouble with getting a gui going in python, can't get it to read any sensor data, but once I figure it out, I'll get it posted.
Sky
Posts: 13
Joined: Dec 31st, 2010, 5:54 am

Re: Tutorial on Arduino Serial Communication

Post by Sky »

Hey all
Thanks for the tutorial

Hope you dont mind me asking on your thread but didnt want to create a new thread and seemed kinda appropriate here so
If i use a older arduino with a real serial port(RS232) and connect a RS485 to RS232 converter to the arduino so i can connect to my pc via RS485
Would the converter have to be full duplex or would a half duplex converter also work?

I am considering using the following converter but i dont know if it will work since it says that the converter is only half Duplex
http://aquaticus.info/rs485_to_rs232

Thanks
Post Reply