Digital IO

This chapter introduces the digital input/output usage of the mPython Board’s I/O pins. Pins are how the mPython Board communicates with external devices connected to it. The mPython Board can expand its IO pins through an expansion board to connect, control or read other components or modules.

Attention

Except for P2 (digital input only), P3, P4 and P10, all other pins can use digital input and output modes. For more details, see mPython Board interface pin description.

Digital Input

First, let’s start with how to read digital input from a pin. The following uses the built-in button A on the mPython Board as a button input:

from mpython import *           # Import the mpython module

p5=MPythonPin(5,PinMode.IN)     # Instantiate MPythonPin, set button A pin (P5) to "PinMode.IN" mode

while True:
    value=p5.read_digital()      # Read the digital input of pin P5
    print("Button_a:%d" %value)   # Read value

Note

Now you can press button A and observe the readings for “pressed” and “not pressed”. Since button A’s circuit has a pull-up, it outputs high level when “not pressed” and low level when “pressed”.

from mpython import *
p5=MPythonPin(5,PinMode.IN)

Before use, make sure to import the mpython module first.

Instantiate the pin object and set the mode. Here we use the MPythonPin(pin, mode=PinMode.IN,pull=None) class. pin is the pin you want to access. If mode is not specified, it defaults to PinMode.IN. If pull is not specified, it defaults to None.

p5.read_digital()

Note

Use read_digital() to return the pin’s level value. High level (1), low level (0).

Digital Output

The following is a simple example of driving an external LED to blink:

from mpython import *           # Import the mpython module

p0=MPythonPin(0,PinMode.OUT)     # Instantiate MPythonPin, set P0 to "PinMode.OUT" mode

while True:
    p0.write_digital(1)          # Write high level to P0
    sleep(1)                     #  Delay
    p0.write_digital(0)          # Write low level to P0
    sleep(1)                     #  Delay

Materials and connections

The above requires a breadboard, 1 LED, an mPython expansion board, and jumper wires. Connect the LED’s anode to pin P0 of the mPython Board, and the LED’s cathode to GND of the mPython Board.

p0=MPythonPin(0,PinMode.OUT)

Note

MPythonPin instantiation. mode is set to PinMode.OUT digital output mode.

Write high/low level to pin P0:

p0.write_digital(1)
p0.write_digital(0)

Note

Use the write_digital(value) method to write high/low level to a pin. value is the level: “1” means high level, “0” means low level.

External Interrupts

What is an interrupt?

While a program is running, a situation arises that the CPU must handle immediately; the process in which the CPU temporarily suspends program execution to handle this new situation is called an interrupt. When needed, the CPU must pause what it is doing, handle something else, and then go back to the paused work after finishing.

When a level change occurs on an input pin, a hardware interrupt is triggered and the trigger executes the interrupt handler function. You can define a callback function to handle the interrupt response. Pin interrupts work the same as the mPython Board’s A and B button interrupts.

The following uses the built-in button A (P5 pin) on the mPython Board as an input interrupt; when button A is pressed, the buzzer sounds:

from mpython import *           # Import the mpython module
import music                    # Import the music module
p5=MPythonPin(5,PinMode.IN)     # Instantiate MPythonPin, set P5 to "PinMode.IN" mode

def BuzzOn(_):                    # Define the interrupt callback function
    music.play(music.BA_DING,wait=False)

p5.irq(trigger=Pin.IRQ_FALLING,handler=BuzzOn)            # Set the callback function for P5 pin interrupt

Hint

The effect is the same as using button_a.irq() for button interrupts; button_a’s interrupt also uses the Pin.irq method.

First instantiate MPythonPin and configure pin P5 as PinMode.IN:

p5=MPythonPin(5,PinMode.IN)

Define the callback function:

def BuzzOn(_):
    music.play(music.BA_DING,wait=False)

Note

The callback function must contain one parameter, otherwise it cannot be used. Above, BuzzOn() defines the callback function with parameter _; you can define this parameter however you like.

Finally, we need to tell the pin when to trigger and which function to call when the event is detected:

p5.irq(trigger=Pin.IRQ_FALLING,handler=BuzzOn)

Note

We set P5 to trigger only on the falling edge Pin.IRQ_FALLING (when it changes from high to low level). Set the callback function handler=”your defined interrupt handler callback function”. For more detailed trigger modes, see MPythonPin.irq.