Bird
0
0
Raspberry Piprogramming~20 mins

Communicating with Arduino over UART in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
UART Communication Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this UART read code snippet?

Consider this Python code running on a Raspberry Pi to read data from an Arduino over UART. What will it print?

Raspberry Pi
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
line = ser.readline().decode('utf-8').strip()
print(line)
APrints raw bytes without decoding
BRaises a SerialException due to missing port
CPrints the first line of data sent by Arduino as a string
DPrints an empty string always
Attempts:
2 left
💡 Hint

Think about what readline() and decode() do.

🧠 Conceptual
intermediate
1:30remaining
Which UART setting is critical for successful communication?

When setting up UART communication between Raspberry Pi and Arduino, which setting must match on both devices?

ABaud rate
BGPIO pin numbering scheme
CIP address
DOperating system version
Attempts:
2 left
💡 Hint

Think about the speed at which data is sent.

🔧 Debug
advanced
2:30remaining
Why does this UART write code fail to send data?

Look at this Raspberry Pi Python code intended to send 'Hello' to Arduino over UART. Why does it not send data?

Raspberry Pi
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
ser.write('Hello')
ASerial port not opened error
BTypeError because write expects bytes, not string
CTimeout error due to missing timeout parameter
DNo error, data sent successfully
Attempts:
2 left
💡 Hint

Check the type of argument write() expects.

📝 Syntax
advanced
1:30remaining
Which option correctly opens UART port with timeout?

Choose the correct Python code to open UART port /dev/ttyS0 at 115200 baud with a 2-second timeout.

Aserial.Serial('/dev/ttyS0', 115200, time_out=2)
Bserial.Serial('/dev/ttyS0', baudrate=115200 timeout=2)
Cserial.Serial(port='/dev/ttyS0', 115200, timeout=2)
Dserial.Serial('/dev/ttyS0', 115200, timeout=2)
Attempts:
2 left
💡 Hint

Check the correct keyword argument spelling and commas.

🚀 Application
expert
3:00remaining
How many bytes are sent by this UART write sequence?

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)
A13 bytes
B12 bytes
C11 bytes
D14 bytes
Attempts:
2 left
💡 Hint

Count all characters including escape sequences.