Wednesday 5 August 2020

Serial Communications With Arduino

 This article is going to cover the topic of serial communication between an Arduino and another device. There are plenty of very good examples in the Arduino IDE that help you get to grips with the basics, but for this one we are going to do something that could actually be useful. To do this we will be using the following:

  • Neo-6M/GY-GPS6MV2 GPS Module
  • Arduino 2650 MEGA
  • CH340E USB to TTL adapter
  • breadboard
To start off with, we can illustrate a very simple example of serial communication taking place. The GPS module we have here is a self contained unit, once it is powered up and has its antenna attached, it will start to look for satellites and begin printing out its status via serial communication. I don't need to do anything at all for this to happen, however in order to read this data, we must connect a TTL/serial terminal to the module itself.

USB to TTL on left



This is where the USB to TTL module comes in to play. We can attach the serial port on this device to the serial port on the GPS module and connect the lot up to a computer using a USB cable. Once done, you will be able to use a terminal to connect to the serial port and view the raw GPS data in the form of NMEA sentences.

So, lets get started on the easy part of connecting the USB to TTL up to the GPS module. If you don't already have pins installed on these modules, go ahead and solder them in now, once done you want to put them side by side on the breadboard, its probably a good idea to use the outer most pins on the edge to seat these components.

Before doing anything else, lets make sure that the USB to TTL adapter works, and that you can use it. To do this, connect a USB cable to it and your computer, if this is the first time you are doing this your OS may tell you it is being installed, so wait for that to take place. If not, then it should just give you the noise to tell you it has been connected.

We can verify the connection by looking at the available ports your OS has available. On Windows, there are a couple of ways to do this. The best way is to go into Device Manager and look under "Ports, COM & LPT". You will see your adapter listed here if it is present, mine looks like this:

USB to TTL adapter highlighted

Note that port number down, it probably wont change and you will need it later. For now, we can disconnect the USB cable and get to work setting this up.

We need to power the GPS module, it is 5v tolerant but for right now lets run it at 3.3v - we can get this from our USB connection. Simple connect a wire between the 3V3 pin on the USB adapter and the VCC pin on the GPS module. Connect a ground wire accordingly.

Next, we need to connect the TX and RX pins to one another. When you are trying to transmit data between devices like this you need to connect these pins to their opposite.

This means you connect RX to TX and TX to RX. 

Doing so means that the device transmitting is always transmitting to a connection that is receiving and vice versa. So lets connect the TX and RX pins together, take a wire and connect TXD on the USB adapter to RX on the GPS module. Next connect the RXD pin on the USB adapter to the TX pin on the GPS module.

You need to be careful that you do not accidentally connect one of the serial pins to a power supply by mistake, this could damage your components.

Once you are happy with your connections, you could have something like this:


All we need to do now is hook up the USB cable and connect to the serial port. The best way to do this if you are using Windows is to get hold of PuTTY. Its a fantastic, free SSH and Telnet client that pretty much the entire world uses I think, you can download it from here.

So, to get this up and running, open up PuTTY. You will be greeted with a screen like this:


Make sure you have Session selected under Categories on the left, this is the highlighted field in the top left. Then select the Serial radio button, highlighted on the right. You will need to enter your COM port number here, I have entered 12 as this is the port number that has been assigned. Once I am sure everything has been done, I go ahead and click on Open.

And hey presto, you start getting GPS data back in the form of NMEA sentences, just like this:


This data will keep scrolling for as long as you keep power flowing through this circuit. As time goes by, the module will receive more and more data from the GPS network and all of those missing fields will be populated.

And all of this has been achieved without writing a single line of code.

So how useful is this? Well, in general terms, if you have a device that pins or pads designated as "UART" or "Serial", then you stand a very good chance of hooking them up to something like PuTTY to see what, if anything, is being sent out of the device. This can be really useful if you have a faulty device, sometimes they write out the error state on a serial port which can be read from another device. You might even be able to write data back over that connection - for example I have a NAS that is faulty at the current time, I have connected it up in this way to see if there is a specific error message, or any other info being written to the port. Unfortunately in this case, nothing useful turned up - but I did discover that I can flash the device using ICMP - which will be really useful once I figure out how to do it.

But in the context of the module used in the example above? Well, the data we received from it were standard NMEA sentences. To a human, they don't mean anything, we cannot really decode them without help. So with some code, we can consume those sentences and turn them into data that we can use easily, like coordinates and other measurements that we can easily comprehend.

But this is a topic for another time...