title: RS-232 communication using an Arduino Duemilanove and the Cutedigi RS-232 interface
body: <p>I'm having some trouble getting my Arduino microcontroller to read <a href="http://en.wikipedia.org/wiki/RS-232" rel="nofollow">RS-232</a> signals. My project requires me to read data that is being outputted by an air quality monitor.</p>
  
  <p>My components:</p>
  
  <ul>
  <li><a href="http://arduino.cc/en/Main/ArduinoBoardDuemilanove" rel="nofollow">Arduino Duemilanove</a></li>
  <li><a href="http://www.cutedigi.com/product_info.php?products_id=4329" rel="nofollow">Cutedigi RS-232 interface</a></li>
  </ul>
  
  <p>To test if the serial communications is working properly, I found some example code on the Arduino website. This is the exact code that I am running:</p>
  
  <pre><code>//Created August 23 2006
  //Heather Dewey-Hagborg
  //http://www.arduino.cc
  
  #include &lt;ctype.h&gt;
  
  #define bit9600Delay 84
  #define halfBit9600Delay 42
  #define bit4800Delay 188
  #define halfBit4800Delay 94
  
  byte rx = 0;
  byte tx = 1;
  byte SWval;
  
  void setup() {
      pinMode(rx,INPUT);
      pinMode(tx,OUTPUT);
      digitalWrite(tx,HIGH);
      digitalWrite(13,HIGH); //turn on debugging LED
      SWprint('h');  //debugging hello
      SWprint('i');
      SWprint(10); //carriage return
  }
  
  void SWprint(int data)
  {
      byte mask;
      //startbit
      digitalWrite(tx,LOW);
      delayMicroseconds(bit9600Delay);
      for (mask = 0x01; mask&gt;0; mask &lt;&lt;= 1) {
          if (data &amp; mask){ // choose bit
              digitalWrite(tx,HIGH); // send 1
          }
          else{
              digitalWrite(tx,LOW); // send 0
          }
          delayMicroseconds(bit9600Delay);
      }
      //stop bit
      digitalWrite(tx, HIGH);
      delayMicroseconds(bit9600Delay);
  }
  
  int SWread()
  {
      byte val = 0;
      while (digitalRead(rx));
      //wait for start bit
      if (digitalRead(rx) == LOW) {
          delayMicroseconds(halfBit9600Delay);
          for (int offset = 0; offset &lt; 8; offset++) {
              delayMicroseconds(bit9600Delay);
              val |= digitalRead(rx) &lt;&lt; offset;
          }
          //wait for stop bit + extra
          delayMicroseconds(bit9600Delay);
          delayMicroseconds(bit9600Delay);
          return val;
      }
  }
  
  void loop()
  {
      SWval = SWread();
      SWprint(toupper(SWval));
  }
  </code></pre>
  
  <p>I changed the RX and TX pins to 0 and 1 respectively, because these are the pins that the Cutedigi RS-232 chip uses. Now, when I open a terminal window and type characters in, I get garbled symbols and letters (like this: <code>¾_ò_òòËÌßÌËßÌÊÌòyofyofsæóÙöÇ æü æ</code>).</p>
  
  <p>According to the example code website, if I type <code>abcdefg</code>, the terminal window should display <code>ABCDEFG</code>.</p>
  
  <p>Why would this be the case? I have set the baud rate to 9600, as specified in the <a href="http://www.arduino.cc/en/Tutorial/Sketch" rel="nofollow">sketch</a>, but I'm still getting issues. Resetting the Arduino doesn't seem to help either - I still get garbled text.</p>