machine — Hardware-related Functions
The machine module contains specific functions related to the hardware on a particular board. Most functions in this module allow direct and unrestricted access to and control of hardware blocks on the system (such as CPU, timers, buses, etc.).
Improper use may lead to failures, lockups, board crashes, and in extreme cases hardware damage.
Note about callbacks used by functions and class methods in the machine module: all these callbacks should be treated as executing in an interrupt context.
This is true for both physical devices with ID >= 0 and “virtual” devices with negative IDs (e.g. -1) (these “virtual” devices are still thin shims on top of real hardware and actual hardware interrupts).
See Interrupt Handlers
- class Pin – control I/O pins
- Class ADC – Analog-to-Digital Conversion
- Class TouchPad – Touch Sensor
- Class PWM – Pulse Width Modulation
- class UART – duplex serial communication bus
- class I2C – a two-wire serial protocol
- Class SPI – Serial Peripheral Interface Bus Protocol (Master)
- class Timer – control hardware timers
- class RTC – real time clock
- class WDT – watchdog timer
Other Functions
- machine.unique_id()
Returns a byte string with a unique identifier of the board/SoC. It will vary from a board/SoC instance to another, if underlying hardware allows. Length varies by hardware (use a substring of a full value if you expect a short ID). In some MicroPython ports, the ID corresponds to the network MAC address.
>>> machine.unique_id() b'\xccP\xe3\x90\xeb\xd4'
- machine.time_pulse_us(pin, pulse_level, timeout_us=1000000)
Time a pulse on the given pin, and return the duration of the pulse in microseconds.
pulse_level=1 tests for high level duration, pulse_level=0 tests for low level duration. When the set level and the current pulse level are inconsistent, timing starts only when the input level matches the set level. If the set level matches the current pulse level, timing starts immediately. When the pin level remains opposite to the set level, it waits until timeout and returns -2. When the pin level remains the same as the set level, it also waits until timeout and returns -1.timeout_usis the timeout duration.
- machine.rng()
Returns a 24-bit software generated random number.
Constants
IRQ Wake Values
- machine.SLEEP
2
- machine.DEEPSLEEP
4