This repository has been archived on 2025-11-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
RP2040_Resources/1_Basic_GPIO/digitalIn.md

27 lines
1.2 KiB
Markdown

## Read Button
This example uses `gpio_get` api call to read and return the logic level of a gpio pin. A button is used to control the logic level of the pin. The connections are made as below. An additional concept called pulling is used to properly switch between high and low.
```c
int buttonPin = 15;
int ledPin = 14;
gpio_init(buttonPin);
gpio_init(ledPin);
gpio_set_dir(buttonPin, GPIO_IN);
gpio_pull_down(buttonPin);
gpio_set_dir(ledPin, GPIO_OUT);
while (true){
gpio_put(ledPin, gpio_get(buttonPin));
sleep_ms(10);
}
```
The usual `gpio_init` and `gpio_set_dir` are used to initialize and set the direction of the pins. Here, the pin which connects to the button gets set as input using `GPIO_IN`. The button is also configured in pull down configuration. [This guide](https://eepower.com/resistor-guide/resistor-applications/pull-up-resistor-pull-down-resistor) explains the use of pull up and pull down resistors.
- insert gif of controlling led with button
The same code can be used for controlling the fan from earlier experiment. LED connected to the `ledPin` is replaced with connection to the gate pin of the mosfet.
- insert gif of controlling fan with button