:mod:`digitalio` ================ .. py:module:: digitalio .. autoapi-nested-parse:: Basic digital pin support The `digitalio` module contains classes to provide access to basic digital IO. All classes change hardware state and should be deinitialized when they are no longer needed if the program continues after use. To do so, either call :py:meth:`!deinit` or use a context manager. See :ref:`lifetime-and-contextmanagers` for more info. For example:: import digitalio from board import * pin = digitalio.DigitalInOut(D13) print(pin.value) This example will initialize the the device, read :py:data:`~digitalio.DigitalInOut.value` and then :py:meth:`~digitalio.DigitalInOut.deinit` the hardware. Here is blinky:: import digitalio from board import * import time led = digitalio.DigitalInOut(D13) led.direction = digitalio.Direction.OUTPUT while True: led.value = True time.sleep(0.1) led.value = False time.sleep(0.1) .. py:class:: DigitalInOut(pin: microcontroller.Pin) Digital input and output A DigitalInOut is used to digitally control I/O pins. For analog control of a pin, see the :py:class:`analogio.AnalogIn` and :py:class:`analogio.AnalogOut` classes. .. attribute:: direction :annotation: :Direction The direction of the pin. Setting this will use the defaults from the corresponding :py:meth:`switch_to_input` or :py:meth:`switch_to_output` method. If you want to set pull, value or drive mode prior to switching, then use those methods instead. .. attribute:: value :annotation: :Bool The digital logic level of the pin. .. attribute:: drive_mode :annotation: :DriveMode The pin drive mode. One of: - `digitalio.DriveMode.PUSH_PULL` - `digitalio.DriveMode.OPEN_DRAIN` .. attribute:: pull :annotation: :Optional[Pull] The pin pull direction. One of: - `digitalio.Pull.UP` - `digitalio.Pull.DOWN` - `None` :raises AttributeError: if `direction` is :py:data:`~digitalio.Direction.OUTPUT`. .. method:: deinit(self) Turn off the DigitalInOut and release the pin for other use. .. method:: __enter__(self) No-op used by Context Managers. .. method:: __exit__(self) Automatically deinitializes the hardware when exiting a context. See :ref:`lifetime-and-contextmanagers` for more info. .. method:: switch_to_output(self, value: bool = False, drive_mode: digitalio.DriveMode = digitalio.DriveMode.PUSH_PULL) Set the drive mode and value and then switch to writing out digital values. :param bool value: default value to set upon switching :param ~digitalio.DriveMode drive_mode: drive mode for the output .. method:: switch_to_input(self, pull: Pull = None) Set the pull and then switch to read in digital values. :param Pull pull: pull configuration for the input Example usage:: import digitalio import board switch = digitalio.DigitalInOut(board.SLIDE_SWITCH) switch.switch_to_input(pull=digitalio.Pull.UP) # Or, after switch_to_input switch.pull = digitalio.Pull.UP print(switch.value) .. py:class:: Direction Defines the direction of a digital pin .. attribute:: INPUT :annotation: :Any Read digital data in .. attribute:: OUTPUT :annotation: :Any Write digital data out .. py:class:: DriveMode Defines the drive mode of a digital pin .. attribute:: PUSH_PULL :annotation: :Any Output both high and low digital values .. attribute:: OPEN_DRAIN :annotation: :Any Output low digital values but go into high z for digital high. This is useful for i2c and other protocols that share a digital line. .. py:class:: Pull Defines the pull of a digital input pin .. attribute:: UP :annotation: :Any When the input line isn't being driven the pull up can pull the state of the line high so it reads as true. .. attribute:: DOWN :annotation: :Any When the input line isn't being driven the pull down can pull the state of the line low so it reads as false.