Bird
0
0
Arduinoprogramming~20 mins

SPI library usage in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SPI Mastery Badge
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 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();
AAlways 0x00 because no slave is connected
BThe value received from the SPI slave device during transfer
CThe same value as <code>dataToSend</code> (0x3C)
DCompilation error due to missing SPI library include
Attempts:
2 left
💡 Hint
Remember that SPI.transfer sends data and simultaneously receives data from the slave device.
🧠 Conceptual
intermediate
1: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)
AClock polarity (CPOL) = 1, Clock phase (CPHA) = 0
BClock polarity (CPOL) = 0, Clock phase (CPHA) = 0
CClock polarity (CPOL) = 1, Clock phase (CPHA) = 1
DClock polarity (CPOL) = 0, Clock phase (CPHA) = 1
Attempts:
2 left
💡 Hint
SPI modes are numbered 0 to 3, combining CPOL and CPHA bits.
🔧 Debug
advanced
2: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?
ABecause <code>SPI.endTransaction()</code> was called, SPI bus is not active for transfer
BBecause the SPI clock speed is too high for the device
CBecause <code>SPI.begin()</code> was never called
DBecause <code>SPI.transfer</code> requires a chip select pin to be set manually
Attempts:
2 left
💡 Hint
Check the order of SPI transaction calls and when transfers are allowed.
📝 Syntax
advanced
1: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?
ApinMode(10, OUTPUT); digitalWrite(10, HIGH); SPI.begin();
BpinMode(10, INPUT); SPI.begin();
CSPI.setCS(10); SPI.begin();
DSPI.begin(10); pinMode(10, OUTPUT);
Attempts:
2 left
💡 Hint
Chip select pins must be outputs and set HIGH before SPI communication.
🚀 Application
expert
2: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?
A0 bytes sent and 4 bytes received
B4 bytes sent and 0 bytes received
C1 byte sent and 1 byte received
D4 bytes sent and 4 bytes received
Attempts:
2 left
💡 Hint
Each SPI.transfer sends one byte and receives one byte simultaneously.