1/43
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
from machine import Pin
Imports the Pin class from the machine module to control GPIO pins.
Pin(pin_number, mode, [pull])
Creates a Pin object to control a physical GPIO pin. (pin_number sets which pin, mode sets input/output, pull adds optional resistor)
Pin.OUT
Configures a pin as an output (LEDs and Relays)
Pin.IN
Configures a pin as an input (buttons and sensors)
pin.PULL_UP
Activates an internal pull-up resistor to keep the input pin high until pulled low.
Pin.PULL_DOWN
Activates an internal pull-down resistor to keep the input pin low until pulled high.
pin.value([x])
Reads or writes the pin’s value (x=1 turns on/output high, x=0 turns off/output low, no x reads pin state).
pin.on()
Sets the pin’s output to HIGH (same as pin.value(1)).
pin.off()
Sets the pin’s output to LOW (same as pin.value(0)).
pin.toggle()
Toggles the output state of the pin (on becomes off, off becomes on).
from machine import PWM
Imports the PWM class to generate pulse-width modulation signals (for brightness or speed control).
PWM(pin)
Creates a PWM object on the specified pin for analog-style control.
pwm.freq(frequency)
Sets the PWM signal frequency (in hertz).
pwm.duty_u16(value)
Sets the PWM duty cycle from 0–65535 (controls brightness or motor speed).
pwm.deinit()
Stops PWM output and releases the pin.
from machine import ADC
Imports the ADC class to read analog signals (like sensors returning variable voltages).
ADC(pin)
Creates an ADC object connected to a specific analog-capable pin.
adc.read_u16()
Reads the analog voltage as a value from 0–65535.
from machine import I2C
Imports the I2C class for communication with I2C devices (like sensors or displays).
I2C(id, scl, sda, freq)
Creates an I2C object for communication (id = bus number, scl/sda = pin objects, freq = frequency).
i2c.scan()
Scans the I2C bus and returns a list of connected device addresses.
i2c.readfrom(addr, nbytes)
Reads nbytes of data from the device with the specified I2C address.
i2c.writeto(addr, data)
Writes data bytes to the device with the specified I2C address.
i2c.readfrom_mem(addr, memaddr, nbytes)
Reads nbytes from a specific register (memaddr) of the I2C device.
i2c.writeto_mem(addr, memaddr, data)
Writes data to a specific register (memaddr) of the I2C device.
from machine import UART
Imports the UART class for serial communication (used for Bluetooth, GPS, or other modules).
UART(id, baudrate)
Creates a UART serial connection with a specified port ID and communication speed.
uart.write(data)
Sends data over UART (string or bytes).
uart.read()
Reads available data from UART (returns bytes or None).
uart.any()
Returns True if data is waiting to be read from UART.
from machine import SPI
Imports the SPI class for high-speed communication with SPI devices.
SPI(id, baudrate, polarity, phase, sck, mosi, miso)
Creates an SPI connection with configurable pins and parameters.
spi.read(nbytes)
Reads nbytes from the SPI bus.
spi.write(data)
Writes bytes to the SPI bus.
spi.readinto(buffer)
Reads bytes directly into an existing buffer.
spi.writereadinto(write_buf, read_buf)
Writes and reads simultaneously (full-duplex communication).
spi.deinit()
Disables the SPI interface and frees resources.
from machine import Timer
Imports the Timer class for scheduling functions to run at intervals.
Timer(id)
Creates a Timer object (id identifies which hardware timer to use).
timer.init(period, mode, callback)
Initializes the timer (period in ms, mode=Timer.PERIODIC or Timer.ONE_SHOT, callback=function to run).
timer.deinit()
Stops the timer and releases it.
from machine import RTC
Imports the Real-Time Clock class for tracking date and time.
RTC()
Creates an RTC object.
rtc.datetime([datetimetuple])
Sets or reads the current date and time (tuple = (year, month, day, weekday, hours, minutes, seconds, subseconds)).