








{"id":71,"date":"2020-10-12T20:07:58","date_gmt":"2020-10-12T20:07:58","guid":{"rendered":"https:\/\/osoyoo.info\/?p=71"},"modified":"2020-10-13T22:02:55","modified_gmt":"2020-10-13T22:02:55","slug":"arduino-basic-lesson-8-use-serial-port-to-control-led","status":"publish","type":"post","link":"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-8-use-serial-port-to-control-led\/","title":{"rendered":"Hardware Programming with Arduino IDE &#8211; Lesson 8: Use Serial Port to control LED"},"content":{"rendered":"<p><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-lesson-7-blink\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Previous.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-tutorial-directory\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Tutorial.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-9-flowing-water-light\/\"><img loading=\"lazy\" class=\"alignright\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Next.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/p>\n<div align=\"center\">\n<div align=\"center\"><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/icon\/authorized_retailers.png\" \/><\/div>\n<div align=\"center\"><\/div>\n<table>\n<tbody>\n<tr>\n<td>Buy from US<\/td>\n<td>Buy from UK<\/td>\n<td>Buy from DE<\/td>\n<td>Buy from IT<\/td>\n<td>Buy from FR<\/td>\n<td>Buy from ES<\/td>\n<td>Buy from JP<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/www.amazon.com\/dp\/B08J7DKJK9?ref=myi_title_dp\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/us_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.co.uk\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/icon\/uk_buy_now.png\" alt=\"\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.de\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/de_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.it\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/it_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.fr\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/fr_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.es\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/es_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.co.jp\/dp\/B08J7TLP48\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/jp_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2><strong>Content<\/strong><\/h2>\n<ol>\n<li><a href=\"#1\">Introduction<\/a><\/li>\n<li><a href=\"#2\">Preparations<\/a>\n<ul>\n<li><a href=\"#2.1\">Hardware<\/a><\/li>\n<li><a href=\"#2.2\">Software<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#3\">Connection<\/a><\/li>\n<li><a href=\"#4\">Upload Sketch<\/a><\/li>\n<li><a href=\"#5\">Arduino Code Analysis<\/a><\/li>\n<li><a href=\"#6\">Connecting, Receiving and Sending Data to the Arduino<\/a><\/li>\n<\/ol>\n<h1 id=\"1\">Introduction<\/h1>\n<p>In this lesson, I will be going through on how to setup the Arduino serial monitor so you can debug and interact with a program running on the Arduino. This is pretty simple but it can be a bit confusing at first especially if you\u2019re new to programing and the Arduino.<\/p>\n<p>As part of this lesson we will build a simple circuit to demonstrate how you can both receive and send commands over the serial monitor.<\/p>\n<h1 id=\"2\">Preparations<\/h1>\n<h2 id=\"2.1\">Hardware<\/h2>\n<ul>\n<li>Osoyoo UNO Boars (Fully compatible with Arduino UNO rev.3) x 1<\/li>\n<li>OSOYOO Magic I\/O Shield for Arduino x1<\/li>\n<li>Red LED Module x 1<\/li>\n<li>OSOYOO 3-Pin PnP cable x 1<\/li>\n<li>USB Cable x 1<\/li>\n<li>PC x 1<\/li>\n<\/ul>\n<h2 id=\"2.2\">Software<\/h2>\n<p>Arduino IDE (version 1.6.4+)<\/p>\n<h1 id=\"3\">Connection<\/h1>\n<p>First, please plug Osoyoo Magic I\/O shield into UNO board\uff1a<\/p>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson4\/UNO%2BBase-min.png\" \/><\/p>\n<p>Then connect the LED module to the D3 port of the Magic I\/O shield with a 3-pin PNP cable as below:<\/p>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson3\/Base-Red-LED-min.png\" \/><\/p>\n<h1 id=\"4\">Upload Sketch<\/h1>\n<p>After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled <strong>PWR<\/strong>) should go on.<\/p>\n<h2>Code Program<\/h2>\n<p>You can <a href=\"https:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/code\/Lesson9_The_Serial_Monitor.rar\" target=\"_blank\" rel=\"noopener noreferrer\">download the sketch from this link<\/a> or copy below code to your Arduino IDE window:<\/p>\n<pre><span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">redLedPin<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">3<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/ Pin Red LED is connected to<\/span> \r\n<span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">count<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">;<\/span> \r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">setup<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{ \r\n<\/span><span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">redLedPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">OUTPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/Set led pin to output<\/span> \r\n<b><span style=\"color: #d35400;\">Serial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">begin<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">9600<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/Set serial to the 9600 band<\/span> \r\n<span style=\"color: #5e6d03;\">while<\/span> <span style=\"color: #000000;\">(<\/span><span style=\"color: #434f54;\">!<\/span> <b><span style=\"color: #d35400;\">Serial<\/span><\/b><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/ Allow serial to initialise <\/span> \r\n<b><span style=\"color: #d35400;\">Serial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #005c5f;\">\"Enter Y to turn on the LED:\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> \r\n<span style=\"color: #000000;\">}<\/span> \r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">loop<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">) <\/span><span style=\"color: #000000;\">{<\/span> \r\n<span style=\"color: #5e6d03;\">if<\/span> <span style=\"color: #000000;\">(<\/span><b><span style=\"color: #d35400;\">Serial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">available<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\"> {<\/span> <span style=\"color: #00979c;\"> \r\nchar<\/span> <span style=\"color: #000000;\">ch<\/span> <span style=\"color: #434f54;\">=<\/span> <b><span style=\"color: #d35400;\">Serial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">read<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #5e6d03;\"> \r\nif<\/span> <span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">ch<\/span> <span style=\"color: #434f54;\">==<\/span> <span style=\"color: #00979c;\">'y'<\/span><span style=\"color: #434f54;\">||<\/span><span style=\"color: #000000;\">ch<\/span> <span style=\"color: #434f54;\">==<\/span> <span style=\"color: #00979c;\">'Y'<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\"> \r\n{<\/span> <span style=\"color: #d35400;\"> \r\ndigitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">redLedPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">HIGH<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">; <\/span><b><span style=\"color: #d35400;\"> \r\nSerial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #005c5f;\">\"You have turned on the LED!!\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <b><span style=\"color: #d35400;\"> \r\nSerial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">print<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #005c5f;\">\"The LED was off for \"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <b><span style=\"color: #d35400;\"> \r\nSerial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">print<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">count<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <b><span style=\"color: #d35400;\"> \r\nSerial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #005c5f;\">\" seconds\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <b><span style=\"color: #d35400;\"> \r\nSerial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #005c5f;\">\"If you want to switch it off, simply enter N or n!\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> \r\n<span style=\"color: #000000;\">count<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #000000;\"> \r\n}<\/span> <span style=\"color: #5e6d03;\"> \r\nif<\/span> <span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">ch<\/span> <span style=\"color: #434f54;\">==<\/span> <span style=\"color: #00979c;\">'n'<\/span><span style=\"color: #434f54;\">||<\/span><span style=\"color: #000000;\">ch<\/span> <span style=\"color: #434f54;\">==<\/span> <span style=\"color: #00979c;\">'N'<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\"> { <\/span><span style=\"color: #d35400;\"> \r\ndigitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">redLedPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">LOW<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <b><span style=\"color: #d35400;\"> \r\nSerial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #005c5f;\">\"You have turned off the LED!!\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <b><span style=\"color: #d35400;\"> \r\nSerial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">print<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #005c5f;\">\"The LED was on for \"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <b><span style=\"color: #d35400;\"> \r\nSerial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">print<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">count<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <b><span style=\"color: #d35400;\"> \r\nSerial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #005c5f;\">\" seconds\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <b><span style=\"color: #d35400;\"> \r\nSerial<\/span><\/b><span style=\"color: #434f54;\">.<\/span><span style=\"color: #d35400;\">println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #005c5f;\">\"If you want to switch it on, simply enter Y or y!\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #000000;\"> \r\ncount<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #000000;\"> \r\n}<\/span> <span style=\"color: #000000;\"> \r\n}<\/span> <span style=\"color: #d35400;\"> \r\ndelay<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">1000<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #000000;\"> count<\/span> <span style=\"color: #434f54;\">+=<\/span> <span style=\"color: #000000;\">1<\/span><span style=\"color: #000000;\">;<\/span> \r\n<span style=\"color: #000000;\">}<\/span><\/pre>\n<h2>Compile and upload<\/h2>\n<p>Open the Arduino IDE and select corresponding board type and port type for your Arduino board.<\/p>\n<p><a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/boardport-2.jpg\"><img loading=\"lazy\" class=\"size-full wp-image-8183 aligncenter\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/boardport-2.jpg\" alt=\"\" width=\"487\" height=\"593\" \/><\/a><\/p>\n<p>After compile this sketch, simply click the \u201cUpload\u201d button in the environment. Wait a few seconds \u2013 you should see the RX and TX leds on the board flashing. If the upload is successful, the message \u201cDone uploading.\u201d will appear in the status bar.<\/p>\n<p>A few seconds after the upload finishes, click on the right-most button on the toolbar in the Arduino IDE. The button is circled below.<\/p>\n<p><a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/serialmonitor.jpg\"><img loading=\"lazy\" class=\"size-full wp-image-8160 aligncenter\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/serialmonitor.jpg\" alt=\"\" width=\"486\" height=\"37\" \/><\/a><\/p>\n<p>The following window will open.<\/p>\n<p><a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/serialwindow-1.jpg\"><img loading=\"lazy\" class=\"size-full wp-image-8186 aligncenter\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/serialwindow-1.jpg\" alt=\"\" width=\"599\" height=\"366\" \/><\/a><\/p>\n<p>This window is called the Serial Monitor and it is part of the Arduino IDE software. Its job is to allow you to both send messages from your computer to an Arduino board (over USB) and also to receive messages from the Arduino.<\/p>\n<h1 id=\"5\">Arduino Code Analysis<\/h1>\n<p>The code we will be using is again very simple with its purpose is to show the basics of how you can use the Arduino Serial monitor to view and send data. I will explain each of the lines we\u2019re using and how you can use it in your next project to debug or interact with the Arduino. If you just want to download the code straight away then you can download it form <a href=\"https:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/code\/Lesson9_The_Serial_Monitor.rar\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<p>To begin we initialise an integer variable for our LED, this is the pin number leading to the LED. We also initialize a variable called count, this will store the amount of time the LED is on or off.<\/p>\n<pre>int redLedPin = 3;\r\nint count = 0;<\/pre>\n<p>In the setup function we first set the red LED to act as an output so we can turn it off and on.<\/p>\n<p>We then call theserial.beginfunction with a parameter value of 9600. The value we\u2019re setting is known as the <a href=\"http:\/\/electronicdesign.com\/communications\/what-s-difference-between-bit-rate-and-baud-rate\" target=\"_blank\" rel=\"noopener noreferrer\">bits per second<\/a> (baud) rate. This can be set to quite a few different rates but we will use 9600 as this seems to be the default and will work with most devices without any issues. It is very important to call this otherwise you won\u2019t be able to communicate with the Arduino.<\/p>\n<p>Next we wait for the serial interface to initialise and connect to the computer\/device. We then call a function calledSerial.println().This function allows us to print a line to the Arduino serial monitor. You will notice this will print the text and then start on a new line for the next input\/output.<\/p>\n<pre>void setup() {\r\n  pinMode(redLedPin, OUTPUT);\r\n  Serial.begin(9600);\r\n  while (! Serial);\r\n  Serial.println(\"Enter Y to turn on the LED:\");\r\n}<\/pre>\n<p>Lastly we have the loop, this will continually loop through until either a new program is uploaded or the Arduino is switched off.<\/p>\n<p>Inside the loop function we have an if statement checking to see if there is data waiting in the serial buffer(Serial.Available). In simple terms we&#8217;re checking to see if we have sent a command to it. If there is data, we enter the if statement.<\/p>\n<p>Next we create a variable calledchand callSerial.read()which will get us the data currently waiting in the serial buffer. Keep in mind this function will only get us the first byte of the data that is incoming to the Arduino. If you want to get an entire string you will need to use something likeSerial.readString.<\/p>\n<p>If the byte of data is either y or n we then enter the relevant if statement. In here you will notice we change the output of the LED pin high or low. We then print out a series of statements. There are two things you should notice here.Serial.printlnwill print the data and go to a new line whilstSerial.printwill print the data but stay on the same line when outputting. You will also notice you can print variables such as thecountexample in the code below. Doing this will allow you to be able to debug values when it comes to data processing.<\/p>\n<p>Lastly I delay by a second and add 1 to count. This is just showing you an example of adding data to a variable then printing it via the serial output.<\/p>\n<pre>void loop(){\r\n  if (Serial.available()){\r\n    char ch = Serial.read();\r\n    if (ch == 'y' ||\tch == 'Y'){\r\n      digitalWrite(redLedPin, HIGH);\r\n      Serial.println(\"You have turned on the LED!!\");\r\n      Serial.print(\"The LED was off for \");\r\n      Serial.print(count);\r\n      Serial.println(\" seconds\");\r\n      Serial.println(\"If you want to switch it off, simply enter N or n!\");\r\n      count = 0;\r\n    }\r\n    if (ch == 'n' ||\tch == 'N'){\r\n      digitalWrite(redLedPin, LOW);\r\n      Serial.println(\"You have turned off the LED!!\");\r\n      Serial.print(\"The LED was on for \");\r\n      Serial.print(count);\r\n      Serial.println(\" seconds\");\r\n      Serial.println(\"If you want to switch it on, simply enter Y or y!\");\r\n      count = 0;\r\n    }\r\n  }\r\n    delay(1000);\r\n    count += 1;\r\n}<\/pre>\n<p>Once you\u2019re done you should be able to deploy the code onto the Arduino. In the next part I will show you how to connect, receive and send data to the Arduino.<\/p>\n<h1 id=\"6\"><i class=\"fa fa-exchange\"><\/i> Connecting, Receiving and Sending Data to the Arduino<\/h1>\n<p>Now that the code has been uploaded to the Arduino we will need to open up the Arduino serial monitor. This is pretty easy and there are only a few options that I will need to explain.<\/p>\n<p>To open up the serial monitor go up to tools and then select the serial monitor. Alternatively, CTRL+SHIFT+M will also bring up the same window. Make sure you are hooked up to the Arduino otherwise the window won\u2019t open up.<\/p>\n<p><a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/1.jpg\"><img loading=\"lazy\" class=\"size-full wp-image-8177 aligncenter\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/1.jpg\" alt=\"\" width=\"486\" height=\"577\" \/><\/a><br \/>\nYou should now have a window open that looks similar to the one below.<br \/>\nAt the top of this screen there should be an input area. This is where you can type and send text to the Arduino. To send the text that you have typed simply press the send button.<\/p>\n<p>Below this is the output window, all the data that is sent to us will be displayed here. If you&#8217;re using the example code above you will be able to see the text that is in the Serial.println functions.<\/p>\n<p>At the bottom we have 3 different options. The first is pretty self-explanatory, auto scroll will disable and enable the automatically scrolling of the output box.<\/p>\n<p>Second we have the line ending option. You can set the monitor to automatically append a line ending after what you enter\/send to the Arduino.<\/p>\n<p>Thirdly we have the baud rate that I mentioned above. Make sure this matches to what you have set in the code. If it is different than the output text will appear as gibberish. If you\u2019re receiving gibberish, then this is a likely cause of this.<br \/>\n<a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/2.jpg\"><img loading=\"lazy\" class=\"size-full wp-image-8178 aligncenter\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/2.jpg\" alt=\"\" width=\"600\" height=\"367\" \/><\/a><\/p>\n<div align=\"center\"><iframe src=\"https:\/\/www.youtube.com\/embed\/puyE9zxPhZc\" width=\"853\" height=\"480\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/div>\n<p>This is basically everything you need to know to understand the basics of the serial monitor. There are a few more things you can learn about such as using strings via the serial interface and more but this probably all you need to know for now.<\/p>\n<p>I hope you now understand how to use the Arduino serial monitor correctly and will help you in future Arduino projects. If I have missed something, got something wrong , you have feedback or anything else then feel to leave a comment below.<\/p>\n<p><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-lesson-7-blink\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Previous.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-tutorial-directory\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Tutorial.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-9-flowing-water-light\/\"><img loading=\"lazy\" class=\"alignright\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Next.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/p>\n<div align=\"center\">\n<div align=\"center\"><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 Buy from US Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES Buy from JP Content Introduction Preparations Hardware Software Connection Upload [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":150,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/71"}],"collection":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/comments?post=71"}],"version-history":[{"count":2,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"predecessor-version":[{"id":76,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/71\/revisions\/76"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media\/150"}],"wp:attachment":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}