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

Watching an incoming serial stream of ASCII characters with an Arduino

Many devices generate streams of data. Use this to do first tests with them.

Filename: aht4SerAsciiWatch.htm

This is a simple little program. It doesn't "do much". By design.

If it doesn't do much, then if it plus whatever you add don't do what is expected, it is probably in the bit... hardware or software... that you added, or in SMALL tweaks needed to MAKE it work.

The job, plan

I hope you have some new toy that you want to get to grips with. A toy that generates a stream of ASCII characters, and sends them off into the wide world on a serial data line.

If you do, and you don't have immediate success with fancier things, then why not hook your new device ("toy") up to an Arduino running the program I provide in this essay? If the device is generating the data, you should be able to see it with the Arduino IDE's Serial Monitor.

(Digression) You do understand, I hope that some devices run on 3v3, others on 5v? As long as your device and your Arduino both run on one or the other, you are all set. If they don't, and you are a novice-novice, save yourself worry, and the damage that comes from not getting the voltage thing right... buy yourself a little Arduino ($15?) with the voltage used by your device! (Or study the question of voltage level shifting.)

As presented, the program assumes that your device does not need any commands sent to it. The program assumes that the device will send the data, at 9600 baud, any time it is supplied with the volts to do it. You will have to add whatever is needed, if your device does need commands.

Hook-up

Not a lot to say. Be sure to connect a wire between the "ground" of your device and your Arduino... Probably best to do that when NEITHER of them are receiving any power (voltage).

Give both your Arduino and the device power. If your device doesn't need very much power, and if your Arduino can supply that much from its Vcc line, well and good. ("need.... power": Because we know the voltage prevailing, we can offend our physics teachers and specify the "power" by talking about how much current will flow from the source of the voltage to the device. That's the usual shortcut. Note that how much is "very much" isn't a matter for your "common sense". Look it up. The Arduino's datasheets or specs will tell you how much current can be supplied to off-board circuits without over-taxing the voltage regulator on the Arduino. You device's data sheets will tell you the maximum power it might draw. ("Draw" is a bad term for this, even if it is the common one. It is a bit like saying a vacuum can "suck" things. The device doesn't "pull" the current in. The device operates as if it were just a variable resistor. When you are told that the maximum "draw" is x mA, you are being told, indirectly, that at no point will the device look like a resistor of less than y ohms.)

Arduino's default to making their pins inputs. But what was the setting on the pin you are planning to use, the last time that Arduino was doing something? Never trust defaults. Before you connect the device's output to the pin you want to use to receive the data, run my program once in your Arduino... the line that says...

SoftwareSerial GPS_Serial(9,10); // RX, TX

.... makes pin 9 an input, and begins watching it for incoming serial data.

AFTER you've done that, connect the output from your device to that pin of the Arduino, turn everything on (hardware and software.) Open up the serial monitor window, if it wasn't already open... it can usually be left open between running the program again, after the latest changes (but if in doubt, shut it, re-open it)...

And you should see the incoming data streaming nicely in the Arduino IDE's serial monitor!

(At least with a Qduino, I sometimes get clashes between the code going out to the Arduino during upload and opening the serial monitor. If you get "Port (number) not available", don't be downhearted... just try whichever bit uploading / opening the serial monitor) again in a moment.)

Some devices take a bit of time to "wake up". Don't panic (even if you are not Mr Mannering). Give it a little time. The GPS receiver from Sparkfun start sending a stream of ASCII as soon as it has power. It takes up to 30 seconds for it to start sending data about the time, date, and the device's latitude and longitude. (And how fast it is moving, and in what direction. There's also a datum for the local magnetic deviation!) (See also my essay about the Sparkfun part number GPS-1370 if that sounds fun. Only costs about $20 (Aug 20)).

The code

For a simple test to see if data is coming from somewhere, as a 9600 baud stream of ASCII data, the following works...

#include <SoftwareSerial.h>

//Heart of http://sheepdogguides.com/
//                  arduino/aht4SerAsciiWatch.htm

//This watches a stream of arriving ASCII serial data...
//  a simple "tool"... nothing fancy.
//It passes any incoming ASCII to the Arduino IDE's
//  serial monitor

//Written for Qduino... not that it should much matter.
//  ... nothing exotic here!

//At 19 Aug 2020, 20:02, this "working"... at least the
//    serial stream from a GPS receiver was successfully
//    displayed in the Arduino IDE's serial monitor.
//(New to "serial monitor"? See...
//    http://sheepdogguides.com/arduino/aht1serimoni.htm

//This written using Arduino IDE 1.8.13/ Windows 10, 8/2020

//You can just leave the serial monitor open... all should
//    be well, even on multiple compiles.

SoftwareSerial GPS_Serial(9,10); // RX, TX
//These pins DO WORK FOR SoftwareSerial, on a Qduino,
//    The first number tells the program where you have
//    (or will!) connect the incoming 9600 baud
//    source of a stream of ASCII codes.
//    The second number tells the program to set that
//    pin up as an output, to send serial data elsewhere.
//    This particular program doesn't USE the second pin,
//    but there's no variant of SoftwareSerial for just
//    data IN (to the Arduino).

void setup() {
  Serial.begin(9600);
  GPS_Serial.begin(9600);
  delay(2000);//give ports chance to initialize

  Serial.println("---");
  Serial.println(" ");
  Serial.println("Hello from ar791");
  Serial.println("Vers 22 Aug 2020, 00:02");
  Serial.println(" ");
  delay(2000);// Give reader a chance to see the output.
}

void loop() {
   byte bFrmSerialStream;

   while (GPS_Serial.available())
    {
      bFrmSerialStream=GPS_Serial.read();
      Serial.write(bFrmSerialStream);
    }
}// end of loop()

Conclusion

I hope that's helped you at least determine that your new toy IS sending SOMETHING? All the best with getting to grips with it.

I often say... and people almost never do... GET IN TOUCH! PLEASE! Tell me if the page was any use to you? Were there sticking points... or even "no, no sticking points". Why should I write this sort of thing, if no one is reading them? (Contact details near bottom of page)

{!- -


Search across all my sites with the Google search button at the top of the page this link will take you to.

   ... Or... To search THIS site.... (Go to my other sites, below, and use their search buttons if you want to search them.)


index sitemap
What's New at the Site Advanced search
Search tool (free) provided by FreeFind... whom I've used since 2002. Happy with it, obviously!

Unlike the clever Google search engine, this one merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

Please also note that I have two other sites, and that this search will not include them. They have their own search buttons.

My SheepdogSoftware.co.uk site, where you'll find my main homepage. It has links for other areas, such as education, programming, investing.

My site at Arunet.

... and then there's my growing new (2020) site at wywtk.com... "What You Want To Know".



And, within them...

Sequenced set of tutorials on Arduino programming and electronics interfacing.
Tutorials about the free database supplied with Libre Office / Open Office.
Some pages for programmers.
Click here to visit editor's pages about using computers in Sensing and Control, e.g. weather logging.


My website is hosted by Ionos.com. I would commend them to you, if you want web hosting, or email.




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 freeware, shareware pages.



How to email or write this page's editor, Tom Boyd. Please cite "aht4SerAsciiWatch.htm".




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

AND passes... Valid CSS!


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 .....