📖 Scenario: You are working with a simple microcontroller board. You want to control two LEDs connected to output pins. One LED should be turned ON (HIGH) and the other LED should be turned OFF (LOW).
🎯 Goal: Write a program that sets two output pins: one to HIGH and the other to LOW, to control two LEDs.
📋 What You'll Learn
Create two variables representing output pins named pin1 and pin2 with values 5 and 6 respectively.
Create a variable called HIGH with value 1 and a variable called LOW with value 0.
Write code to set pin1 to HIGH and pin2 to LOW using a function called digitalWrite.
Print the states of pin1 and pin2 to the console.
💡 Why This Matters
🌍 Real World
Controlling LEDs or other devices by setting microcontroller pins HIGH or LOW is a basic task in embedded systems.
💼 Career
Embedded software engineers often write code to control hardware pins to turn devices on or off.
Progress0 / 4 steps
1
Define Output Pins
Create two integer variables called pin1 and pin2 and set them to 5 and 6 respectively.
Embedded C
Hint
Use int to declare the pins and assign the numbers 5 and 6.
2
Define HIGH and LOW Values
Create two integer variables called HIGH and LOW and set them to 1 and 0 respectively.
Embedded C
Hint
HIGH means ON (1), LOW means OFF (0).
3
Set Pins to HIGH and LOW
Use the function digitalWrite to set pin1 to HIGH and pin2 to LOW.
Embedded C
Hint
Call digitalWrite twice, once for each pin and value.
4
Print Pin States
Print the states of pin1 and pin2 using printf in the format: "Pin 5 is HIGH" and "Pin 6 is LOW".
Embedded C
Hint
Use printf with %d to insert pin numbers and print the states.
Practice
(1/5)
1. Which function is used to set a pin as an output before writing HIGH or LOW to it?
easy
A. setPinMode(pin, HIGH)
B. digitalWrite(pin, OUTPUT)
C. pinMode(pin, OUTPUT)
D. writePin(pin, OUTPUT)
Solution
Step 1: Understand pin configuration
Before writing HIGH or LOW, the pin must be set as an output to control voltage.
Step 2: Identify the correct function
The function to set pin mode is pinMode(pin, OUTPUT), which configures the pin for output.
Final Answer:
pinMode(pin, OUTPUT) -> Option C
Quick Check:
Set pin as output = pinMode(pin, OUTPUT) [OK]
Hint: Set pin mode with pinMode before writing HIGH or LOW [OK]
Common Mistakes:
Using digitalWrite to set pin mode
Confusing HIGH/LOW with OUTPUT/INPUT
Forgetting to set pin mode before writing
2. Which of the following is the correct syntax to turn pin 5 ON (HIGH) in embedded C?
easy
A. digitalWrite(5, 1);
B. digitalWrite(5, ON);
C. digitalWrite(5, OUTPUT);
D. digitalWrite(5, HIGH);
Solution
Step 1: Identify the function to write pin state
The function digitalWrite(pin, value) sets the pin voltage HIGH or LOW.
Step 2: Use correct constants
Use HIGH to turn the pin ON. The correct syntax is digitalWrite(5, HIGH);.
Final Answer:
digitalWrite(5, HIGH); -> Option D
Quick Check:
Turn pin ON = digitalWrite(pin, HIGH) [OK]
Hint: Use HIGH constant, not ON or numbers, for clarity [OK]
Common Mistakes:
Using ON instead of HIGH
Using OUTPUT instead of HIGH
Using numeric 1 instead of HIGH constant
3. What will be the output state of pin 3 after running this code?
The second line digitalWrite(7, HIGH) is missing a semicolon at the end.
Step 2: Confirm other lines are correct
pinMode(7, OUTPUT); and digitalWrite(7, LOW); are correct syntax.
Final Answer:
Missing semicolon after digitalWrite(7, HIGH) -> Option A
Quick Check:
Every statement must end with semicolon [OK]
Hint: Check each line ends with a semicolon [OK]
Common Mistakes:
Forgetting semicolon after function calls
Calling pinMode after digitalWrite
Misusing HIGH/LOW constants
5. You want to toggle pin 10 ON and OFF every second in a loop. Which code snippet correctly sets pin 10 HIGH then LOW with a 1-second delay between states?
hard
A. pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(1000);
B. digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(1000);
C. pinMode(10, INPUT);
digitalWrite(10, HIGH);
delay(1000);
D. pinMode(10, OUTPUT);
digitalWrite(10, LOW);
digitalWrite(10, HIGH);
Solution
Step 1: Set pin 10 as output
Use pinMode(10, OUTPUT); to configure pin 10 for output.
Step 2: Write HIGH, wait, then LOW, wait
Use digitalWrite(10, HIGH); then delay(1000); to wait 1 second, then digitalWrite(10, LOW); and delay again.