Skip to main content

Chapter 2 – Button & LED

Link Purpose
C Tutorial Chapter 2 – Button & LED Freenove’s official C version – Chapter 2 Button & LED
C Tutorial Chapter 2 – Button & LED Freenove’s official video description
Alire crate Alire crate containing the Ada code in this chapter
GNATdoc documentation for this chapter Automatically generated HTML documentation for the Ada code in this chapter

In Chapter 2 I add a push button to the mix and make the LED respond to it. Hardware-wise, we introduce a proper 3.3 V rail for the button pull-up (or pull-down – depending on wiring).

I’m aiming for a 5 V battery supply in future setups so I can power via battery and USB simultaneously without back-feeding current. A Schottky diode protects the Pico from reverse current, but I’m still double-checking the other direction.

I’ve also started packaging each chapter as an Alire crate – they’re moderated, so it might take a little while for them to appear, but they make fetching and building the examples much easier.

Both sketches worked first time, which was a nice change after some of the runtime experiments in Chapter 1!

sketch_02_1_button_and_led #

This is a fairly direct port of the first C example. The button is wired active-low (pressed = low), so I inverted the logic compared to the C version to make the if read more naturally.

I also added a short delay in the loop to avoid a tight busy-wait – better for power and responsiveness.

---
--  Switch LED sample from Chapter 2.1 (Freenove C Tutorial Chapter 2)
--
procedure Sketch_02_1_Button_And_Led with
   No_Return
is
   --  External LED on GP15
   LED    : RP.GPIO.GPIO_Point renames Pico.GP15;
   --  Button on GP13
   Button : RP.GPIO.GPIO_Point renames Pico.GP13;
begin
   LED.Configure (RP.GPIO.Output);
   Button.Configure (RP.GPIO.Input);

   loop
      --  Button pressed → low (false); released → high (true)
      if Button.Get then
         LED.Clear;   --  off when released
      else
         LED.Set;     --  on when pressed
      end if;

      delay 0.02;  --  gentle loop rate
   end loop;
end Sketch_02_1_Button_And_Led;

Button controlling external LED

sketch_02_2_table_lamp #

The second example turns the setup into a proper table lamp: press once to toggle the LED on/off, with simple software debounce and wait-for-release logic.

Ada’s built-in Toggle method saves us writing extra code. I added a not on the button read to match the active-low wiring.

Added a bit more contract style (No_Return) and kept the gentle delays.

pragma License (Modified_Gpl);
pragma Ada_2022;
pragma Extensions_Allowed (On);

with RP.GPIO;
with Pico;

---
--  Table Lamp sample from Chapter 2.2 (Freenove C Tutorial Chapter 2)
--
procedure Sketch_02_2_Table_Lamp with
   No_Return
is
   --  External LED on GP15
   LED    : RP.GPIO.GPIO_Point renames Pico.GP15;
   --  Button on GP13
   Button : RP.GPIO.GPIO_Point renames Pico.GP13;
begin
   LED.Configure (RP.GPIO.Output);
   Button.Configure (RP.GPIO.Input);

   loop
      --  Button pressed (low)
      if not Button.Get then
         --  Simple debounce
         delay 0.02;
         if not Button.Get then
            LED.Toggle;
         end if;

         --  Wait until button is released
         while not Button.Get loop
            delay 0.02;
         end loop;
      end if;

      delay 0.02;  --  avoid busy loop
   end loop;
end Sketch_02_2_Table_Lamp;

Table lamp toggle demo

That’s Chapter 2 done. It’s a small step up from plain blinking, but it introduces real input handling and debounce – essential for anything interactive.

Next chapter should bring something new. Admit it — you’ve always fancied building your own red Cylon eye scanner. I’ll get cracking on it soon!