Arduino MicroPython LED Module

Need a clear light on your Arduino Giga R1 WiFi project that shows "running", "error" or "done"? A 10mm LED module is a quick way to get one. This guide uses MicroPython and covers:

https://newbiely.com/images/cover/arduino-led-module-micropython.jpg

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×LED Module
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×Recommended: Sensors/Servo Expansion Shield for Arduino 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 kits:

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 .

Buy Note: Use the LED Module for easier wiring. It includes an integrated resistor.

Overview of LED Module and Pinout

Think of the LED module as an LED that comes with its own helpers. On a small 20mm x 20mm black board, there is a big 10mm diffused LED, a 330Ω resistor that limits the LED current, a 1KΩ resistor on the IN line, and a tiny transistor that works as a switch. The power for the LED comes from the VCC pin. The IN pin only tells the transistor to open or close. The DIYables pack has five of these boards: red, yellow, blue, green and white.

LED Module Pinout
image source: diyables.io

The module has a 3-pin right-angle header. The pins are labeled in this order:

  • GND pin: goes to GND (0V) of the Arduino.
  • VCC pin: goes to 5V or 3.3V of the Arduino. The module works from 3.3V to 5V DC.
  • IN pin: the control pin. Connect it to a digital pin of the Arduino. Pick a PWM pin if you want to change the brightness.

How the IN pin works:

  • IN = HIGH (value(1)): the transistor turns on and the LED lights up.
  • IN = LOW (value(0)): the LED is off.
  • A PWM signal on IN: the LED is on for part of each very short cycle, so it looks dimmer. A bigger duty value makes it brighter.

The pins of the Giga R1 WiFi work at 3.3V. With this module, that is not a problem. The pin only drives the small transistor, so it gives very little current, and the LED still gets its full current from VCC.

Wiring Diagram

The module plugs in with just three jumper wires. We use pin D2 because it can make PWM, so the same wiring works for both examples on this page.

The wiring diagram between Arduino MicroPython LED Module

This image is created using Fritzing. Click to enlarge image

LED Module Arduino Giga R1 WiFi
GND GND
VCC 5V (3.3V also works, the light is a bit dimmer)
IN D2

How To Program LED Module With MicroPython

No library is needed. Everything comes from the machine module that is built into MicroPython.

  • Make pin D2 a digital output:
led_module = Pin('D2', Pin.OUT)
  • Turn the LED module on and off:
led_module.value(1) ## ON led_module.value(0) ## OFF
  • For brightness, use the same pin as a PWM output. duty_u16() takes a number from 0 (off) to 65535 (full brightness):
led_module = PWM(Pin('D2')) led_module.freq(1000) led_module.duty_u16(16000) ## about 25% brightness

Arduino MicroPython Code - Fade LED Module

Now let's change the brightness. The code raises the PWM duty step by step until the LED is fully bright, then lowers it back to zero.

""" 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-led-module """ from machine import Pin, PWM import time LED_MODULE_PIN = 'D2' # The Arduino Giga WiFi pin D2 (a PWM pin) connected to the IN pin of the LED module # Create a PWM output on the IN pin led_module = PWM(Pin(LED_MODULE_PIN)) led_module.freq(1000) # 1 kHz is fast enough, so the eye sees no flicker STEP = 1300 # How much the duty changes each time (0-65535 range) DELAY = 0.02 # Wait 20 ms between steps while True: print("Fading in...") for duty in range(0, 65536, STEP): # From OFF to full brightness led_module.duty_u16(duty) time.sleep(DELAY) led_module.duty_u16(65535) # Make sure it is fully ON print("Full brightness") print("Fading out...") for duty in range(65535, -1, -STEP): # From full brightness to OFF led_module.duty_u16(duty) time.sleep(DELAY) led_module.duty_u16(0) # Make sure it is fully OFF print("LED is off") time.sleep(0.5)

Detailed Instructions

  • Keep the same wiring (D2 is a PWM pin).
  • Copy the code above and paste it into Thonny's editor.
  • Save it to the Arduino as main.py (replace the blink file).
  • Click the green Run button (or press F5).
  • The LED module slowly gets brighter, then slowly gets darker. The Shell shows each phase.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Fading in... Full brightness Fading out... LED is off Fading in... Full brightness Fading out... LED is off
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

To make the fade slower, raise DELAY or make STEP smaller. To limit the top brightness, stop the loop at a lower duty, for example 30000.

※ NOTE THAT:

Both examples use time.sleep(), so the Arduino does nothing else while it waits. For a project that must also read buttons or sensors at the same time, see blink without sleep.

LED Module or Bare LED?

If you have done the Blink LED or Fade LED tutorial, you used a bare LED. The MicroPython code is almost the same. The difference is in the wiring:

Bare LED LED Module
Resistor you add one (e.g. 220Ω) already on the board
Polarity find the long leg (anode) and short leg (cathode) pins are labeled GND, VCC, IN
Current from the Arduino pin all of the LED current very small (only drives the transistor)
Breadboard usually needed not needed, plug in jumper wires

So the module is a good choice when you want a fast, clean setup, for example a status light, a fault alarm, a traffic light model or a "heartbeat" light that shows your code is still running.

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!