MicroPython for ODROID-GO - Blue LED and PWM

We will learn how to control the blue status LED on the board and how to adjust the PWM value to give a breathing effect to the LED with MicroPython.

Let's make the LED blink continuously.

You're able to control the LED very easily.

First, we have to import necessary modules machine, micropython, time.
We don't need all functions of the modules, so we could select specific modules we need when importing.

from machine import Pin
from micropython import const
 
import time

The pin number for the LED is 2. Define that with micropython.const() function. This function works like Preprocessor in C/C++.

from machine import Pin
from micropython import const
 
import time
 
LED_PIN = const(2)

And create a Pin object using this LED pin, like Pin(LED_PIN).
When initializing a Pin object, you can specify the direction of the GPIO pin.

  • Pin.IN
  • Pin.OUT
from machine import Pin
from micropython import const
 
import time
 
LED_PIN = const(2)
led = Pin(LED_PIN, Pin.OUT)

Add infinite while loop for blinking the LED.

A Pin object has value() function to read/write a signal. Use this function.
And also use time.sleep() function to delay the blinking speed.

from machine import Pin
from micropython import const
 
import time
 
LED_PIN = const(2)
led = Pin(LED_PIN, Pin.OUT)
 
while True:
    led.value(1)
    time.sleep(0.5)
    led.value(0)
    time.sleep(0.5)

Save and overwrite this code as boot.py in the ODROID-GO module installation directory.

By adjusting the analog output value, known as PWM, we can make the LED show a breathing effect.

MicroPython gives us wrappers that help control the GPIO pins as well as the PWM features.
Generally, these PWM functions are provided as machine.PWM class. So we should import PWM class.

from machine import Pin, PWM
from micropython import const
 
import time

Initialize the LED pin as an object.

from machine import Pin, PWM
from micropython import const
 
import time
 
LED_PIN = const(2)
led = Pin(LED_PIN, Pin.OUT)

And initialize a PWM object like PWM(led).
You also can specify its initial frequency and duty value.
Note that its frequency ranges only between 0 to 1000, and duty ranges only between 0 to 1023.

from machine import Pin, PWM
from micropython import const
 
import time
 
LED_PIN = const(2)
led = Pin(LED_PIN, Pin.OUT)
pwm1 = PWM(led, freq=1000, duty=0)

Add infinite while loop for breathing the LED.
By using range(from, to, step) function from Python, you can easily code with PWM.

You can change its frequency and duty value using,

  • PWM.freq()
  • PWM.duty()
from machine import Pin, PWM
from micropython import const
 
import time
 
LED_PIN = const(2)
led = Pin(LED_PIN, Pin.OUT)
pwm1 = PWM(led, freq=1000, duty=0)
 
while True:
    for dValue in range(0, 1023, 100):
        pwm1.duty(dValue)
        time.sleep(0.1)
 
    for dValue in range(1023, 0, -100):
        pwm1.duty(dValue)
        time.sleep(0.1)

Save and overwrite this code as boot.py in the ODROID-GO module installation directory.

  • To execute this module properly, make sure you've uploaded ODROID-GO module. And you have to upload the written file called boot.py using rshell or ampy.
    • If you uploaded properly, MicroPython will execute boot.py when the device boots automatically.
    • Please refer to setup guide to further information: Install the ODROID-GO MicroPython module.
    • Or you also can do them in REPL. Write the codes line by line in order.

Upload the boot.py file using rshell or ampy, enter to REPL prompt, and restart ODROID-GO.

If the procedure goes well, you can see the blinking/breathing LED on your device.

The complete example is available in following path:

  • odroid_go/examples/led/led.py
  • odroid_go/examples/led_pwm/led_pwm.py

Copy and paste to try the example.