Print a Message in UART console

  1. Introduction:

This experiment demonstrates how to set up UART communication between a Raspberry Pi Pico microcontroller and a PC using Putty as the serial console. The goal is to print the message "Hello Riser" on the UART console.

  1. Scope:

The scope of this experiment encompasses setting up and configuring UART communication between a Raspberry Pi Pico microcontroller and a PC. This involves several key steps: initializing the UART interface on the Pico, establishing the necessary hardware connections, and configuring a serial terminal application on the PC. The primary objective is to successfully transmit the string "Hello Riser" from the Pico to the PC, displaying it in the terminal. By completing this experiment, you will gain practical experience in configuring UART communication, uploading and running MicroPython code on the Pico, and using serial terminal software like Putty.

  1. Prerequisite:

3.1. Hardware:

  • Pico microcontroller

  • Breadboard

  • USB cable (to connect the computer)

3 .2. Software:

  • Thonny Software(For micro python)

  • MicroPython firmware installed on Raspberry Pi Pico

  • ubuntu Terminal(For embedded C)

  • CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.

  1. Connection Details:

Direct USB Connection

  1. Connect the Raspberry Pi Pico to your PC using a Micro USB cable.

  2. Ensure the Pico is recognized as a COM port in the Device Manager (Windows) or as a serial device (Linux/Mac).



  1. Working Principle:

The Raspberry Pi Pico communicates with the PC using UART protocol. The UART (Universal Asynchronous Receiver/Transmitter) is a hardware communication protocol that uses two wires, TX and RX, for serial communication. When the Pico sends data from its TX pin, it can be read by the RX pin on the PC, and vice versa. The MicroPython code initializes the UART communication on the Pico, configures the baud rate, and sends the string "Hello Riser" over the UART interface. This message is then displayed on the PC using a serial terminal application like Putty.

Embedded systems, microcontrollers, and computers mostly use a UART as a form of device-to-device hardware communication protocol. Among the available communication protocols, a UART uses only two wires for its transmitting and receiving ends.

Despite being a widely used method of hardware communication protocol, it is not fully optimized all the time. Proper implementation of frame protocol is commonly disregarded when using the UART module inside the microcontroller.

By definition, UART is a hardware communication protocol that uses asynchronous serial communication with configurable speed. Asynchronous means there is no clock signal to synchronize the output bits from the transmitting device going to the receiving end.

UART Interface


Figure 1. Two UARTs directly communicate with each other.

The two signals of each UART device are named:

  • Transmitter (Tx)

  • Receiver (Rx)

The main purpose of a transmitter and receiver line for each device is to transmit and receive serial data intended for serial communication.

The transmitting UART is connected to a controlling data bus that sends data in a parallel form. From this, the data will now be transmitted on the transmission line (wire) serially, bit by bit, to the receiving UART. This, in turn, will convert the serial data into parallel for the receiving device.

The UART lines serve as the communication medium to transmit and receive one data to another. Take note that a UART device has a transmit and receive pin dedicated for either transmitting or receiving.

For UART and most serial communications, the baud rate needs to be set the same on both the transmitting and receiving device. The baud rate is the rate at which information is transferred to a communication channel. In the serial port context, the set baud rate will serve as the maximum number of bits per second to be transferred.

The UART interface does not use a clock signal to synchronize the transmitter and receiver devices; it transmits data asynchronously. Instead of a clock signal, the transmitter generates a bitstream based on its clock signal while the receiver is using its internal clock signal to sample the incoming data. The point of synchronization is managed by having the same baud rate on both devices. Failure to do so may affect the timing of sending and receiving data that can cause discrepancies during data handling. The allowable difference of baud rate is up to 10% before the timing of bits gets too far off.

Embedded systems, microcontrollers, and computers mostly use a UART as a form of device-to-device hardware communication protocol. Among the available communication protocols, a UART uses only two wires for its transmitting and receiving ends.

Despite being a widely used method of hardware communication protocol, it is not fully optimized all the time. Proper implementation of frame protocol is commonly disregarded when using the UART module inside the microcontroller.

By definition, UART is a hardware communication protocol that uses asynchronous serial communication with configurable speed. Asynchronous means there is no clock signal to synchronize the output bits from the transmitting device going to the receiving end.

UART Interface


Figure 1. Two UARTs directly communicate with each other.

The two signals of each UART device are named:

  • Transmitter (Tx)

  • Receiver (Rx)

The main purpose of a transmitter and receiver line for each device is to transmit and receive serial data intended for serial communication.

The transmitting UART is connected to a controlling data bus that sends data in a parallel form. From this, the data will now be transmitted on the transmission line (wire) serially, bit by bit, to the receiving UART. This, in turn, will convert the serial data into parallel for the receiving device.

The UART lines serve as the communication medium to transmit and receive one data to another. Take note that a UART device has a transmit and receive pin dedicated for either transmitting or receiving.

For UART and most serial communications, the baud rate needs to be set the same on both the transmitting and receiving device. The baud rate is the rate at which information is transferred to a communication channel. In the serial port context, the set baud rate will serve as the maximum number of bits per second to be transferred.

The UART interface does not use a clock signal to synchronize the transmitter and receiver devices; it transmits data asynchronously. Instead of a clock signal, the transmitter generates a bitstream based on its clock signal while the receiver is using its internal clock signal to sample the incoming data. The point of synchronization is managed by having the same baud rate on both devices. Failure to do so may affect the timing of sending and receiving data that can cause discrepancies during data handling. The allowable difference of baud rate is up to 10% before the timing of bits gets too far off.

Data Transmission

In a UART, the mode of transmission is in the form of a packet. The piece that connects the transmitter and receiver includes the creation of serial packets and controls those physical hardware lines. A packet consists of a start bit, data frame, a parity bit, and stop bits.


Figure 3. UART packet.

Start Bit

The UART data transmission line is normally held at a high voltage level when it’s not transmitting data. To start the transfer of data, the transmitting UART pulls the transmission line from high to low for one (1) clock cycle. When the receiving UART detects the high to low voltage transition, it begins reading the bits in the data frame at the frequency of the baud rate.


Figure 4. Start bit.

Data Frame

The data frame contains the actual data being transferred. It can be five (5) bits up to eight (8) bits long if a parity bit is used. If no parity bit is used, the data frame can be nine (9) bits long. In most cases, the data is sent with the least significant bit first.


Figure 5. Data frame.

Parity

Parity describes the evenness or oddness of a number. The parity bit is a way for the receiving UART to tell if any data has changed during transmission. Bits can be changed by electromagnetic radiation, mismatched baud rates, or long-distance data transfers.

After the receiving UART reads the data frame, it counts the number of bits with a value of 1 and checks if the total is an even or odd number. If the parity bit is a 0 (even parity), the 1 or logic-high bit in the data frame should total to an even number. If the parity bit is a 1 (odd parity), the 1 bit or logic highs in the data frame should total to an odd number.

When the parity bit matches the data, the UART knows that the transmission was free of errors. But if the parity bit is a 0, and the total is odd, or the parity bit is a 1, and the total is even, the UART knows that bits in the data frame have changed.


Figure 6. Parity bits.

Stop Bits

To signal the end of the data packet, the sending UART drives the data transmission line from a low voltage to a high voltage for one (1) to two (2) bit(s) duration.


Figure 7. Stop bits.

Steps of UART Transmission

First: The transmitting UART receives data in parallel from the data bus.


Figure 8. Data bus to the transmitting UART.

Second: The transmitting UART adds the start bit, parity bit, and the stop bit(s) to the data frame.


Figure 9. UART data frame at the Tx side.

Third: The entire packet is sent serially starting from start bit to stop bit from the transmitting UART to the receiving UART. The receiving UART samples the data line at the preconfigured baud rate.


Figure 10. UART transmission.

Fourth: The receiving UART discards the start bit, parity bit, and stop bit from the data frame.


Figure 11. The UART data frame at the Rx side.

Fifth: The receiving UART converts the serial data back into parallel and transfers it to the data bus on the receiving end.


Figure 12. Receiving UART to data bus.



  1. Application code:



  • MicroPython:

from machine import UART, Pin

from time import sleep


# Initialize UART with the default pins (TX: GPIO0, RX: GPIO1)

uart = UART(0, baudrate=115200, tx=Pin(0), rx=Pin(1))


# Give UART some time to initialize

sleep(1)


# Print "Hello Riser" to the UART console

while(True):

uart.write("Hello Riser\n")

sleep(1)


  • Embedded C:



#include "pico/stdlib.h"


int main() {

// Initialize UART with the default baud rate (115200)

stdio_init_all();


// Wait for the UART to be ready

sleep_ms(1000);


// Print "Hello Riser" to the UART console

While(True)

{

printf("Hello Riser\n");

sleep_ms(1000);

}

return 0;

}

  1. Hardware Image:





  1. Output:

When the code runs, the message "Hello Riser" should appear in the Putty serial console. Ensure Putty is configured correctly:

  • Serial Line: The correct COM port (check in Device Manager)

  • Speed: 115200

  • Data Bits: 8

  • Stop Bits: 1

  • Parity: None








  1. Video Demonstration:





  1. Appendix:



  • Common CMakeLists:

cmake_minimum_required(VERSION 3.12)


# Pull in SDK (must be before project)

include(pico_sdk_import.cmake)


project(pico_experiments C CXX ASM)


set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD 17)


if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")

    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")

endif()


# Initialize the SDK

pico_sdk_init()


add_compile_options(-Wall

        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int

        -Wno-unused-function # we have some for the docs that arent called

        )

if (CMAKE_C_COMPILER_ID STREQUAL "GNU")

    add_compile_options(-Wno-maybe-uninitialized)

endif()


# Hardware-specific examples in subdirectories:

add_subdirectory(expt_10_LCD)

  • CMakeLists:

cmake_minimum_required(VERSION 3.12)


include(pico_sdk_import.cmake)


project(hello_riser_project)


pico_sdk_init()


add_executable(hello_riser main.c)


target_link_libraries(hello_riser pico_stdlib)


pico_add_extra_outputs(hello_riser)

Class Schedules:
# Topic Date & time Action

Curriculum

0% Completed (0/7)