External LED
Instead of using an on-board LED, you can also connect a LED to an external pin. Just like you see in the example on the right. It connects an external LED (a red one in this case) to the pin A1 on the board.
The code is almost identical to the previous example. The only difference is this line:
led := machine.A1
We’ve replaced the led
variable: instead of the on-board LED we’re now using the external pin A1.
If you want to try this on a development board, make sure you use the correct wiring:
- Connect the LED cathode (short lead with flat side) to a resistor of around 220Ω-330Ω. Use at least 150Ω to protect the LED, and use at most 1000Ω to still be able to see the LED (higher values will make the LED very dim).
- Connect the other end of the resistor to ground.
- Connect the LED anode to pin A1 on the board. See the silkscreen on the board or the graphic in the simulator to know which pin this is.
Warning: the resistor is not optional, not using the resistor can destroy the LED and/or your board. The LED might even explode!
The simulation doesn’t need this resistor because the virtual LED can’t burn out. This simplifies the demo, but remember that the real world is messy and LEDs need a resistor. (There are technical reasons for this, read this answer for more details).