Arduino MicroPython Blink LED

This tutorial teaches you how to control an LED with an Arduino using MicroPython. It explains how to write MicroPython code to turn the LED on and off and make it blink.

Arduino MicroPython Blink LED

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Mega/Giga
1×(Recommended) Enclosure For Arduino Giga
1×(Recommended) Power Splitter For Arduino Giga

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of LED

Pinout

LEDs have two legs.

  • Attach the negative (-) pin to the ground (0V)
  • The positive (+) pin manages the LED's status
LED Pinout

How It Works

After connecting the negative side (cathode) of the LED to the ground (GND):

  • If you connect the ground (GND) to the long leg (anode) of the LED, the LED will not turn on.
  • If you connect the power source (VCC) to the long leg (anode) of the LED, the LED will turn on.
How LED Works

You can also adjust the brightness of the LED by applying a PWM signal to the positive terminal (anode). How to change the LED light intensity using PWM is explained in detail in this tutorial.

※ NOTE THAT:

LEDs usually need a resistor to function correctly. You can connect the resistor to either the positive side (anode) that connects to the power source (VCC) or the negative side (cathode) that connects to the ground (GND). The required resistor value depends on the specific requirements of the LED. Some LEDs come with a built-in resistor, and if yours does, an additional resistor is generally not needed.

Wiring Diagram

The wiring diagram between Arduino MicroPython LED

This image is created using Fritzing. Click to enlarge image

How To Program

  • Configure Arduino pin GPIO18 to a digital output.
led = Pin(18, Pin.OUT)
  • Turn off the LED.
led.value(0) # Turn the LED off
  • Turn on the LED.
led.value(1) # Turn the LED on

Arduino MicroPython Code

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-blink-led """ from machine import Pin from time import sleep led = Pin('D2', Pin.OUT) while True: led.value(1) # Turn the LED on sleep(1) # Wait for 1 second led.value(0) # Turn the LED off sleep(1) # Wait for 1 second

Detailed Instructions

Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the LED to the Arduino as shown in the diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • Look at the LED to see if it's working.

Code Explanation

You can find the explanation in the comments section of the Arduino MicroPython code mentioned above.

※ NOTE THAT:

The code you're looking at uses sleep(), which prevents the Arduino from doing other things at the same time. If your project has to manage several tasks, avoid using this function because it stops the Arduino. Instead, think about using a method that keeps the Arduino active.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!