Ada_Drivers_Library/examples/MicroBit/digital_out
Fabien Chouteau f67115ada7
Various Microbit examples fixes and improvements (#367)
* micro:bit examples digital_out: fix resistor value

* micro:bit examples digital_in: fix resistor value

* Update README.md

* Typo

* Typo

* Update README.md

* Update README.md

* Update README.md (#366)
2020-09-18 13:16:44 +02:00
..
src MicroBit: Add a documented examples 2018-07-13 19:44:34 +02:00
README.md Various Microbit examples fixes and improvements (#367) 2020-09-18 13:16:44 +02:00
digital_out.gpr Use startup-gen on MicroBit board support 2019-08-22 19:53:17 +02:00

README.md

Digital Output Example

In this example we will see how to control a pin as digital output by flashing an external LED. This means that the voltage on the pin connected to the LED will be either 0 volts or 3.3 volts.

Wiring Diagram

Wiring the LED directly from the output pin to ground will make it burn, so we have to add a resistor to limit the flow of current.

Extra hardware:

  • A breadboard
  • An LED
  • A 470 ohm resistor

Wiring:

Code

To control the IO pin we are going to use the procedure Set of the package MicroBit.IOs.

   procedure Set (Pin : Pin_Id; Value : Boolean)
     with Pre => Supports (Pin, Digital);

Arguments:

  • Pin : The id of the pin that we want to control as digital output
  • Value : A boolean that says if we want the pin to be high (True) or low (False)

Precondition:

The procedure Set has a precondition that the pin must support digital IO.

We also use the procedure Delay_Ms of the package MicroBit.Time to stop the program for a short amount of time.

Here is the code:

with MicroBit.IOs;
with MicroBit.Time;

procedure Main is
begin

   --  Loop forever
   loop
      --  Turn on the LED connected to pin 0
      MicroBit.IOs.Set (0, True);

      --  Wait 500 milliseconds
      MicroBit.Time.Delay_Ms (500);

      --  Turn off the LED connected to pin 0
      MicroBit.IOs.Set (0, False);

      --  Wait 500 milliseconds
      MicroBit.Time.Delay_Ms (500);
   end loop;
end Main;