0
0
Embedded Cprogramming~15 mins

Reading digital input pin state in Embedded C - Mini Project: Build & Apply

Choose your learning style9 modes available
Reading digital input pin state
📖 Scenario: You are working with a microcontroller to read the state of a button connected to a digital input pin. The button can be pressed or released, and you want to detect its current state in your program.
🎯 Goal: Build a simple embedded C program that reads the digital input pin state of a button and prints whether the button is pressed or released.
📋 What You'll Learn
Create a variable to represent the digital input pin state.
Create a variable to represent the button pin number.
Read the digital input pin state using a function call.
Print the button state as 'Pressed' or 'Released'.
💡 Why This Matters
🌍 Real World
Reading digital input pins is essential in embedded systems to detect button presses, switches, or sensor signals.
💼 Career
Embedded software developers often need to read hardware pin states to interact with physical devices and control systems.
Progress0 / 4 steps
1
Setup button pin number variable
Create an integer variable called buttonPin and set it to 2 to represent the digital input pin number where the button is connected.
Embedded C
Need a hint?

Use int buttonPin = 2; to store the pin number.

2
Create variable for button state
Create an integer variable called buttonState to store the digital input pin state of the button.
Embedded C
Need a hint?

Declare int buttonState; without initializing it yet.

3
Read the digital input pin state
Use the function digitalRead(buttonPin) to read the button state and assign it to the variable buttonState.
Embedded C
Need a hint?

Use buttonState = digitalRead(buttonPin); to read the pin.

4
Print the button state
Use printf to print "Pressed" if buttonState equals 1, otherwise print "Released".
Embedded C
Need a hint?

Use an if statement to check buttonState and print accordingly.