Digital IO
This chapter introduces how to use the digital input and output of the mPython Board’s I/O pins. Pins are the way the mPython Board communicates with the external devices connected to it. The mPython Board can expand the IO pins through the 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 detailed information, please refer to mPython Board interface pin description .
Digital Input
First, let’s start with how to read the digital input of a pin. The following uses the built-in button a of the mPython Board as a button input:
from mpython import * # Import the mpython module
p5=MPythonPin(5,PinMode.IN) # Instantiate MPythonPin, set the button a pin (P5) to "PinMode.IN" mode
while True:
value=p5.read_digital() # Read the digital input of the P5 pin
print("Button_a:%d" %value) # Read value
Note
At this point, you can press the button a key to see the read values for “pressed” and “not pressed”. Since the button a circuit has a pull-up, the output is high level when “not pressed”, and low level when “pressed”.
from mpython import *
p5=MPythonPin(5,PinMode.IN)
Before use, be sure to import the mpython module first, so that it can be used.
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 level value of the pin. 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 high level to P0
sleep(1) # Delay
Materials and Connections
The above requires a breadboard, 1 LED, an mPython expansion board, and jumper wires. The positive pole of the LED is connected to the P0 pin of the mPython Board, and the negative pole of the LED is connected to the GND of the mPython Board.
p0=MPythonPin(0,PinMode.OUT)
Note
MPythonPin instantiation. mode is set to PinMode.OUT digital output mode.
Write high and low levels to the P0 pin:
p0.write_digital(1)
p0.write_digital(0)
Note
Use the write_digital(value) method to write high and low levels to the pin. Here value is the level value, “1” represents high level, and “0” represents low level.
External Interrupt
What is an interrupt?
During program execution, when a situation arises that must be handled immediately by the CPU, the CPU temporarily suspends the execution of the program and turns to handle this new situation. This process is called an interrupt. When needed, the CPU must pause what it is doing now, handle other things, and then go back to execute the paused task after finishing.
When the input pin undergoes a level change, a hardware interrupt is triggered, and the trigger executes the interrupt handler function. You can define a callback function to do the interrupt response work. The use of pin interrupts is essentially the same as the a and b button interrupts of the mPython Board.
The following uses the built-in button a of the mPython Board (P5 pin) as an input interrupt. When button A is pressed, the buzzer makes a sound:
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 the P5 pin interrupt
Hint
The effect is the same as when using the button_a.irq() button interrupt. The button_a interrupt also uses the Pin.irq method.
We first instantiate MPythonPin and configure the P5 pin 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. In the above BuzzOn() callback function definition, the parameter is _, and you can define this parameter arbitrarily.
Finally, we need to tell the pin when to trigger, and the function to call when an 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 level to low level). Set the callback function
handler=”the callback function you define for interrupt handling”. For more detailed trigger methods, please refer to MPythonPin.irq .