Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to open the UART serial port on Raspberry Pi.
Raspberry Pi
import serial ser = serial.Serial('/dev/ttyS0', [1]) print("Serial port opened")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a baud rate that does not match the Arduino's baud rate.
Forgetting to specify the baud rate when opening the serial port.
✗ Incorrect
The baud rate 9600 is the standard speed for UART communication with Arduino.
2fill in blank
mediumComplete the code to send the string 'Hello Arduino' over UART.
Raspberry Pi
message = 'Hello Arduino' ser.write([1]) print("Message sent")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Sending a string directly without encoding causes an error.
Using bytes() without encoding may cause unexpected results.
✗ Incorrect
We must encode the string to bytes before sending over UART.
3fill in blank
hardFix the error in reading a line from UART and decoding it.
Raspberry Pi
line = ser.readline() text = line.[1]('utf-8').strip() print(text)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using encode() instead of decode() on bytes.
Using a non-existent method like decode_utf8 or to_string.
✗ Incorrect
We use decode('utf-8') to convert bytes to string.
4fill in blank
hardFill both blanks to create a dictionary of word lengths for words longer than 3 characters.
Raspberry Pi
words = ['arduino', 'pi', 'uart', 'code'] lengths = {word: [1] for word in words if len(word) [2] 3} print(lengths)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of len(word) for dictionary values.
Using '>=' instead of '>' which includes words of length 3.
✗ Incorrect
We use len(word) to get length and filter words with length greater than 3.
5fill in blank
hardFill all three blanks to create a dictionary with uppercase keys and values greater than 2.
Raspberry Pi
data = {'temp': 1, 'humidity': 3, 'pressure': 5}
filtered = [1]: [2] for k, v in data.items() if v [3] 2}
print(filtered) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using k instead of k.upper() for keys.
Using '<' or '>=' instead of '>' for filtering.
✗ Incorrect
Keys are converted to uppercase with k.upper(), values are v, and filter uses '>' to select values greater than 2.
