HOME - - - - - - - - Table of contents, my Arduino "How To" articles
Other material for programmers    Recommend to StumbleUpon

Pt. 2 Arduino Serial Monitor

Sending data to Arduino, using serial monitor


This is one of a number of articles which attempt to help people have fun with Arduinos.

It addresses a use of the sometimes under-used "serial monitor", which is a software tool built into the Arduino IDE.

It follows on from an earlier article which introduced the Arduino serial monitor. The earlier article showed you how to send text to the serial monitor. (By the way... there's also a tool which draws graphs from numbers from the Arduino. That's accessed via Tools/Serial Plotter.)

In this article, I will show you how to send data in the other direction... You'll type things on keyboard of the laptop in the middle of the illustration, and the Arduino will "see" what you typed as soon as you press "enter".

Diagram to show where Serial Monitor fits in bigger picture

Look at the right-hand third of the illustration. It shows the serial monitor window, enlarged. The text you see is a sample of output from the Arduino. That text was specified in the code put into the Arduino under development. But we're not doing that today.

Besides letting you see messages from the Arduino, the serial monitor also allows you to send things to the Arduino.

Sadly perhaps, but necessarily I suspect, pressing a key (or two, or more) doesn't send it to the Arduino immediately. Your input is only sent when you then press the "enter" key. The upside of this is that if you want to send, say, "Hello Arduino", you can type that... getting it right along the way (hurrah for the backspace key!), and then send the "finished message" at the moment of your choosing.

We're going to create a tiny program, just to illustrate the basics. For that you only need an Arduino with an LED on pin 13. Thus, most people won't need to connect anything, as most Arduinos have a built-in LED on pin 13.

The "bad news"? (Obvious(?) when you think about it.) This approach to sending characters to the Arduino only works when it is hooked up to "the big computer" used for programming the Arduino. (If you want to connect a keyboard to an Arduino, for use when it is not connected to the big computer, have a look at the USB Host Controller Board V2.4, which you can buy (£15, +p&p, 2/17) pre-loaded with a USB Keyboard to serial stream of ASCII from Hobbytronics.co.uk.)

The code

Don't be alarmed at how "long" the following is... a lot of it is just comments!

(When viewing this on some devices, you may have to scroll down the page a little to see the code....)



/*Ar796-v1-0 * Version: 06 Feb 18 * Started: 06 Feb 18 * * Demo getting input from big PC's keyboard, * using Serial Monitor * *Derived from the very clear tutorial at... *https://startingelectronics.org/software/ * arduino/learn-to-program-course/19-serial-input/ * *The LED was put on pin 13 to take advantage of the * "built in" LED present on many Arduinos. No * other external hardware is needed. * *The program only "works" when the Arduino is hooked * up to the Arduino, running in a "big" computer. * *You need to open the Serial Monitor window, and click * in the edit box near the top of that. Press "a" * and "enter", and the LED should flash. * *Serial Monitor: *http://sheepdogguides.com/arduino/aht1serimoni.htm */ const byte pLED0=13; char chFrmSerial; void setup() { pinMode(pLED0,OUTPUT); chFrmSerial=0; Serial.begin(9600); }//end of setup() void loop() {2 if (Serial.available() > 0) { // Is a character available? //Note: It won't be "available" as soon as you type it... // You have to press "enter". THEN the whole line is // sent, be it one letter or more. (They would be sent // one letter at a time. chFrmSerial = Serial.read(); // There was! Get the character //Now use it... if (chFrmSerial="a"){ digitalWrite(pLED0,HIGH); delay(30); digitalWrite(pLED0,LOW); //Think: Why no "delay(30)" here? }//end of if chSerial=='a' } // end: if (Serial.available() > 0) }// end of loop()

Wait until the usual "Binary sketch size..." message comes up in the pane at the bottom of the Arduino integrated development environment ("IDE"). Then click on the "Serial Monitor" button, the "magnifying glass" I marked it "6" in the diagram below. (It shows the top of the Arduino IDE window.)

Image of Arduino interface

(And, just in case, below: The old interface. The "Serial Monitor" button was the right hand icon, the one like an upside down window shade.)

Image of Arduino interface

That should bring up a new window, something like...

Image of Arduino Serial Monitor window

Click in the narrow band ("edit box") at the top of the Serial Monitor window. The box just to the left of the "Send" button. On your big computer's keyboard, type an "a". Watch the LED on the Arduino, and, still watching that, press the "enter" key. The LED should flash... and I hope you can see what made that happen?

That's it! Unleash your creativity!

If it doesn't work

A few things that might get in the way...

a) Remember: The text in the edit box is not sent until you press the big computer's "enter" key. (Clicking the "Send" button is as good. Try that!)

b) In the program as presented, you can see Serial.begin(9600).

Look again at the most recent graphic. See "9600" in the lower left? That can be set to a different number, if you have reason to do so. But the number there and in the Serial.begin line must match.

c) Near the "9600" we were just talking about, you can see "No line ending". What's there shouldn't matter to our simple use of the serial monitor to send data to the Arduino, but I hope you will go beyond our simple use! When you do, you may or may not want to leave the setting at "No line ending". I would tend to leave it thus.

What's next?

The program presented is, of course, very, very simple, crude.

What's next is up to you! Now that you know how to send characters to the Arduino from "the big computer", I hope you will find the skill let's you create programs that do things which amuse you... or at least are useful.

   Search this site or the web        powered by FreeFind
  Site search Web search
Site Map    What's New    Search

The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.
In addition to the tutorials for which this page serves as Table of Contents, I have other sites with material you might find useful.....

Sequenced set of tutorials on Arduino programming and electronics interfacing.
Tutorials about the free database supplied with Open Office version 2. (If you experienced Adabas with Star Office, ooBase is nothing like it!)
Some pages for programmers.
Using the parallel port of a Windows computer.

If you visit 1&1's site from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service.

Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's Sheepdog Software (tm) freeware, shareware pages.

And if you liked that, or want different things, here are some more pages from the editor of these tutorials....

Click here to visit the homepage of my biggest site.

Click here to visit the homepage of Sheepdogsoftware.co.uk.

Click here to visit editor's pages about using computers in Sensing and Control, e.g. weather logging.

To email this page's editor, Tom Boyd.... Editor's email address. Suggestions welcomed!

Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org

Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Why do I mention the script? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....