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);
}
Following code blinks the onboard LED and then an external LED connected to GPIO 16
#include <stdio.h>
#include <pico/stdlib.h>
#include <hardware/gpio.h>
#define ledPin1 25
#define ledPin2 16
void setup()
{
stdio_init_all();
gpio_init(ledPin1); // Initialize GPIO 25
gpio_init(ledPin2); // Initialize GPIO 16
gpio_set_dir(ledPin1,true); // Configure GPIO 25 as an output
gpio_set_dir(ledPin2,true); // Configure GPIO 16 as an output
}
void loop()
{
gpio_put(ledPin1,true); //Turn GPIO 25 on
sleep_ms(1000); //Pause 1000 miliseconds
gpio_put(ledPin1,false); //Turn GPIO 25 off
sleep_ms(1000); //Pause 1000 miliseconds
gpio_put(ledPin2,true); //Same thing for external LED but blinking rate is 0.2 s
sleep_ms(200);
gpio_put(ledPin2,false);
sleep_ms(200);
}
int main()
{
setup();
while (true)
loop();
}
HIGH
or LOW
using a GPIOFollowing 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>
#define inPin 15
void setup()
{
stdio_init_all();
gpio_init(inPin); // Configure GPIO 15 as an input
gpio_set_dir(inPin,false); // Configure GPIO 15 as an input
gpio_set_pulls(inPin,0,0); // Set GPIO input as floating (0,0), pull down (0,1) or pull up (1,0)
}
void loop()
{
bool x = gpio_get(inPin); // Read GPIO state, i.e. HIGH or LOW
printf("%u\r\n",x); // Print out the GPIO state as 1 or 0
sleep_ms(200); //Sleep to give the print function time to work properly
}
int main()
{
setup();
while (true)
loop();
}