Python General functions

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/43

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 4:15 AM on 9/3/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

44 Terms

1
New cards

from machine import Pin

Imports the Pin class from the machine module to control GPIO pins.

2
New cards

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)

3
New cards

Pin.OUT

Configures a pin as an output (LEDs and Relays)

4
New cards

Pin.IN

Configures a pin as an input (buttons and sensors)

5
New cards

pin.PULL_UP

Activates an internal pull-up resistor to keep the input pin high until pulled low.

6
New cards

Pin.PULL_DOWN

Activates an internal pull-down resistor to keep the input pin low until pulled high.

7
New cards

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).

8
New cards

pin.on()

Sets the pin’s output to HIGH (same as pin.value(1)).

9
New cards

pin.off()

Sets the pin’s output to LOW (same as pin.value(0)).

10
New cards

pin.toggle()

Toggles the output state of the pin (on becomes off, off becomes on).

11
New cards

from machine import PWM

Imports the PWM class to generate pulse-width modulation signals (for brightness or speed control).

12
New cards

PWM(pin)

Creates a PWM object on the specified pin for analog-style control.

13
New cards

pwm.freq(frequency)

Sets the PWM signal frequency (in hertz).

14
New cards

pwm.duty_u16(value)

Sets the PWM duty cycle from 0–65535 (controls brightness or motor speed).

15
New cards

pwm.deinit()

Stops PWM output and releases the pin.

16
New cards

from machine import ADC

Imports the ADC class to read analog signals (like sensors returning variable voltages).

17
New cards

ADC(pin)

Creates an ADC object connected to a specific analog-capable pin.

18
New cards

adc.read_u16()

Reads the analog voltage as a value from 0–65535.

19
New cards

from machine import I2C

Imports the I2C class for communication with I2C devices (like sensors or displays).

20
New cards

I2C(id, scl, sda, freq)

Creates an I2C object for communication (id = bus number, scl/sda = pin objects, freq = frequency).

21
New cards

i2c.scan()

Scans the I2C bus and returns a list of connected device addresses.

22
New cards

i2c.readfrom(addr, nbytes)

Reads nbytes of data from the device with the specified I2C address.

23
New cards

i2c.writeto(addr, data)

Writes data bytes to the device with the specified I2C address.

24
New cards

i2c.readfrom_mem(addr, memaddr, nbytes)

Reads nbytes from a specific register (memaddr) of the I2C device.

25
New cards

i2c.writeto_mem(addr, memaddr, data)

Writes data to a specific register (memaddr) of the I2C device.

26
New cards

from machine import UART

Imports the UART class for serial communication (used for Bluetooth, GPS, or other modules).

27
New cards

UART(id, baudrate)

Creates a UART serial connection with a specified port ID and communication speed.

28
New cards

uart.write(data)

Sends data over UART (string or bytes).

29
New cards

uart.read()

Reads available data from UART (returns bytes or None).

30
New cards

uart.any()

Returns True if data is waiting to be read from UART.

31
New cards

from machine import SPI

Imports the SPI class for high-speed communication with SPI devices.

32
New cards

SPI(id, baudrate, polarity, phase, sck, mosi, miso)

Creates an SPI connection with configurable pins and parameters.

33
New cards

spi.read(nbytes)

Reads nbytes from the SPI bus.

34
New cards

spi.write(data)

Writes bytes to the SPI bus.

35
New cards

spi.readinto(buffer)

Reads bytes directly into an existing buffer.

36
New cards

spi.writereadinto(write_buf, read_buf)

Writes and reads simultaneously (full-duplex communication).

37
New cards

spi.deinit()

Disables the SPI interface and frees resources.

38
New cards

from machine import Timer

Imports the Timer class for scheduling functions to run at intervals.

39
New cards

Timer(id)

Creates a Timer object (id identifies which hardware timer to use).

40
New cards

timer.init(period, mode, callback)

Initializes the timer (period in ms, mode=Timer.PERIODIC or Timer.ONE_SHOT, callback=function to run).

41
New cards

timer.deinit()

Stops the timer and releases it.

42
New cards

from machine import RTC

Imports the Real-Time Clock class for tracking date and time.

43
New cards

RTC()

Creates an RTC object.

44
New cards

rtc.datetime([datetimetuple])

Sets or reads the current date and time (tuple = (year, month, day, weekday, hours, minutes, seconds, subseconds)).