Difference between revisions of "YourDuino-Serial-Monitor"

From ArduinoInfo
Jump to: navigation, search
m
m
 
(One intermediate revision by the same user not shown)
Line 32: Line 32:
 
===LOOP:===
 
===LOOP:===
 
Here you can print helpful info to the Serial Monitor. Examples:<br /> <br />  Serial.println("Top of loop");<br />  Serial.println("Reading Temperature Sensor");<br />  Serial.print("LoopCounter value = ");<br />  Serial.println(LoopCounter);<br />  For details of how to show different data, see " Serial_Print.html" in the Reference section of your Arduino IDE: Top menu: Help>reference/Serial_Print.html<br /> <br />  NOTE: Every time you start the Serial Monitor it resets Arduino and your sketch starts over at the beginning. <br /> <br />  
 
Here you can print helpful info to the Serial Monitor. Examples:<br /> <br />  Serial.println("Top of loop");<br />  Serial.println("Reading Temperature Sensor");<br />  Serial.print("LoopCounter value = ");<br />  Serial.println(LoopCounter);<br />  For details of how to show different data, see " Serial_Print.html" in the Reference section of your Arduino IDE: Top menu: Help>reference/Serial_Print.html<br /> <br />  NOTE: Every time you start the Serial Monitor it resets Arduino and your sketch starts over at the beginning. <br /> <br />  
Example Serial Communications Sketch: (Cut-Paste)
+
Example Serial Communications Sketch: (Cut-Paste)  (Thanks Peter Hellen, for fixing this!)
 
   
 
   
<span style="color: #7E7E7E">/* YourDuinoStarter_SerialMonitor_SEND_RCVE</span>
+
<shtml hash="9e38c029bf9b69ab377c07091fccdb5971d56c8f615e31e2359da4710f617dfb"><pre>
<span style="color: #7E7E7E"> - WHAT IT DOES: </span>
+
<font color="#95a5a6">&#47;* YourDuinoStarter_SerialMonitor_SEND_RCVE</font>
<span style="color: #7E7E7E">   - Receives characters from Serial Monitor</span>
+
<font color="#95a5a6"> &nbsp;- WHAT IT DOES:</font>
<span style="color: #7E7E7E">   - Displays received character as Decimal, Hexadecimal and Character</span>
+
<font color="#95a5a6"> &nbsp;&nbsp;- Receives characters from Serial Monitor</font>
<span style="color: #7E7E7E">   - Controls pin 13 LED from Keyboard</span>
+
<font color="#95a5a6"> &nbsp;&nbsp;- Displays received character as Decimal, Hexadecimal and Character</font>
<span style="color: #7E7E7E"> - SEE the comments after "//" on each line below</span>
+
<font color="#95a5a6"> &nbsp;&nbsp;- Controls pin 13 LED from Keyboard</font>
<span style="color: #7E7E7E"> - CONNECTIONS:</span>
+
<font color="#95a5a6"> &nbsp;- SEE the comments after &#34;&#47;&#47;&#34; on each line below</font>
<span style="color: #7E7E7E">   - None: Pin 13 built-in LED</span>
+
<font color="#95a5a6"> &nbsp;- CONNECTIONS:</font>
<span style="color: #7E7E7E">   - </span>
+
<font color="#95a5a6"> &nbsp;&nbsp;- None: Pin 13 built-in LED</font>
<span style="color: #7E7E7E"> - V1.00 02/11/13</span>
+
<font color="#95a5a6"> &nbsp;&nbsp;-</font>
<span style="color: #7E7E7E">   Questions: terry@yourduino.com */</span>
+
<font color="#95a5a6"> &nbsp;- V1.01 11&#47;29&#47;2019 FIXED - THANKS! Peter Hellen</font>
+
<font color="#95a5a6"> &nbsp;&nbsp;Questions: terry@yourduino.com *&#47;</font>
<span style="color: #7E7E7E">/*-----( Import needed libraries )-----*/</span>
+
 
<span style="color: #7E7E7E">/*-----( Declare Constants and Pin Numbers )-----*/</span>
+
<font color="#95a5a6">&#47;*-----( Import needed libraries )-----*&#47;</font>
#define led 13  <span style="color: #7E7E7E">// built-in LED</span>
+
<font color="#95a5a6">&#47;*-----( Declare Constants and Pin Numbers )-----*&#47;</font>
<span style="color: #7E7E7E">/*-----( Declare objects )-----*/</span>
+
<font color="#5e6d03">#define</font> <font color="#000000">led</font> <font color="#000000">13</font> &nbsp;<font color="#434f54">&#47;&#47; built-in LED</font>
<span style="color: #7E7E7E">/*-----( Declare Variables )-----*/</span>
+
<font color="#95a5a6">&#47;*-----( Declare objects )-----*&#47;</font>
<span style="color: #CC6600">int</span> ByteReceived;
+
<font color="#95a5a6">&#47;*-----( Declare Variables )-----*&#47;</font>
+
<font color="#00979c">int</font> <font color="#000000">ByteReceived</font><font color="#000000">;</font>
<span style="color: #CC6600">void</span> <span style="color: #CC6600">'''setup'''</span>()  <span style="color: #7E7E7E">/****** SETUP: RUNS ONCE ******/</span>
+
 
{
+
<font color="#00979c">void</font> <font color="#5e6d03">setup</font><font color="#000000">(</font><font color="#000000">)</font> &nbsp;&nbsp;<font color="#95a5a6">&#47;****** SETUP: RUNS ONCE ******&#47;</font>
  <span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">begin</span>(9600);
+
<font color="#000000">{</font>
  <span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">println</span>(<span style="color: #006699">"--- Start Serial Monitor SEND_RCVE ---"</span>);
+
&nbsp;<font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">led</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font>
    <span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">println</span>(<span style="color: #006699">" Type in Box above, <ENTER>. "</span>);
+
  &nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">begin</font><font color="#000000">(</font><font color="#000000">9600</font><font color="#000000">)</font><font color="#000000">;</font>
  <span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">println</span>(<span style="color: #006699">"(Decimal)(Hex)(Character)"</span>);
+
&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">&#34;--- Start Serial Monitor SEND_RCVE ---&#34;</font><font color="#000000">)</font><font color="#000000">;</font>
  <span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">println</span>();
+
&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">&#34; Type in Box above, &lt;ENTER&gt;. &#34;</font><font color="#000000">)</font><font color="#000000">;</font>
   
+
&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#005c5f">&#34;(Decimal)(Hex)(Character)&#34;</font><font color="#000000">)</font><font color="#000000">;</font>
}<span style="color: #7E7E7E">//--(end setup )---</span>
+
&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>
+
 
+
<font color="#000000">}</font><font color="#434f54">&#47;&#47;--(end setup )---</font>
<span style="color: #CC6600">void</span> <span style="color: #CC6600">'''loop'''</span>()  <span style="color: #7E7E7E">/****** LOOP: RUNS CONSTANTLY ******/</span>
+
 
{
+
 
  <span style="color: #CC6600">if</span> (<span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">available</span>() > 0)
+
<font color="#00979c">void</font> <font color="#5e6d03">loop</font><font color="#000000">(</font><font color="#000000">)</font> &nbsp;&nbsp;<font color="#95a5a6">&#47;****** LOOP: RUNS CONSTANTLY ******&#47;</font>
  {
+
<font color="#000000">{</font>
    ByteReceived = <span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">read</span>();
+
  &nbsp;<font color="#5e6d03">if</font> <font color="#000000">(</font><b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">available</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#434f54">&gt;</font> <font color="#000000">0</font><font color="#000000">)</font>
    <span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">print</span>(ByteReceived);
+
&nbsp;<font color="#000000">{</font>
    <span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">print</span>(<span style="color: #006699">"        "</span>);
+
&nbsp;&nbsp;&nbsp;<font color="#000000">ByteReceived</font> <font color="#434f54">=</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">read</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>
    <span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">print</span>(ByteReceived, <span style="color: #006699">HEX</span>);
+
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#000000">ByteReceived</font><font color="#000000">)</font><font color="#000000">;</font>
    <span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">print</span>(<span style="color: #006699">"      "</span>);
+
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">&#34; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;</font><font color="#000000">)</font><font color="#000000">;</font>
    <span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">print</span>(<span style="color: #CC6600">char</span>(ByteReceived));
+
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#000000">ByteReceived</font><font color="#434f54">,</font> <font color="#00979c">HEX</font><font color="#000000">)</font><font color="#000000">;</font>
+
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">&#34; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;</font><font color="#000000">)</font><font color="#000000">;</font>
    <span style="color: #CC6600">if</span>(ByteReceived == <span style="color: #006699">'1'</span>) <span style="color: #7E7E7E">// Single Quote! This is a character.</span>
+
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#00979c">char</font><font color="#000000">(</font><font color="#000000">ByteReceived</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
    {
+
 
      <span style="color: #CC6600">digitalWrite</span>(led,<span style="color: #006699">HIGH</span>);
+
&nbsp;&nbsp;&nbsp;<font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">ByteReceived</font> <font color="#434f54">==</font> <font color="#00979c">&#39;1&#39;</font><font color="#000000">)</font> <font color="#434f54">&#47;&#47; Single Quote! This is a character.</font>
      <span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">print</span>(<span style="color: #006699">" LED ON "</span>);
+
&nbsp;&nbsp;&nbsp;<font color="#000000">{</font>
    }
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">led</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">&#34; LED ON &#34;</font><font color="#000000">)</font><font color="#000000">;</font>
    <span style="color: #CC6600">if</span>(ByteReceived == <span style="color: #006699">'0'</span>)
+
&nbsp;&nbsp;&nbsp;<font color="#000000">}</font>
    {
+
 
      <span style="color: #CC6600">digitalWrite</span>(led,<span style="color: #006699">LOW</span>);
+
&nbsp;&nbsp;&nbsp;<font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">ByteReceived</font> <font color="#434f54">==</font> <font color="#00979c">&#39;0&#39;</font><font color="#000000">)</font>
      <span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">print</span>(<span style="color: #006699">" LED OFF"</span>);
+
&nbsp;&nbsp;&nbsp;<font color="#000000">{</font>
    }
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">led</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">&#34; LED OFF&#34;</font><font color="#000000">)</font><font color="#000000">;</font>
    <span style="color: #CC6600">'''Serial'''</span>.<span style="color: #CC6600">println</span>();    <span style="color: #7E7E7E">// End the line</span>
+
&nbsp;&nbsp;&nbsp;<font color="#000000">}</font>
   
+
 
  }<span style="color: #7E7E7E">// END Serial Available</span>
+
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> &nbsp;&nbsp;&nbsp;<font color="#434f54">&#47;&#47; End the line</font>
+
 
}<span style="color: #7E7E7E">//--(end main loop )---</span>
+
&nbsp;<font color="#000000">}</font><font color="#434f54">&#47;&#47; END Serial Available</font>
+
 
<span style="color: #7E7E7E">/*-----( Declare User-written Functions )-----*/</span>
+
<font color="#000000">}</font><font color="#434f54">&#47;&#47;--(end main loop )---</font>
+
 
+
<font color="#95a5a6">&#47;*-----( Declare User-written Functions )-----*&#47;</font>
<span style="color: #7E7E7E">//*********( THE END )***********</span>
+
 
 +
 
 +
<font color="#434f54">&#47;&#47;*********( THE END )***********</font>
 +
 
 +
 
 +
</pre></shtml>
  
 
==='''OTHER TERMINAL MONITORS YOU MIGHT LIKE:'''===
 
==='''OTHER TERMINAL MONITORS YOU MIGHT LIKE:'''===

Latest revision as of 08:16, 29 November 2019

HOW-TO USE THE ARDUINO SERIAL MONITOR

The Arduino IDE has a feature that can be a great help in debugging sketches or controlling Arduino from your computer's keyboard.

The Serial Monitor is a separate pop-up window that acts as a separate terminal that communicates by receiving and sending Serial Data. Serial Data is sent over a single wire (but usually travels over USB in our case) and consists of a series of 1's and 0's sent over the wire. Data can be sent in both directions (In our case on two wires).

SerialMonitorICON.jpg

Problem: Wrong Baud Rate

If you see something like this, or only a few wrong characters, the baud rate set on the serial monitor does not match the Arduino or other device sending data. 4�⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮Ǜ⸮K⸮⸮4⸮⸮�⸮⸮⸮⸮⸮7⸮⸮˒⸮Ǐ4⸮⸮�⸮[⸮⸮⸮�⸮⸮⸮K⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮�⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮K⸮⸮⸮�4⸮⸮⸮⸮⸮⸮⸮⸮⸮�⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮˒⸮⸮�⸮�⸮�⸮⸮⸮�⸮⸮⸮�⸮⸮⸮⸮⸮7⸮⸮⸮⸮⸮⸮�⸮⸮⸮⸮ғ⸮⸮⸮⸮⸮�⸮˒⸮⸮⸮⸮⸮⸮�⸮ۚۚ⸮⸮�i�)⸮⸮⸮⸮⸮⸮K⸮⸮i⸮⸮�⸮⸮⸮⸮⸮�⸮⸮⸮⸮⸮⸮Nj⸮⸮⸮⸮⸮⸮⸮⸮⸮[⸮⸮⸮⸮�⸮⸮⸮ˢ⸮⸮⸮⸮⸮⸮ۚ⸮⸮⸮⸮⸮⸮�⸮�⸮⸮⸮⸮iˢ⸮⸮�⸮⸮⸮⸮⸮ۚ�⸮⸮⸮�

When you have an Arduino connected by USB, and the IDE opened, you will see an icon on the far right like this (hover over it):
YD_SerialMonitor_Send-Rcve.jpg
When you click on it, the Serial Monitor will pop up in a new window. Here's what our example Serial Monitor Sketch looks like with the Monitor opened:

(NOTE: The sketch, which you can cut and paste into the IDE, is below.)

Look at the Serial Monitor window.

  • The small upper box is where you can type in characters (hit <enter> or click "Send")
  • The larger area (Corner can be dragged to enlarge) is where characters sent From Arduino will be displayed.
  • At the bottom are two pulldowns:
  • One sets the "line ending" that will be sent to Arduino when you <enter> or click Send
  • The other sets the Baud Rate for communications. (If this does not match the value set up in your sketch in Setup, characters will be unreadable).

Example: Serial.begin(9600); Some sketches or other applications may use a different Baud Rate.

DEBUGGING WITH SERIAL MONITOR:

If you are testing a new sketch you may need to know what's happening when you try to run it. But "Software Is Invisible". So you need the tell the software to tell you what it's doing so it's visible, and sometimes the value of changing variables. You do this my using the Serial Monitor and adding code to your sketch to send characters that you can see.

SETUP:

In Setup you need to begin Serial Communications and set the Baud Rate (speed) that data will be transferred at. That looks like this:
Serial.begin(9600); // Other baud rates can be used...
Serial.println("My Sketch has started");
The second line is optional...

LOOP:

Here you can print helpful info to the Serial Monitor. Examples:

Serial.println("Top of loop");
Serial.println("Reading Temperature Sensor");
Serial.print("LoopCounter value = ");
Serial.println(LoopCounter);
For details of how to show different data, see " Serial_Print.html" in the Reference section of your Arduino IDE: Top menu: Help>reference/Serial_Print.html

NOTE: Every time you start the Serial Monitor it resets Arduino and your sketch starts over at the beginning.

Example Serial Communications Sketch: (Cut-Paste) (Thanks Peter Hellen, for fixing this!)

/* YourDuinoStarter_SerialMonitor_SEND_RCVE
  - WHAT IT DOES:
   - Receives characters from Serial Monitor
   - Displays received character as Decimal, Hexadecimal and Character
   - Controls pin 13 LED from Keyboard
  - SEE the comments after "//" on each line below
  - CONNECTIONS:
   - None: Pin 13 built-in LED
   -
  - V1.01 11/29/2019 FIXED - THANKS! Peter Hellen
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
/*-----( Declare Constants and Pin Numbers )-----*/
#define led 13  // built-in LED
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
int ByteReceived;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  Serial.println("--- Start Serial Monitor SEND_RCVE ---");
  Serial.println(" Type in Box above, <ENTER>. ");
  Serial.println("(Decimal)(Hex)(Character)");
  Serial.println();

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  if (Serial.available() > 0)
  {
    ByteReceived = Serial.read();
    Serial.print(ByteReceived);
    Serial.print("        ");
    Serial.print(ByteReceived, HEX);
    Serial.print("       ");
    Serial.print(char(ByteReceived));

    if (ByteReceived == '1') // Single Quote! This is a character.
    {
      digitalWrite(led, HIGH);
      Serial.print(" LED ON ");
    }

    if (ByteReceived == '0')
    {
      digitalWrite(led, LOW);
      Serial.print(" LED OFF");
    }

    Serial.println();    // End the line

  }// END Serial Available

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/


//*********( THE END )***********


OTHER TERMINAL MONITORS YOU MIGHT LIKE:

Serial data is Serial data So other things (like the PROCESSING language and IDE) and other terminal programs can interact with your Arduino using it. Here are some possibilities:

Putty [Windows]

Download HERE: PuTTY.

License: MIT.

Hands-down one of our favorite terminal applications for Windows is PuTTY from Simon Tatham

In addition to being a great serial terminal it can also handle telnet, ssh, and a host of other things (no pun intended). If you’re using Windows, this is probably your best go-to terminal application.

Screen [Mac, Linux]

Download: NONE (Already exists in MAC ) [1]

Screen is a command line based fullscreen VT100 emulator. It makes a create terminal client and has the ability to “detach” from the current terminal to run in the background. When it comes to serial communication, it’s the tool we use the most outside of the Serial Monitor. Drop to a terminal window and run screen:

PROCESSING:

ProcessingIDE.jpg

PROCESSING is a PC and MAC language that works well in interacting with Arduino over Serial communications. You would need to install the free "Processing" IDE on your computer. See: https://processing.org/download/

You will see that it looks almost like Arduino! That's because Arduino came from Processing.