Lab 6: UART Serial Communications

This lab allows students to explore the basics of serial communication using a microcontroller (such as an Arduino UNO) as the transmitter and the Analog Discovery Studio as the receiver. Students will explore and visualize the effects of different factors (such as baud rates and endian-ness) on serial communications. Advanced students can challenge themselves to build a multi-device transmitter and receiver system or investigate decoding signals from binary back to decimal values.


Introduction

In this lab we will explore serial transfer via UART and how to successfully transfer and receive the signal using the Analog Discovery Studio. We will be using a microcontroller programmable with LabVIEW, in this case an Arduino UNO, as the serial transmitter by creating a start and stop bit and setting a baud rate to transfer the data at a specific speed. In later parts of the lab, we will then receive the signal using the Analog Discovery Studio with the Mixed Signal Oscilloscope functionality.

Learning Objectives

In this section, students will:

  • Investigate ways to send Serial Data
  • Learn about the basic properties of Serial UART Communication
  • See the value of start and stop bits and the effect of baud rate
  • Use LabVIEW to receive and decode a serial signal
  • Visualize how synchronization bits are used to see data packets

Part List

The following equipment is required for this experiment:

Hardware


Serial UART Communications

In a Serial UART (Universal Asynchronous Receiver Transmitter) communications system, a transmitter is used to send a digital signal to a receiver. Compared to other communication protocols, UART is very useful because it is an asynchronous system; it can transmit signals without needing to synchronize with a clock signal. This method of transmission is extremely useful for reducing wires and I/O pins compared to parallel systems which transmit data faster but are far more complex. The serial transmission signal is sent in binary format, one data point after another, and flagged by start and stop bits, not decided by a clock signal. This is where the term Asynchronous Serial Transmission comes from.

To successfully transmit an asynchronous signal, the following items must be considered:

  • baud rate
  • data bits
  • synchronization bits
  • parity bits

Baud Rate

The baud rate, specified in bits-per-second (bps), indicates how fast a signal is sent over a serial line. One of the more common baud rates is 9600bps which will be used as the default for this lab. In a communications system, both the receiver and the transmitter will have to match baud rates to successfully transfer data.

Data Bits and Endian-ness

Data bits are derived from the actual signal being sent over the line. The standard data packet is 8-bits, or a single byte, but can be other sizes. Since these bits are sent in a serial manner, the receiver needs to know whether the first bit received is the most significant bit or least significant bit. This is known as the endian notation of the data. In a big-endian system, the most significant bit is stored first with the following bits in decreasing significant order. The little-endian format is the exact opposite and stores the least significant bit in the first location. As with the baud rate, the endian notation of the communications system can be decided beforehand and hard-coded into the system.

Synchronization Bits

Besides data bits, the serial protocol also includes a start and stop bit that bookend the serial data packet. The start and stop bits do not transmit data, but rather mark the beginning and end of a data packet. The start bit is always set as a data line going from high to low with the stop bit going back from the last data signal to high. In this case, a constant high line indicates an idle state.

Parity Bits

Parity bits are a basic error checking method. When data is being transferred, occasionally that data becomes lost along the way or a transition is missed by the receiver. Sometimes the last data bit is used as a parity check to see if data has been missed. If the total sum of the data packet is even, then the parity bit is set to 0. Alternatively, if the sum of the data byte is odd, then the bit is set to 1. That way, the receiver can count how much data was received, compare that to the parity bit, and determine if all of the data has been received. Parity bits are useful for validating data but is optional.

Questions and Exercises
  • If transmitting a signal with a baud rate at 9600bps, how much time does it take to transmit a single bit?
  • What possible errors could occur from setting a faster baud rate than can be received from a signal receiver?
  • Given the binary data 0b01001110, what value would the parity bit be set? Additionally, what advantages and disadvantages would there be in using a parity bit?
  • Say a serial data packet 0b01001110 was received by a serial receiver. What would the decimal value of the packet be if the system was used the little-endian notation? How about big-endian notation?

Analysis with LabVIEW

Now we will go ahead and experiment with sending and receiving signals using a microcontroller and Analog Discovery Studio. This lab uses the Arduino UNO microcontroller, but the exact same code can be used with a chipKIT or other similar microcontroller. Follow the steps below to output a serial signal.

One way to take those signals and recover the original message signal is to acquire the signal with the Logic Analyzer from the Analog Discovery Studio in LabVIEW. We will be doing this by sampling the signal using the Analog Discovery studio and displaying it in LabVIEW. By taking advantage of LabVIEW’s ability to visualize data, we can see how each step in this process is accomplished.

In order to receive the signal, we will first acquire the signal using the digital capabilities of the Mixed Signal Oscilloscope on the Analog Discovery Studio and then use software controls to set an appropriate sample rate. We will then look at baud rates and the start and stop bits and compare the binary output with the signal we sent with the help of the microcontroller.

This section of the lab will assume a working knowledge of the LabVIEW environment and basic programming conventions. For more information on getting started in LabVIEW, including the installation of the Digilent WaveForms VIs, please view the resources available here: Getting Started with LabVIEW and a Digilent Discovery Device.

Note: Before testing or running your LabVIEW code, make sure that WaveForms is not opened. The Digilent WaveForms VIs will throw an error if Digilent WaveForms is still open when you run your code.

Note: If you don't know what a VI does, you can check the Context Help by pressing Ctrl+H, then highlighting the respective VI.


Objectives

Design a VI in LabVIEW that will send an UART signal on one of the microcontroller's pins using LINX and a VI that reads in a serial signal and displays the digital waveform. You can build your own VIs, following the steps below, or you can download the VIs used in this guide from here: uart.zip.

The first VI, UART_send.vi, is used to send out serial data. The Front Panel of this VI contains control elements for setting the COM port to which the microcontroller is connected, setting the digital pin used to send data, setting the signal parameters (baud rate, endian-ness, parity bit, stop bit number), entering the data to be sent and two buttons to send the data and to stop the program. Two indicators are also present to display the current status of the microcontroller and the data in binary form.

The Block Diagram contains, except from the control and indicator elements, the blocks needed for communication with the microcontroller and data processing.

The UART_receive VI's Front Panel contains controls for selecting the used Test and Measurement device, setting the digital IO channel used for receiving and the baud rate. The main element of this front panel is the Digital Waveform Graph used to display the received signal. A stop button is also present, allowing the user to stop the reception when they want.

The Block Diagram of this VI contains the blocks needed to control the Mixed Signal Oscilloscope.

General Operation

The transmitter VI should generate the bit array from the inputted data, then output these bits on the chosen pin of the connected microcontroller. After writing a bit, the next one should be delayed with a time set by the baud rate.

The receiver VI should set the Mixed Signal Oscilloscope in the desired mode, then wait for a signal. If the signal is received, it should display it. If no signal is received, the scope should be auto triggered, to prevent it blocking the program. After the channel was triggered, measurements should be restarted.

The image below presents the general program flow of these two VIs.


Building and Configuring Your Circuit

Connect the ground of the microcontroller to the ground of the Analog Discovery Studio (black wire) and connect a digital pin of the microcontroller to a digital pin of the Analog Discovery Studio. In this lab, pin 11 of the Arduino UNO will be connected to the DIO line 0 of the ADS. Also, connect the microcontroller to the PC with a USB cable.

You can download the wiring diagram here: wiring_diagram_digital.zip

Follow the instructions below to set up your microcontroller in LabVIEW and send out the serial signal for this experiment. In LabVIEW, navigate to ToolsMakerHubLINXLINX Firmware Wizard. Set the Device Family to the brand of microcontroller you are using, the Device Type to the microcontroller type and the Firmware Upload Method to Serial / USB.

In the next window, select the COM port connected to your microcontroller, then hit Next. Click Next again and click Finish. This allows the LINX Toolkit to automatically configure the firmware to work with LabVIEW.

Note: The COM port can be identified by going to Windows Device Manager → USB Serial Port.

Important

In the latest version of LabVIEW, Digilent LINX VIs can be found in the Hobbyist panel. Tools like the Firmware Wizard are also placed in the Hobbyist submenu.


Software Setup

For this laboratory we need two VIs: the transmitter and the receiver.

The Transmitter (UART_send.vi)

Setup and Instrument Configuration

As a first step the control and indicator elements should be placed by right-clicking on the Front Panel and selecting the required element. In this VI we need three Numeric Controls: one which sets the used digital pin, one which sets the Baud Rate (due to the processing speed of LabVIEW, the maximum achievable baud rate is around 150 bits per second) and one which sets the number of stop bits. A Combo Box should be used for selecting endian-ness, a button for enabling the parity bit and a String Control for entering the data to be sent. A different button for sending the message is also necessary. Two String Indicators can be used to display the current state of the microcontroller and the data in binary format (with all start, stop and parity bits). The drop-down list which sets the serial port connected to the microcontroller, can be added later by right-clicking on the respective input of the Open.vi subvi from the LINX tab. A Stop Button should also be placed on the Front Panel, to interrupt the program if needed.

Arrange everything on the Front Panel. Rename the placed elements by double-clicking on their name.

In the Block Diagram, initialize the microcontroller connected to a serial port with the Open.vi from the LINX tab. Don't forget to add the necessary control element to the Front Panel. After the MCU is connected, set the selected digital pin to high (this is the idle level for UART communication). You can output a message about the current operation with the help of the respective indicator and local variables.

Data Conversion and Sending

In a while loop create an event structure and set the triggering event to Mouse Up on the Send button. VIs in this structure will only be accessed when the send button is pressed.

To create the binary message from the inputted character, do the following:

  • convert the data character to a byte (unsigned 8-bit integer)
  • convert the byte to a bit array,
  • arrange the bits according to the selected endian-ness,
  • append the start bit (a 0),
  • calculate and append the parity bit if required,
  • append the required number of stop bits.

After conversion, the full message can be displayed on the Front Panel in binary format (this will be necessary when comparing with the received signal).

When the conversion is ready, data is sent bit after bit. Measure the current time before sending the bit, set the bit value on the selected digital pin, then measure the time again. Substract the difference of these times (the duration of setting the pin) from the reciprocal of the baud rate (in milliseconds). The resulting time gives you the amount you have to wait before sending out the next bit.

If an error appears, or the Stop button is pressed, exit the loop and close the MCU, then handle the errors.

Note: Flat Sequence Structures can help you to organize your VIs in the desired order.

Note: It can happen that LabVIEW can not start the communication with your microcontroller when running the VI. If an error appears on start, check the selected Serial Port, then rerun the VI.

The Receiver (UART_receive.vi)

Setup and Instrument Configuration

As a first step the control and indicator elements should be placed by right-clicking on the Front Panel and selecting the required element. In this VI we need two Combo Boxes: one which sets the device type, with the elements “Analog Discovery Studio”, “Analog Discovery 2” and “Analog Discovery” and one which sets the digital channel used for communication with the microcontroller, with the elements “DIO 0” - “DIO 15”. A Numeric Control is also needed for setting the baud rate. A Stop Button should also be placed on the Front Panel, to interrupt the program if needed.

Arrange everything on the Front Panel. Rename the placed elements by double-clicking on their name.

Place a Digital Waveform Graph on the Front Panel, then turn off the auto scaling for both axes. Right-click on the graph, click on Properties then select Cursors. Add 11 vertical Single-Plot cursors and set a visible style for them. Double click on the Front Panel anywhere to create a note. Create labels for the start-, stop-, parity- and data bits, then arrange these labels above the graph.

In the Block Diagram create property nodes for the digital graph's YScale.Minimum, YScale.Maximum, XScale.Minimum, XScale.Maximum, ActCrs and Cursor.PosX properties. Set the Y axis range according to the selected digital channel (DIO 0 is on position 15, DIO 1 is on position 14, etc.). By default, the graph represents the whole buffer and the buffer size is 1000. We received signal will have maximum 12 parts (start bit, 8 data bits, parity bit and two stop bits), which means, that each bit should be sampled $\frac{1000}{12}=83$ times, with the speed set by the baud rate. The remaining four bits from the buffer will be filled before triggering, so the X axis of the graph needs an offset of four time the sample time: $t_{sample}=\frac{1}{83br}$, where $br$ is the baud rate. The higher limit of the X axis can be calculated if the sample time is multiplied with $1000-4=996$.

Cursors should be positioned after each “section”, which means 83 samples, so set the X position of the first cursor to $t_{cursor}=\frac{996t_{sample}}{12}$ away from the offset and every other cursor to $t_{cursor}$ away from the previous one.

After setting up the graph, initialize the mixed signal oscilloscope (MSO). Place the Configure Advance Digital Timing VI, set it to manual mode, the set the pretrigger time to $\frac{4}{1000}=0,4$ and the sample rate to $\frac{1}{t_{sample}}=83br$.

Enable digital edge triggering on the falling edge of the selected channel with the Enable Digital Channels and Configure Digital Edge Triggering VIs. Start the instrument, but enable auto triggering, to prevent the Scope from blocking the program.

Reception

In a while loop, read the Scope. Display the results, then compare them to the ones from the previous iteration (to a blank array in the first iteration). If the results are the same as the previous ones, or the instrument was auto triggered, restart it.

If an error appears, or the Stop button is pressed, exit the loop and close the used instrument, disconnect the device, then handle the errors.

Questions and Exercises
  • The Arduino UNO comes with one UART Channel. Try setting the UART channel in the transmitter VI to pin 1, then to pin 0. Why do you think the results are now erroneous? What could be using these channels?
  • How does the acquired digital waveform compare to the data that was sent? Does the acquired binary data match up to the binary representation of the character sent (remember to take into account the endian-ness of the communications system)?
  • What happens as the baud rate of the transmitter increases or decreases? Zoom in to the digital waveform to better visualize the data if necessary.
  • Change the trigger edge slope to Rising Edge. How does changing the Edge Slope change the acquired data? Knowing that the start bit goes from 1 to 0, which trigger slope configuration would be best for this system?
  • By using various Digital Sample Rates and Baud Rates, you may have noticed data transmission being miscommunicated. Why would a parity bit as discussed earlier in the lab be useful in such cases?

Further Exploration

Serial Decoding with LabVIEW

In this lab we decided to demodulate by visualizing the data using a graph. However, there are other ways to visualize the data. Build on what you learned in this lab to implement a receiver that decodes the signal from binary back to decimal instead of just displaying the binary values. Try to implement ASCII character transmission and convert back to ASCII characters on the receiver. Try to use different settings in the transmitter VI.


Next Steps

For more complementary laboratories, return to the Complementary Labs for Electrical Engineering page of this wiki.

For technical support, please visit the Test and Measurement section of the Digilent Forums.