Bird
0
0
Raspberry Piprogramming~10 mins

Communicating with Arduino over UART in Raspberry Pi - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A9600
B115200
C19200
D4800
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.
2fill in blank
medium

Complete 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'
Amessage
Bstr(message)
Cmessage.encode()
Dbytes(message, 'utf-8')
Attempts:
3 left
💡 Hint
Common Mistakes
Sending a string directly without encoding causes an error.
Using bytes() without encoding may cause unexpected results.
3fill in blank
hard

Fix 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'
Ato_string
Bdecode
Cdecode_utf8
Dencode
Attempts:
3 left
💡 Hint
Common Mistakes
Using encode() instead of decode() on bytes.
Using a non-existent method like decode_utf8 or to_string.
4fill in blank
hard

Fill 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'
Alen(word)
B>
C>=
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of len(word) for dictionary values.
Using '>=' instead of '>' which includes words of length 3.
5fill in blank
hard

Fill 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'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using k instead of k.upper() for keys.
Using '<' or '>=' instead of '>' for filtering.