Challenge - 5 Problems
GPIO Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this GPIO pin toggle code?
Consider a microcontroller GPIO pin initially set to LOW (0). The following code toggles the pin state twice. What will be the final state printed?
Embedded C
#include <stdio.h> int main() { int gpio_pin = 0; // LOW gpio_pin = !gpio_pin; // toggle once gpio_pin = !gpio_pin; // toggle twice printf("GPIO pin state: %d\n", gpio_pin); return 0; }
Attempts:
2 left
💡 Hint
Think about what toggling means: switching from 0 to 1 or 1 to 0.
✗ Incorrect
The pin starts at 0 (LOW). First toggle changes it to 1 (HIGH). Second toggle changes it back to 0 (LOW). So the final state is 0.
🧠 Conceptual
intermediate1:30remaining
Why is GPIO considered the foundation of embedded systems?
Which of the following best explains why GPIO pins are fundamental in embedded systems?
Attempts:
2 left
💡 Hint
Think about how embedded devices sense and act in the real world.
✗ Incorrect
GPIO pins are the basic interface for input and output, enabling the microcontroller to read signals from sensors and control actuators or LEDs.
🔧 Debug
advanced2:30remaining
Identify the error in this GPIO initialization code
This code is supposed to configure a GPIO pin as output and set it HIGH. What is the error that will cause it to fail?
Embedded C
void gpio_init() {
int pin = 5;
// Configure pin 5 as output
pin = 1;
// Set pin 5 HIGH
pin = 1;
}Attempts:
2 left
💡 Hint
Think about what 'pin = 1;' does in C code.
✗ Incorrect
Assigning 1 to the variable 'pin' does not configure the hardware GPIO registers. Proper register manipulation or API calls are needed.
📝 Syntax
advanced2:00remaining
Which option correctly sets GPIO pin 3 as input with pull-up resistor enabled?
Choose the correct C code snippet for configuring GPIO pin 3 as input with pull-up enabled on a generic microcontroller.
Attempts:
2 left
💡 Hint
Input pins are cleared in direction register; pull-up bits are set.
✗ Incorrect
Clearing bit 3 in GPIO_DIR sets pin 3 as input. Setting bit 3 in GPIO_PULLUP enables pull-up resistor.
🚀 Application
expert3:00remaining
How many GPIO pins are set HIGH after this code runs?
Given the following code snippet, how many GPIO pins will be set HIGH at the end?
Embedded C
#include <stdio.h> #define NUM_PINS 4 int main() { unsigned int gpio_state = 0b0000; for (int i = 0; i < NUM_PINS; i++) { if (i % 2 == 0) { gpio_state |= (1 << i); } else { gpio_state &= ~(1 << i); } } int count = 0; for (int i = 0; i < NUM_PINS; i++) { if (gpio_state & (1 << i)) { count++; } } printf("Number of pins HIGH: %d\n", count); return 0; }
Attempts:
2 left
💡 Hint
Check which pins (0 to 3) are set HIGH based on the condition i % 2 == 0.
✗ Incorrect
Pins 0 and 2 (even indices) are set HIGH. Pins 1 and 3 are cleared. So total HIGH pins = 2.