Bird
0
0

You want to connect a temperature sensor to Raspberry Pi using serial communication. The sensor sends data at 4800 baud. Which setup is correct to read data continuously and print it line by line?

hard🚀 Application Q15 of 15
Raspberry Pi - Serial UART Communication
You want to connect a temperature sensor to Raspberry Pi using serial communication. The sensor sends data at 4800 baud. Which setup is correct to read data continuously and print it line by line?
Aimport serial ser = serial.Serial('/dev/ttyS0', 4800) while True: line = ser.readline().decode().strip() print(line)
Bimport serial ser = serial.Serial('/dev/ttyS0', 9600) while True: line = ser.read(10).decode() print(line)
Cimport serial ser = serial.Serial('/dev/ttyUSB0', 4800) line = ser.read(5) print(line)
Dimport serial ser = serial.Serial('/dev/ttyS0', 4800) line = ser.read(5).decode() print(line)
Step-by-Step Solution
Solution:
  1. Step 1: Match baud rate and port for continuous reading

    Sensor uses 4800 baud, so serial.Serial('/dev/ttyS0', 4800) matches speed and port.
  2. Step 2: Use readline() in a loop for line-by-line reading

    Using readline() reads until newline, decode() converts bytes to string, strip() removes extra spaces, and print() outputs lines continuously.
  3. Final Answer:

    import serial ser = serial.Serial('/dev/ttyS0', 4800) while True: line = ser.readline().decode().strip() print(line) -> Option A
  4. Quick Check:

    Correct baud + readline loop = continuous line reading [OK]
Quick Trick: Use readline() with correct baud rate for continuous sensor data [OK]
Common Mistakes:
MISTAKES
  • Using wrong baud rate causing no data
  • Reading fixed bytes instead of lines
  • Using wrong port name for device

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes