4.3. Code Examples#
4.3.1. Turn Onboard LED On#
The Raspberry Pi Pico board has a green LED already connected to GPIO 25. Following code configures GPIO 25 as an output and turns the LED on.
#include <pico/stdlib.h>
#include <hardware/gpio.h>
#define LED_PIN 25 // Define a pin number macro to use it throughout the code
int main()
{
gpio_init(LED_PIN); // Initialize GPIO 25 to the default state
gpio_set_dir(LED_PIN, true); // Configure GPIO 25 as an output
gpio_put(LED_PIN, true); // Drive GPIO 25 to High
while (true);
}
4.3.2. Read HIGH
or LOW
using a GPIO#
Following code configures a GPIO as an input. The exact configuration can be set by changing values in gpio_set_pulls
function.
#include <stdio.h>
#include <pico/stdlib.h>
#include <hardware/gpio.h>
void setup()
{
stdio_init_all();
gpio_init(0);
gpio_set_dir(0,0);
gpio_set_pulls(0,0,0); // Change GPIO input configuration to see different behavior
}
void loop()
{
bool x = gpio_get(0); // Read GPIO state, i.e. HIGH or LOW
printf("%u\r\n",x); // Print out the GPIO state as 1 or 0
}
int main()
{
setup();
while (true)
loop();
}