{"id":4783,"date":"2012-05-30T23:30:35","date_gmt":"2012-05-31T03:30:35","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=4783"},"modified":"2013-04-23T21:33:43","modified_gmt":"2013-04-24T01:33:43","slug":"chipkit-tutorial-2-serial-communication-with-pc","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/chipkit-tutorial-2-serial-communication-with-pc\/","title":{"rendered":"chipKIT Tutorial 2: Serial communication with PC"},"content":{"rendered":"<p>The PIC32 processor on the chipKIT Uno32 board provides two hardware serial ports. One of these is used by the on-board FTDI chip to create an USB-UART interface that allows the MPIDE tool running on the PC to communicate with the Uno32 board through an USB port. In this tutorial, we will use the same USB-UART interface to demonstrate a two-way asynchronous serial communication between the Uno32 and the PC. The Uno32 board receives a serial command through the PC&#8217;s USB port to turn an external LED on and off. In return, the Uno32 board acknowledges to the PC by sending the new state of the LED.<\/p>\n<div id=\"attachment_4788\" style=\"width: 580px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialTransferOP2.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4788\" class=\"size-full wp-image-4788\" title=\"SerialTransferOP2\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialTransferOP2.jpg\" alt=\"\" width=\"570\" height=\"450\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialTransferOP2.jpg 570w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialTransferOP2-550x434.jpg 550w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><p id=\"caption-attachment-4788\" class=\"wp-caption-text\">Controlling an external LED with serial command from PC<\/p><\/div>\n<p><!--more--><\/p>\n<p><strong>Theory<\/strong><\/p>\n<p>The chipKIT Uno32 board features two hardware serial ports:\u00a0<strong>UART port1<\/strong>\u00a0and\u00a0<strong>UART port2.\u00a0<\/strong>Pin 0 and Pin 1 serves as <strong>RX<\/strong> and <strong>TX<\/strong> for\u00a0<strong>UART port1. <\/strong>These pins are\u00a0also used by the FT232R USB-to-Serial converter on the board. Similarly,\u00a0<strong>UART port2<\/strong>\u00a0uses Pin 39 and Pin 40 for <strong>RX<\/strong> and <strong>TX<\/strong> functions, respectively. We will use <strong>UART port1<\/strong> in this tutorial as it allows serial communication with the PC over an USB port.<\/p>\n<p>The MPIDE has got a built-in serial monitor tool that allows you to watch any data that the Uno32 sends out its serial port. It also let you send characters from the PC back to the Uno32 board over the same serial interface. You can open the\u00a0serial monitor window by clicking on the serial monitor button in the toolbar. In a Windows version of MPIDE, the serial monitor window looks like this.<\/p>\n<div id=\"attachment_4786\" style=\"width: 580px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialMonitor.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4786\" class=\"size-full wp-image-4786\" title=\"SerialMonitor\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialMonitor.jpg\" alt=\"\" width=\"570\" height=\"528\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialMonitor.jpg 570w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialMonitor-550x509.jpg 550w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><p id=\"caption-attachment-4786\" class=\"wp-caption-text\">MPIDE serial monitor window<\/p><\/div>\n<p>There\u00a0is a blank text box at the top where you enter text that you want to send to the chipKIT Uno32 board. Below that is a white space where the serial data sent by the Uno32 board will be displayed. On the bottom right side, you can select the Baud Rate (speed) of the serial communication. The default setting for Baud Rate is 9600 baud but you can select other baud rates from the drop-down menu. Note that the chipKIT board must be configured to the same baud rate for serial communication. You must free up the serial port before returning back to the programming mode of MPIDE. You can do that by closing the serial monitor window.<\/p>\n<p><strong>Circuit setup<\/strong><\/p>\n<p>The experimental circuit setup for this tutorial is very simple. All you need is an LED and a 220? series resistor. The LED will be controlled from a serial command received from the PC. The picture below shows the LED connections to the Uno32 board. The LED is connected to Pin 2. Note that during serial communication, digital pins 0 (RX) and 1 (TX) are occupied and cannot be used for other I\/O purposes.\u00a0The board is connected to an USB port and receives power from there.<\/p>\n<div id=\"attachment_4824\" style=\"width: 582px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/T3_Circuit1.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4824\" class=\"size-full wp-image-4824\" title=\"T3_Circuit\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/T3_Circuit1.jpg\" alt=\"\" width=\"572\" height=\"510\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/T3_Circuit1.jpg 572w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/T3_Circuit1-550x490.jpg 550w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><\/a><p id=\"caption-attachment-4824\" class=\"wp-caption-text\">An external LED controlled through serial commands from a PC<\/p><\/div>\n<p><strong>Writing sketch<\/strong><\/p>\n<p>In your setup function, you need to tell the Uno32 to open serial port. This is done by <strong>Serial.begin()<\/strong> command. The number within the parenthesis sets the baud rate of the serial communication.\u00a0The typical baud rate for communicating with the computer is 9600 but other speeds are also supported. Next you need to define Pin 2 as output, which can be done using <strong>pinMode()<\/strong> function.<\/p>\n<p>In order to send data from Uno32 to PC, MPIDE mainly offers two commands: <strong>Serial.print()<\/strong> and <strong>Serial.println()<\/strong>. Both of them send whatever is inside the parenthesis to the PC through the serial port. On PC&#8217;s side, the received data is displayed on the serial monitor window. However, the difference between using <strong>print<\/strong> and <strong>println<\/strong> is the way they print the data out the serial port. When you use <strong>Serial.print()<\/strong>, then the cursor remains at the end of whatever you just printed. So, if there is another print command following that then the new data will show up in the serial monitor window at the end of the previously received data, along the same line. On the other hand, <strong>Serial.println()<\/strong> prints data with a linefeed at the end that drops the cursor down to the next line after the data has been printed. Both of these commands can take many forms, which are described very well in the <a href=\"http:\/\/arduino.cc\/en\/Reference\/serial\" target=\"_blank\">Arduino serial library page<\/a>.<\/p>\n<p>Here&#8217;s the complete program to control the external LED with a command byte sent from the PC over the serial line. If the Uno32 receives &#8216;1&#8217; from the PC, it turns the LED on. If it receives &#8216;0&#8217;, the LED is turned off. In response to the command, the Uno32 prints the new status of the LED back to the PC.<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"c\" style=\"font-family:monospace;\"><span style=\"color: #808080; font-style: italic;\">\/*\n  Tutorial 2:  Serial interface between PC and chipKIT \n  Description: chipKIT UNO32 receives commands from a PC through serial port\n               and control an external LED connected to pin 2\n  Board: chipKIT UNO32\n *\/<\/span>\n&nbsp;\n<span style=\"color: #993333;\">const<\/span> <span style=\"color: #993333;\">int<\/span> LED_PIN <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">2<\/span><span style=\"color: #339933;\">;<\/span>  <span style=\"color: #666666; font-style: italic;\">\/\/ LED is connected to digital I\/O pin 2<\/span>\n<span style=\"color: #993333;\">int<\/span> SERIAL_DATA<span style=\"color: #339933;\">;<\/span>        <span style=\"color: #666666; font-style: italic;\">\/\/ Variable to store incoming serial byte<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> setup<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n Serial.<span style=\"color: #202020;\">begin<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">9600<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>  <span style=\"color: #666666; font-style: italic;\">\/\/ Sets the baud rate for serial transmission <\/span>\n pinMode<span style=\"color: #009900;\">&#40;<\/span>LED_PIN<span style=\"color: #339933;\">,<\/span> OUTPUT<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #666666; font-style: italic;\">\/\/ Send these messages to PC only once <\/span>\n Serial.<span style=\"color: #202020;\">println<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #ff0000;\">&quot;Serial interface between PC and chipKIT&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n Serial.<span style=\"color: #202020;\">println<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #ff0000;\">&quot;1: LED ON, 0: LED OFF&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n digitalWrite<span style=\"color: #009900;\">&#40;<\/span>LED_PIN<span style=\"color: #339933;\">,<\/span> LOW<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>       <span style=\"color: #666666; font-style: italic;\">\/\/ LED is off initially<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>  \n&nbsp;\n<span style=\"color: #993333;\">void<\/span> loop<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n&nbsp;\n  <span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">&#40;<\/span>Serial.<span style=\"color: #202020;\">available<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">&gt;<\/span><span style=\"color: #0000dd;\">0<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>        <span style=\"color: #666666; font-style: italic;\">\/\/ Check if serial data is available.<\/span>\n   SERIAL_DATA <span style=\"color: #339933;\">=<\/span> Serial.<span style=\"color: #202020;\">read<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ If yes, read the incoming byte<\/span>\n   <span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">&#40;<\/span>SERIAL_DATA <span style=\"color: #339933;\">==<\/span> <span style=\"color: #ff0000;\">'1'<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>        <span style=\"color: #666666; font-style: italic;\">\/\/ Check if the received byte is '1'  <\/span>\n     digitalWrite<span style=\"color: #009900;\">&#40;<\/span>LED_PIN<span style=\"color: #339933;\">,<\/span> HIGH<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>  <span style=\"color: #666666; font-style: italic;\">\/\/ If so, turn the LED ON<\/span>\n     Serial.<span style=\"color: #202020;\">println<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #ff0000;\">&quot;LED is ON&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>  <span style=\"color: #666666; font-style: italic;\">\/\/ and acknowledge.<\/span>\n   <span style=\"color: #009900;\">&#125;<\/span>\n   <span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">&#40;<\/span>SERIAL_DATA <span style=\"color: #339933;\">==<\/span> <span style=\"color: #ff0000;\">'0'<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>        <span style=\"color: #666666; font-style: italic;\">\/\/ If the received byte is '0',<\/span>\n     digitalWrite<span style=\"color: #009900;\">&#40;<\/span>LED_PIN<span style=\"color: #339933;\">,<\/span> LOW<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>   <span style=\"color: #666666; font-style: italic;\">\/\/ turn the LED OFF and<\/span>\n     Serial.<span style=\"color: #202020;\">println<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #ff0000;\">&quot;LED is OFF&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ acknowledge.<\/span>\n   <span style=\"color: #009900;\">&#125;<\/span>\n <span style=\"color: #009900;\">&#125;<\/span>    \n<span style=\"color: #009900;\">&#125;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p><strong><a href=\"http:\/\/www.embedded-lab.com\/uploads\/chipKIT\/T3_SerialPC.zip\">Download the sketch file<\/a><\/strong><\/p>\n<p>The <strong>Serial.begin(9600)<\/strong> opens the serial port of Uno32 and sets the baud rate of 9600. You can see there are two<strong> Serial.println()<\/strong> commands inside the setup function. They print two text messages over the serial line, which are displayed on the serial monitor. Since setup function is executed only once, these messages are also printed only once at the beginning. Inside the loop function is used the <strong>Serial.available()<\/strong> command, which checks to see if any characters have been sent down the serial port by the PC. It returns the number of characters available for reading from the serial port. Once there&#8217;s data available, it can be read using <strong>Serial.read()<\/strong> command. The Uno32 then compares the received byte with &#8216;1&#8217; and &#8216;0&#8217; to take proper action with the LED.<\/p>\n<p><strong>Output<\/strong><\/p>\n<p>Verify and upload the above sketch into the Uno32 board. Open the serial monitor window and make sure it is configured to use a baud rate of 9600. Reset the Uno32 board and wait until it gets out of the bootloader mode so that it could execute the user program. You will read two text lines (&#8220;<em>Serial interface between PC and chipKIT<\/em>&#8221; and &#8220;<em>1: LED ON, 0: LED OFF<\/em>&#8220;) on the serial monitor that were printed by the Uno32. If you type in &#8216;1&#8217; in the send box at the top and click on send button, it will be sent over the serial line. The Uno32, on\u00a0receiving the command byte, will turn the LED on. If a &#8216;0&#8217; is sent, the LED will be turned off. The Uno32 board acknowledges to the receieved command by printing back the new state of LED on the serial monitor. If the received character is other than &#8216;0&#8217; and &#8216;1&#8217;, then it will be ignored by Uno32.<\/p>\n<div id=\"attachment_4785\" style=\"width: 568px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialCommandOP.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4785\" class=\"size-full wp-image-4785\" title=\"SerialCommandOP\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialCommandOP.jpg\" alt=\"\" width=\"558\" height=\"434\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialCommandOP.jpg 558w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialCommandOP-550x427.jpg 550w\" sizes=\"auto, (max-width: 558px) 100vw, 558px\" \/><\/a><p id=\"caption-attachment-4785\" class=\"wp-caption-text\">Controlling LED through serial command<\/p><\/div>\n<div id=\"attachment_4787\" style=\"width: 580px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialTransferOP.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4787\" class=\"size-full wp-image-4787\" title=\"SerialTransferOP\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialTransferOP.jpg\" alt=\"\" width=\"570\" height=\"461\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialTransferOP.jpg 570w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/04\/SerialTransferOP-550x444.jpg 550w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><p id=\"caption-attachment-4787\" class=\"wp-caption-text\">LED is turned on and off through a command received from PC over serial interface<\/p><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The PIC32 processor on the chipKIT Uno32 board provides two hardware serial ports. One of these is used by the on-board FTDI chip to create an USB-UART interface that allows the MPIDE tool running on the PC to communicate with the Uno32 board through an USB port. In this tutorial, we will use the same USB-UART interface to demonstrate a two-way asynchronous serial communication between the Uno32 and the PC. The Uno32 board receives a serial command through the PC&#8217;s USB port to turn an external LED on and off. In return, the Uno32 board acknowledges to the PC by<\/p>\n","protected":false},"author":1,"featured_media":4785,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[401],"tags":[451,402,427],"class_list":["post-4783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chipkit","tag-arduino-serial","tag-chipkit-uno32","tag-serial-transfer"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/4783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=4783"}],"version-history":[{"count":25,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/4783\/revisions"}],"predecessor-version":[{"id":6263,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/4783\/revisions\/6263"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/4785"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=4783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=4783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=4783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}