Serial3 or SoftwareSerial use on Mega

I am having trouble running an alternative serial line from a Mega to an Uno using either Serial3 or SoftwareSerial without the Mega's standard serial plugged into a computer's USB port. Seems odd.

The intent is to run the Arduino Mega as a remote data collection device, powered by a standard "9V 1A Arduino power supply adapter 110V AC" - without a USB connection to a computer, and passing its collected system probe data to an Arduino Uno via an alternative Serial connection. The Uno is to be an Uno WiFi that simply collects data from remote data collection devices like the mega.

Code is not much:

/*
 * Arduino Uno - Software Serial Example
 * Updated: 170610
 */

#include <SoftwareSerial.h>
// Wiring: pins 10 to 10 and 11 to 11
const byte rxPin = 10;
const byte txPin = 11;
SoftwareSerial swSerial(rxPin, txPin);

void setup()
{
  Serial.begin(9600);
  swSerial.begin(9600);

  delay( 1000 );
  Serial.println( "Uno started." );
}

void loop()
{
  swSerial.println( "From Uno" );

  delay( 1000 );

  while( swSerial.available() ) {
    Serial.print( (char) swSerial.read() );
  }

  delay( 1000 );
}
/*
 * Arduino Mega
 * Updated: 170610
 */

#include <SoftwareSerial.h>
// Wiring: pins 10 to 10 and 11 to 11
const byte rxPin = 11;
const byte txPin = 10;
SoftwareSerial swSerial(rxPin, txPin);

void setup()
{
  Serial.begin(9600);
  swSerial.begin(9600);

  delay( 1000 );

  Serial.println( "Mega started." );
  swSerial.println( "From Mega" );
}

void loop()
{
  delay( 1000 );

  Serial.println( "Sending to Uno." );
  while( swSerial.available() ) {
    swSerial.write( (char) swSerial.read() );
  }
}

This example works great until I unplug the Mega's USB cable - then communications fail.

Any thoughts?

Why are you using SoftwareSerial on the Mega?
It has 4. Count them; 4 hardware serial ports!

Why use the Uno to connect to WiFi. It's just another un needed step to go through. Ditch that and use the WiFi module directly on the Mega (ESP8266 for example). This is how my Hive monitor system is set up

Hive sensors>Nano>Xbee ----2.4Ghz-------Xbee>Mega>ESP8266>Wireless Network Router>EmonCMS Server

With the Xbees in API mode you can have many sources sending to the 1 Mega Reciever

Thanks for responding.

ieee488 You seemed to have missed something in my post. I have tried using the Mega's extra Serial ports like Serial3, as in the topic even.

MarkDerbyshire Thank you. If I knew how to get the wireless up and running on my mega, that would be the best solution. I am no developer; just an old man hacking away trying to get something to work for my Aquaponics system. I will look up Xbee, but my trouble has been the:
">ESP8266>Wireless Network Router>EmonCMS Server"

I appreciate your experience. Your setup sounds good. Any additional help is welcomed. TY

I have attached two files of the Mega2560 Shield I made which houses the Xbee and ESP8266 WiFi module. One is the schematic and the other is the PCB layout

The regulators I use are the Pololu D246Fx modules for 9V, 5V and 3.3V

Pololu V Regulators

The Logic level shifter is a Pololu 4 Channel Bi Directional LLS which you will need as a minimum for the ESP8266 Module. I used the same module to LLS the Xbee Rx and Tx as well

Pololu Logic Level Shifter

The Xbees are Series 1

Hope this helps and gets you on your way

XBEE_Reciever - PCB.pdf (103 KB)

XBEE_Reciever - Project.pdf (34.7 KB)

gregBaker:
ieee488 You seemed to have missed something in my post. I have tried using the Mega's extra Serial ports like Serial3, as in the topic even.

That is not what your code says.

gregBaker:
ieee488 You seemed to have missed something in my post. I have tried using the Mega's extra Serial ports like Serial3, as in the topic even.

That doesn't answer the question.
But if

This example works great until I unplug the Mega's USB cable - then communications fail.

It sounds like you have a power supply problem, and the code is fine - if a little silly. You might check that all the lights are on, and the wiring is kosher.

Nick_Pyner: Thank you for your interest.

I have the same results with a different Arduino power supply and different Mega boards even. Seeme like a Serial3.begin(9600) issue for it simply does not begin. Change no code, plug into my USB and the communications work again.

Argh... I am not experienced at debugging!

And you aren't making much sense either. Whatever the problem is, it can only fall into one of two categories:

  1. Software - fixed by debugging
  2. Hardware - fixed by fiddling and procedure.

You probably have done enough debugging for the moment anyway, because IF your communications works when you plug in the USB cable it can only mean there is nothing fatally wrong with the software, the problem is hardware, and no amount of DBugging on Serial3 will fix it. The only exception to this is where you aren't actually using Serial3, in which case the same applies, and you are really sending data on Serial0 and reading it on your monitor via the USB cable, which is apparently not your intention.

I really can't comment on the power supply. A standard 9v 1A should be fine to prove the point, and one would be more inclined to suspect the USB of not delivering the goods.

Serial3 is used by connecting the other device's Rx line to pin 14(Tx), and its Tx to pin 15(Rx), as clearly marked, and using the various commands Serial3.dadedah. There really isn't anything more to it than that.

While it can be used on a Mega, provided you stay off the (multitude of) hardware serial pins and use those pins appropriate for it, software serial is never a good idea, you have to be pretty desperate if you do so, and you definitely shouldn't tell your mother about it. In the meantime, if it works at all, the real problem is somewhere else.

Nick_Pyner I seriously appreciate your help.

I have tried most combination in a test bed - one Uno, one Mega and just two jumper wires. The code is a small example extracted from my running Arduino system monitoring an Aquaponics system. The wiring seems correct as shown in the code above and similar when testing Serial3, Rx-Tx crossed. I have tried multiple Arduino power supplies of 9V 1A on the Mega and tested with / without the USB cable attached. Only the USB cable is attached to the Uno, e.g., no external power supply.

All works fine as monitored by the Uno console serial connection. But after removing the Mega's USB cable and starting both back up, the Uno to Mega to Uno communications fail.

Try it. I do not understand why. Software, hardware or simply my stupidity - whatever.

My only thought is: for any serial communication on (to/from) the Mega, the standard Serial.begin command must not fail to connect via the USB cable. That is, I believe that not successfully initializing the standard USB serial port, their can be no Mega serial communications including a SoftwareSerial connection.

If you or someone else knows better - e.g. I am a stupid newbie, please let me know how to make this work. But I urge you to try the example yourself first.

gregBaker:
I am a stupid newbie

Newbie is a relative term. We all learn. That is why we are here.

Here is some speculation. I have no experience of connecting Arduinos by wire, but I rather suspect the problem might be to do with grounds. I understand the Uno is powered by USB cable from PC.

IF, when you run the Mega off USB, it is the same PC as that which is powering the Uno, then they share a common ground, but they don't when you use the wall wart. If you run a wire connecting Mega and Uno's ground, you might get a result.

Hummm... Sounds like something that I have not yet tried. Thank you. I will do so and let you know.

Newbie is a relative term.

Yes. Is true. But these days I seem to forget more than I can learn/re-learn. An old man's problem; my problem, not yours. I try all I can think of, research and test. Then try to ask of others what it is that I am missing.

I appreciate your time.

I will try the ground later tonight.

TY

One last note ... for now. Thank you for your patience, Nick_Pyner.

The ground wire IS the solution. Something that I maybe should have known/tried but failed to.

Common sense is not common until you know it too.

Again, thank you for your time.

Greg

gregBaker:
An old man's problem; my problem, not yours.

Really? I thought I was the old guy round here......

Glad to hear you have a result, but now is the time to get off that software serial junk before it becomes habit-forming - and an embarassment..

Nick_Pyner The software serial is due to two factors:

1 ) I use all six "interruptible" pins on the mega, 2, 3, 18, 19, 20, 21 for "Hall Effect Flow" monitors; and
2 ) I use an Uno WiFi to push the data up to ThingSpeak, Iot data collection and display.

I2C, SPI require an interruptible pin on the Mega of which I have none left, and SoftwareSerial seems to run fine on the Uno WiFi (if I can ever get mine back up and running - has a bootloader issue at the moment, I think...)

Anyway, data collection and distribution separation; even on separate devices. I am way old school and I've been out of school for this exponential growth of developers methods and tools - my Masters was way back in '73. I know so little of today's environments and still use antiquated design thoughts - too linear but still doable I think. I was a programmer (not of today's "developer" caliber) a problem solver with critical thinking, mathematical programming, system simulation and optimizations skills. I was a C programmer there at Bell Labs for a while - that was 'C' not C+, C++, C# et al., just Brian Kernighan and Dennis Ritchie's original C Programming Language. (BTW: A good read at about 200 pages.)

Everything Arduino is new to me! ...even pin boards and jumpers. LOL

But I am making my own monitor and control system for my DIY Aquaponics system.
Just something to do really; simply keeps me off the streets, so to speak.

A day without learning something new is a wasted day.

Thank you again. Without people like you, here on this forum, the journey would be much harder for people like myself. New, young or old, is still new, and as Rumsfeld famously has said:

...you don't know what you don't know.

You should consider using AltSoftSerial on pins 46 & 48, and/or NeoSWSerial on pins 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68) or A15 (69). AltSoftSerial is the best, but it only works on those two pins. NeoSWSerial is almost as good, but it only works at 9600, 19200 and 38400, and it disables interrupts while transmitting (not receiving).

Both these libraries are much more efficient than SoftwareSerial. SoftwareSerial disables interrupts for long periods of time, the entire time that a character is received or sent (1ms @ 9600). It can't TX and RX at the same time, and doing either will interfere with other parts of your sketch. It can only receive from one port at a time. Data on all other SoftwareSerial ports will be ignored.

And if the SoftwareSerial baud rate is lower than any of the HardwareSerial ports, you may lose data on them, too. :stuck_out_tongue:

AltSoftSerial and NeoSWSerial can co-exist, giving you two reliable software serial ports that don't affect the hardware serial ports.

1 Like

gregBaker:
The software serial is due to two factors:
1 ) I use all six "interruptible" pins on the mega, 2, 3, 18, 19, 20, 21 for "Hall Effect Flow" monitors; and
2 ) I use an Uno WiFi to push the data up to ThingSpeak, Iot data collection and display.

  1. As things are, the above suggests you still have at least two hardware serial ports still available and I fail to see why there is any discussion about software serial - of any kind...

  2. OK, this all harks back to reply #2, which addresses the real serial comms problem by asking the same question I was going to. I just use a WiFi repeater for IoT myself so am not that familiar with it, but I would assume that running WiFi off a Mega is exactly the same as running it off a Uno and, if you can't do it on a Mega, it is because of some silly mistake that you will only make once - jumper selection or something(!)

  1. As things are, the above suggests you still have at least two hardware serial ports still available and I fail to see why there is any discussion about software serial - of any kind...

Oh geez... I read his post to mean he wanted 6 serial ports.

@gregBaker, QUIT TRYING TO USE SOFTWARE SERIAL PORTS. There are 4 hardware serial ports that will work fine... as long as you can figure out how to connect them.

-dev Thanks for chiming in. I appreciate your time and thoughts.

This thread has morphed: my problem was solved by the addition of a ground jumper between the Uno and Mega.

I was trying SoftwareSerial for the Uno WiFi that I bought last year, I also had the Mega. Both are in my tank room. The Uno WiFi only works with the USP unplugged, which is good for there is no dedicated computer in the tank room for it NOR for the Mega.

I am using an Uno (not the Uno WiFi) for testing. It is using the SoftwareSerial and I tried both SoftwareSerial and Serial3 on the Mega, both with the same unsuccessful results. Now, with the common ground jumper (thank you Nick_Pyner), both SoftwareSerial and Seria3 work fine.

If I keep this configuration, of course I will use the Mega's Serial3.

However, it seems that I should add wifi capabilities to the Mega and skip the Uno WiFi all together. And I'd love to but for my ability [knowledge] to add the wifi to the Mega (not withstanding MarkDerbyshire's post above, TY.)

So, my path is to fix the boot loader on my Uno WiFi for now and then start setting up a Mega with add on wifi capabilities ... stay tuned, 'though it will be a new thread for this one is solved.

gregBaker:
However, it seems that I should add wifi capabilities to the Mega and skip the Uno WiFi all together.

Definitely. And there should be no problem with doing this - certainly not with the code. However, while I found it hard to believe, I now recall somebody else having the same problem recently.

Probably because I wouldn't believe it, nothing was really clear, but it appeared that the poor bugger might have had a WiFi shield that worked on Uno but was incompatible with a Mega. Maybe it's true and you have the same problem. I wouldn't believe it only because a standard Ethernet shield works fine with both.