This is the v1.22.0 version of the MicroPython documentation. The latest development version of this page may be more current.

2. Using peripherals

For quick help information, please enter:

help()

You can access RA MCU鈥檚 peripherals using MicroPython modules. To list supported modules, please enter:

help('modules')

Especially machine module and class machine.Pin are very important for using peripherals.

Using 鈥渇rom machine import Pin鈥, Pin name is available corresponding to the RA MCU鈥檚 pin name which are Pin.cpu.P000 and 鈥楶000鈥. In addition, you can use 鈥楲ED1鈥, 鈥楲ED2鈥, 鈥楽W1鈥, and 鈥楽W2鈥 name if the board has these LEDs and switches.

2.1. LED blinking

As simple example, you can enter following program to blink LED1. Please enter key 4 times after the input of last time.sleep(1).

import time
from machine import Pin
led1 = Pin('LED1')
print(led1)
while True:
    led1.on()
    time.sleep(1)
    led1.off()
    time.sleep(1)

You can see the LED1 blinking per 1 second.

If you want to stop the program, please enter CTRL-C.

Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
KeyboardInterrupt:

This message is displayed, and the program stops. The message means the program was interrupted at line 5 鈥渨hile鈥 statement.

Using print(led1), you can confirm that LED1 is assigned to Pin.cpu.P106 on the board.:

Pin(Pin.cpu.P106, mode=Pin.OUT, pull=Pin.PULL_NONE, drive=Pin.LOW_POWER)

So you can get the same result if Pin(Pin.cpu.P106) is specified instead of Pin(鈥楲ED1鈥).

import time
from machine import Pin
led1 = Pin(Pin.cpu.P106)
print(led1)
while True:
    led1.on()
    time.sleep(1)
    led1.off()
    time.sleep(1)