Challenge - 5 Problems
SPI Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this SPI transfer code?
Consider the following Arduino code snippet using the SPI library. What value will be stored in
receivedData after the transfer?Arduino
#include <SPI.h> byte dataToSend = 0x3C; byte receivedData; SPI.begin(); SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); receivedData = SPI.transfer(dataToSend); SPI.endTransaction();
Attempts:
2 left
💡 Hint
Remember that SPI.transfer sends data and simultaneously receives data from the slave device.
✗ Incorrect
The SPI.transfer function sends a byte and returns the byte received from the slave device at the same time. The received value depends on the slave device's response.
🧠 Conceptual
intermediate1:30remaining
Which SPI mode is used in this code snippet?
Given the following SPISettings constructor call, which SPI mode is being set?
SPISettings(1000000, MSBFIRST, SPI_MODE3)
Attempts:
2 left
💡 Hint
SPI modes are numbered 0 to 3, combining CPOL and CPHA bits.
✗ Incorrect
SPI_MODE3 means CPOL=1 and CPHA=1, which means clock idles high and data is sampled on the trailing edge.
🔧 Debug
advanced2:00remaining
Why does this SPI code cause a runtime error?
Examine this Arduino code snippet:
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0)); SPI.transfer(0x55); SPI.endTransaction(); SPI.transfer(0xAA);Why does the last
SPI.transfer call cause a problem?Attempts:
2 left
💡 Hint
Check the order of SPI transaction calls and when transfers are allowed.
✗ Incorrect
After
SPI.endTransaction(), the SPI bus transaction ends and transfers should not be done outside a transaction.📝 Syntax
advanced1:30remaining
Which option correctly initializes SPI with a custom chip select pin?
You want to use SPI with a chip select pin connected to pin 10. Which code snippet correctly sets this up?
Attempts:
2 left
💡 Hint
Chip select pins must be outputs and set HIGH before SPI communication.
✗ Incorrect
The chip select pin must be set as an output and set HIGH before starting SPI. SPI.begin() does not take parameters.
🚀 Application
expert2:30remaining
How many bytes are transferred in this SPI sequence?
Given this Arduino code:
byte buffer[4] = {0x01, 0x02, 0x03, 0x04};
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
for (int i = 0; i < 4; i++) {
buffer[i] = SPI.transfer(buffer[i]);
}
SPI.endTransaction();
How many bytes are sent and received over SPI during this code execution?Attempts:
2 left
💡 Hint
Each SPI.transfer sends one byte and receives one byte simultaneously.
✗ Incorrect
The loop calls SPI.transfer 4 times, each sending and receiving one byte, so total 4 bytes sent and 4 bytes received.
