9 de noviembre de 2025
En el proyecto nº 1 de esta serie de tutoriales, vimos cómo encender el led integrado en la placa Pico 2 W. Dicho led es diferente al resto porque no está conectado a un pin GPIO del microcontrolador, si no que está controlado por el chip wifi.
Así que a continuación veremos cómo controlar ledes externos conectados en los pines GPIO. Para ello conectamos tres ledes como se muestra en el siguiente esquema.
Este código enciende el LED 0 conectado en el pin 17 (GPIO 13).
#include <stdio.h>
#include "pico/stdlib.h"
#define LED_0_PIN 13 // GP13 = PIN 17
int main() {
// Inicializa el sistema estándar (UART, etc.)
stdio_init_all();
// Configura el pin como salida
gpio_init(LED_0_PIN);
gpio_set_dir(LED_0_PIN, GPIO_OUT);
// Enciende el LED (nivel alto)
gpio_put(LED_0_PIN, 1);
// Bucle infinito para mantenerlo encendido
while (true) {
// El LED permanecerá encendido
sleep_ms(1000);
}
return 0;
}
En el siguiente ejemplo, el LED 0 está siempre ON y el LED 1 parpadea en intervalos de 500 ms.
#include <stdio.h>
#include "pico/stdlib.h"
#define LED_0_PIN 13 // GP13 = PIN 17
#define LED_1_PIN 14 // GP14 = PIN 19
int main() {
// Inicializa el sistema estándar (UART, etc.)
stdio_init_all();
// Configurar los pines como salidas
gpio_init(LED_0_PIN);
gpio_set_dir(LED_0_PIN, GPIO_OUT);
gpio_init(LED_1_PIN);
gpio_set_dir(LED_1_PIN, GPIO_OUT);
// LED 0 encendido
gpio_put(LED_0_PIN, 1);
// LED 1 parpadeando
while (true) {
gpio_put(LED_1_PIN, 1); // Encendido
sleep_ms(500);
gpio_put(LED_1_PIN, 0); // Apagado
sleep_ms(500);
}
return 0;
}
En este ejemplo el LED 0 está siempre ON, el LED 1 parpadea en intervalos de 500 ms y el LED 2 lo hace en intervalos de 250 ms.
#include <stdio.h>
#include "pico/stdlib.h"
#define LED_0_PIN 13 // GP13 = PIN 17
#define LED_1_PIN 14 // GP14 = PIN 19
#define LED_2_PIN 15 // GP15 = PIN 20
int main() {
// Inicializa el sistema estándar (UART, etc.)
stdio_init_all();
// Configurar los pines como salidas
gpio_init(LED_0_PIN);
gpio_set_dir(LED_0_PIN, GPIO_OUT);
gpio_init(LED_1_PIN);
gpio_set_dir(LED_1_PIN, GPIO_OUT);
gpio_init(LED_2_PIN);
gpio_set_dir(LED_2_PIN, GPIO_OUT);
// LED 0 encendido fijo
gpio_put(LED_0_PIN, 1);
// Estados de parpadeo iniciales
bool estado_led1 = false;
bool estado_led2 = false;
// Temporizadores para cada LED
absolute_time_t tiempo_led1 = make_timeout_time_ms(500);
absolute_time_t tiempo_led2 = make_timeout_time_ms(250);
// Bucle principal
while (true) {
// Comprueba si ha pasado el tiempo del LED 1 (500 ms)
if (absolute_time_diff_us(get_absolute_time(), tiempo_led1) <= 0) {
estado_led1 = !estado_led1;
gpio_put(LED_1_PIN, estado_led1);
tiempo_led1 = make_timeout_time_ms(500);
}
// Comprueba si ha pasado el tiempo del LED 2 (250 ms)
if (absolute_time_diff_us(get_absolute_time(), tiempo_led2) <= 0) {
estado_led2 = !estado_led2;
gpio_put(LED_2_PIN, estado_led2);
tiempo_led2 = make_timeout_time_ms(250);
}
// Pequeña espera para evitar saturar la CPU
sleep_ms(10);
}
return 0;
}
Por último haremos que los tres ledes parpadeen a distintas frecuencias: LED 0 = 100 ms; LED 1 = 500 ms y el LED 2 = 250 ms.
#include <stdio.h>
#include "pico/stdlib.h"
#define LED_0_PIN 13 // GP13 = PIN 17
#define LED_1_PIN 14 // GP14 = PIN 19
#define LED_2_PIN 15 // GP15 = PIN 21
int main() {
stdio_init_all();
// Configurar los tres pines como salidas
gpio_init(LED_0_PIN);
gpio_set_dir(LED_0_PIN, GPIO_OUT);
gpio_init(LED_1_PIN);
gpio_set_dir(LED_1_PIN, GPIO_OUT);
gpio_init(LED_2_PIN);
gpio_set_dir(LED_2_PIN, GPIO_OUT);
// Variables de estado
bool estado_led0 = false;
bool estado_led1 = false;
bool estado_led2 = false;
// Temporizadores iniciales
absolute_time_t tiempo_led0 = make_timeout_time_ms(100);
absolute_time_t tiempo_led1 = make_timeout_time_ms(500);
absolute_time_t tiempo_led2 = make_timeout_time_ms(250);
while (true) {
absolute_time_t ahora = get_absolute_time();
// LED 0 → 100 ms
if (absolute_time_diff_us(ahora, tiempo_led0) <= 0) {
estado_led0 = !estado_led0;
gpio_put(LED_0_PIN, estado_led0);
tiempo_led0 = make_timeout_time_ms(100);
}
// LED 1 → 500 ms
if (absolute_time_diff_us(ahora, tiempo_led1) <= 0) {
estado_led1 = !estado_led1;
gpio_put(LED_1_PIN, estado_led1);
tiempo_led1 = make_timeout_time_ms(500);
}
// LED 2 → 250 ms
if (absolute_time_diff_us(ahora, tiempo_led2) <= 0) {
estado_led2 = !estado_led2;
gpio_put(LED_2_PIN, estado_led2);
tiempo_led2 = make_timeout_time_ms(250);
}
sleep_ms(10); // Evita saturar la CPU
}
return 0;
}
¡Hasta la próxima!