EnglishFrançais

Blink an LED

The GPIO "hello world": flash an LED on and off.

Wiring

GPIO17 ──▶│── 330Ω ──┐
         LED         │
                    GND

LED anode → GPIO17, cathode → 330 ΩGND.

Code

namespace Demo

import Amalgame.Hardware
import Amalgame.DateTime

public class Program {
    public static void Main(string[] args) {
        let led: int = 17

        if (!Gpio.PinMode(led, PinMode.Output)) {
            Console.WriteLine("could not open GPIO17 — check permissions")
            return
        }

        var i: int = 0
        while (i < 20) {
            Gpio.Toggle(led)               // flip the output
            DateTime.SleepMillis(500)      // ...every half second
            i = i + 1
        }

        Gpio.Close()                       // release the line
    }
}

Run

amc package add datetime     # once — for DateTime.SleepMillis
amc build main.am -o blink
./blink

What's happening

Next: read a button →