Consider this Python code running on a Raspberry Pi to read data from an Arduino over UART. What will it print?
import serial ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) line = ser.readline().decode('utf-8').strip() print(line)
Think about what readline() and decode() do.
The readline() method reads bytes until a newline. decode('utf-8') converts bytes to string. strip() removes whitespace. So it prints the first line sent by Arduino.
When setting up UART communication between Raspberry Pi and Arduino, which setting must match on both devices?
Think about the speed at which data is sent.
Both devices must use the same baud rate to correctly interpret the bits sent over UART.
Look at this Raspberry Pi Python code intended to send 'Hello' to Arduino over UART. Why does it not send data?
import serial ser = serial.Serial('/dev/ttyUSB0', 9600) ser.write('Hello')
Check the type of argument write() expects.
The write() method requires a bytes object, but 'Hello' is a string. It raises a TypeError.
Choose the correct Python code to open UART port /dev/ttyS0 at 115200 baud with a 2-second timeout.
Check the correct keyword argument spelling and commas.
Option D uses correct syntax with positional baud rate and keyword timeout. Others have syntax errors or wrong argument names.
Given this Python code on Raspberry Pi sending data to Arduino, how many bytes are sent over UART?
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
data = b'Pi\nArduino\n'
ser.write(data)Count all characters including escape sequences.
The byte string b'Pi\nArduino\n' contains 'P'(1), 'i'(2), '\n'(3-4), 'A'(5), 'r'(6), 'd'(7), 'u'(8), 'i'(9), 'n'(10), 'o'(11), '\n'(12-13). The \n escape sequence represents a newline character (one byte each), not literal backslash and 'n'. Total 13 bytes.
