Challenge - 5 Problems
Digital Input Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of reading a digital input pin?
Consider the following embedded C code snippet that reads a digital input pin connected to a button. What value will be printed if the button is pressed (assuming active HIGH)?
Embedded C
#include <stdio.h> #define BUTTON_PIN 2 int digitalRead(int pin) { // Simulated hardware input: returns 1 if pressed, 0 if not if (pin == BUTTON_PIN) return 1; return 0; } int main() { int buttonState = digitalRead(BUTTON_PIN); printf("Button state: %d\n", buttonState); return 0; }
Attempts:
2 left
💡 Hint
Think about what digitalRead returns when the button is pressed.
✗ Incorrect
The digitalRead function returns 1 when the button is pressed (active HIGH). So the printed output is 'Button state: 1'.
❓ Predict Output
intermediate2:00remaining
What happens if you read an unconfigured input pin?
Given this code snippet, what output will it produce if the input pin is not configured and floating?
Embedded C
#include <stdio.h> #define INPUT_PIN 5 int digitalRead(int pin) { // Simulate floating pin returns random 0 or 1 if (pin == INPUT_PIN) return 0; // Simulated as LOW return 1; } int main() { int state = digitalRead(INPUT_PIN); printf("Pin state: %d\n", state); return 0; }
Attempts:
2 left
💡 Hint
Floating pins often read as LOW or unpredictable, here simulated as LOW.
✗ Incorrect
The digitalRead function simulates the floating pin as LOW (0), so the output is 'Pin state: 0'.
🔧 Debug
advanced2:30remaining
Why does this code always print 0 for the input pin?
Find the bug in this code that causes the input pin state to always print 0, even when the button is pressed.
Embedded C
#include <stdio.h> #define BUTTON_PIN 3 int digitalRead(int pin) { if (pin == BUTTON_PIN) return 1; return 0; } int main() { int buttonState = 0; if (BUTTON_PIN = 3) { buttonState = digitalRead(BUTTON_PIN); } printf("Button state: %d\n", buttonState); return 0; }
Attempts:
2 left
💡 Hint
Check the if condition syntax carefully.
✗ Incorrect
The if condition uses '=' which assigns 3 to BUTTON_PIN (which is a macro, so this causes a compile error or unexpected behavior). It should use '==' to compare. This causes the code to behave incorrectly.
🧠 Conceptual
advanced1:30remaining
What is the effect of enabling pull-up resistor on a digital input pin?
In embedded systems, enabling an internal pull-up resistor on a digital input pin means:
Attempts:
2 left
💡 Hint
Pull-up resistors pull the pin voltage up to a high level when no external signal is applied.
✗ Incorrect
Enabling a pull-up resistor connects the pin internally to a high voltage through a resistor, so the pin reads HIGH when the button is not pressed (open). Pressing the button connects it to ground, reading LOW.
❓ Predict Output
expert3:00remaining
What is the output of this code reading multiple digital input pins?
Given the following code that reads three digital input pins and prints their states, what is the output?
Embedded C
#include <stdio.h> int digitalRead(int pin) { switch(pin) { case 1: return 0; case 2: return 1; case 3: return 1; default: return -1; } } int main() { int states[3]; for (int i = 0; i < 3; i++) { states[i] = digitalRead(i + 1); } printf("States: %d %d %d\n", states[0], states[1], states[2]); return 0; }
Attempts:
2 left
💡 Hint
Check the digitalRead return values for each pin number.
✗ Incorrect
digitalRead returns 0 for pin 1, 1 for pin 2, and 1 for pin 3. So the printed states are '0 1 1'.