arduino UNO serial.begin and serial1.begin

Can the Uno have two serial speeds set at the same time? I am trying to run some code that sets up: Serial.begin(9600); Serial1.begin(4800); to communicate with a GPS and the serial monitor. It does not work. If it is possible is there some extra code required to make it work? Thanks.

serial1.begin is for the mega which has four serial i/o ports. Unfortunately, the uno only has one, which would be serial.begin, as you refer to it.

To simplify, pin 0 and 1 is the native serial port on the uno. You can use it with no problems, however you'll need to disconnect anything attached to it when you program your auno (these pins are shared by the serial input to the arduino from the pc). If you need more serial ports, you can use the library newsoftserial to create virtual serial ports with digital io pins (it's now just called softwareserial, not to be confused with the serial library that comes with the IDE), available here: http://arduiniana.org/2011/01/newsoftserial-11-beta/

In your situation, you'll need to use softwareserial (from the link above) to connect to your GPS on different pins (eg, pin 2 and 3), and then use serial to output to the serial monitor, leaving nothing plugged into pin 0 and 1.

I hope that's clear enough - happy to clarify if not.

Uno can use only serial library not serial1...serial1 is only for arduino mega

The following sketch is my attempt to connect to GPS & use the serial monitor at the same time.
The serial monitor shows no activity.
Could you please identify the problem.
Thanks.

// This example illustates access to all sentence types,
// using the NMEA library. It assumes that a GPS receiver
// is connected to serial port ‘Serial1’ at 4800 bps.
#include <NewSoftSerial.h>
#include <nmea.h>
#undef round
NewSoftSerial mySerial(2, 3);
NMEA gps(ALL); // GPS data connection to all sentence types

void setup() {
mySerial.begin(57600);
Serial.begin(4800);
}

void loop() {
if (Serial.available() > 0 ) {
// read incoming character from GPS and feed it to NMEA type object
if (gps.decode(Serial.read())) {
// full sentence received
Serial.print ("Sentence = ");
Serial.println (gps.sentence());
Serial.print ("Datatype = ");
Serial.println (gps.term(0));
Serial.print ("Number of terms = ");
Serial.println (gps.terms());
}
}
}

Your code does not use mySerial at all.

This:

  if (Serial.available() > 0 ) {
    // read incoming character from GPS and feed it to NMEA type object
    if (gps.decode(Serial.read())) {

Should probably be this:

  if (mySerial.available() > 0 ) {
    // read incoming character from GPS and feed it to NMEA type object
    if (gps.decode(mySerial.read())) {

I am still having trouble with getting the following sketch to run on a UNO (its an example from the Arduino NMEA examples included with the software).
I think the GPS data is not getting to the UNO.
Can anyone please show me how to make this sketch run on a UNO?
Thanks.

// This example illustates basic use of the NMEA library.
// It assumes that a GPS receiver is connected to serial
// port ‘Serial1’ at 4800 bps, and that 2 LEDs are connected
// to digital i/o pins 8 and 9.
//
// A GPS data connection of type GPRMC is created, and
// used to show direction to a destination, relative to
// your own direction of movement. The LEDs light up as
// follows. If the destination is
// - more than 5 degrees to your left, pin 8 lights up
// - more than 5 degrees to your right, pin 9 lights up
// - otherwise, both LEDs light up.

#include <nmea.h>

NMEA gps(GPRMC); // GPS data connection to GPRMC sentence type
float d; // relative direction to destination

// destination coordinates in degrees-decimal
float dest_latitude = 48.858342;
float dest_longitude = 2.294522;

void setup() {
Serial1.begin(4800);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}

void loop() {
if (Serial1.available() > 0 ) {
// read incoming character from GPS
char c = Serial1.read();

// check if the character completes a valid GPS sentence
if (gps.decode(c)) {
// check if GPS positioning was active
if (gps.gprmc_status() == ‘A’) {
// calculate relative direction to destination
d = gps.gprmc_course_to(dest_latitude, dest_longitude) - gps.gprmc_course();
if (d < 0) { d += 360; }
if (d > 180) { d -= 360; }
// set LEDs accordingly
if (d < 5) {
digitalWrite(8, HIGH);
} else {
digitalWrite(8, LOW);
}
if (d > -5) {
digitalWrite(9, HIGH);
} else {
digitalWrite(9, LOW);
}
}
}
}
}

The Uno does not have Serial1. Don't use it.

Hi Nick
Should I use a Mega2560?
Will the GPS shield with EM406 just plug & play as it does on the UNO?

I can't say for certain as I don't have one of those shields. A lot of shields work on the Mega. That is the easiest way because you have the extra serial ports. However as wildbill suggested you can use the soft serial to have a software serial port which may work adequately.

I was playing around yesterday with a Midi interface which uses serial at 31250 baud, and as I wanted to see what was happening I had to use another serial port at a more standard baud rate. My Mega worked perfectly there. However it wasn't a shield as such, I just plugged the wires in. You haven't said exactly what shield you have but if you have the Adafruit one they say:

If you have an ATmega168 or 328, download and install the NewSoftSerial library from the download page. Install the library as necessary for your IDE

So they seem to be recommending you use the soft serial library. But depending on exactly what pins the shield uses it should work with a Mega. Perhaps someone else will know for sure.

Thanks Nick I am using the Sparkfun GPS shield. I have asked Sparkfun about this and am waiting for a reply. Thanks for your help.

Sorry to bump this up, but I'll lend a hand here.. Give me a few hours and I'll repost with some new code as well.

Your first sketch was more on the right track, you just have your serials mixed up pretty badly :P also, depending on what you want to do with the gps, you should probably use the tinyGPS library, since you can easily get the longitude, latitude and also the distance to a location. just google for it and you'll find it easily :)

The main reason you're having troubles is because the board you're using (an uno) doesn't have serial1, serial2 and serial3 (only the Mega has those). Unfortunately, while yes, you can put a GPS shield on the Mega, you still won't be able to use the serial1 pins because the shield is designed to work with an Uno, so they wouldn't be connected.

To clarify another thing as well, Serial (eg, Serial.begin(9600) and Serial.print("text")) is for talking to the computer, you need to set up a software serial port (which you kind of did but then didn't use it) for talking to the GPS. Like I said, i'll repost later today with some code for you.

Hope that helps a bit.

Cheers, Dan

Little bit of code for you, hopefully it all makes sense. It’s fully commented so it should. Any questions just ask. From here on you have to figure it out on your own :slight_smile: that’s seriously the best way to learn - make sure you understand all the code here too.

#include <SoftwareSerial.h>  // make sure this is NewSoftSerial beta 11 - now called SoftwareSerial
#include <TinyGPS.h>       // you can get TinyGPS from the same place as NewSoftSerial - Mikal Hart's site.


SoftwareSerial GPSSerial(2, 3);  // Set up the software serial port on pins 2 and 3 for the GPS.
TinyGPS gps;                     // instantiate the tinygps instance and call it 'gps'


void setup()
{
  GPSSerial.begin(4800);   // start up the serial port used to talk to the GPS
  Serial.begin(9600);      // start the inbuilt serial port used to talk to the Serial Debugger in the IDE
  
  float CurLat;  // variable for the current latitude
  float CurLon;  // variable for the current longitude
  unsigned long fix_age;  // variable for the age of the location fix (ie, when did we last get a good fix?)
}

void loop()
{
  while (GPSSerial.available())  // if serial data is available then do the stuff below..
  {
    if (gps.encode(c))  // valid data was received from the gps and has been parsed, ready for use
	{
      gps.f_get_position(&CurLat, &CurLon, &fix_age);  // get the current location data from the GPS
	  
	  Serial.print("Current Latitude: ");
	  Serial.print(CurLat)  // output current latitude
	  Serial.print("Current Longitude: ");
	  Serial.print(CurLon)  // output current longitude
	  Serial.print("Age of Fix: ");
	  Serial.print(fix_age)  // output age of GPS fix
	  Serial.print("  |  ");  // put a break between this output and the next output, maybe Serial.println(); would put the next output on a new line..
	  
	  delay(10000);             // wait 10 seconds then continue - this stops the serial debugger from getting swamped.
    }
  }
}