Helper I2C/SPI Classes and Functions

Summary

lbutils.helpers

Helper code and utilities, mostly helping to automate common tasks and replace boiler-plate code. Many of the classes and utilities in this collection are aimed at supporting the Leeds Beckett micro-controller development board, but may also be helpful for bare Pico H/W support.

Tested Implementations

This version is written for MicroPython 3.4, and has been tested on:

  • Raspberry Pi Pico H/W

lbutils.helpers.i2c

Functions and attributes for setting up, scanning and manipulating the I2C busses.

Classes for direct manipulation of the I2C bus can be found in the standard MicroPython library machine.I2C, and the classes for the I2C 'pmods' in lbutils.pmods. This library contains only helper functions, and attributes with the defaults for the Leeds Beckett micro-controller development board.

Attributes

I2C_SCL_PIN_DEFAULT module-attribute

I2C_SCL_PIN_DEFAULT = 17

Define the pin used for the I2C clock line, SCL, when scanning for I2C devices.

This default reflects the pin layout of the Leeds Beckett micro-controller development board.

I2C_SDA_PIN_DEFAULT module-attribute

I2C_SDA_PIN_DEFAULT = 16

Define the pin used for the I2C data line, SDA, when scanning for I2C devices.

This default reflects the pin layout of the Leeds Beckett micro-controller development board.

Functions

scan_i2c_bus

scan_i2c_bus(
    i2c_controller: int = 0,
    sda_pin: int = I2C_SDA_PIN_DEFAULT,
    scl_pin: int = I2C_SCL_PIN_DEFAULT,
) -> None

Scan for I2C devices on the listed bus, printing out the found device addresses to the console.

Example

The defaults are chosen to reflect the normal set-up of the Leeds Beckett micro-controller board, and so the scan_i2c_bus function can usually be called simply as

import lbutils.helpers.i2c

scan_i2c_bus()

The list of addresses is reported directly on the console, for instance as

Found 1 I2C devices:
    at address  0x1d

Note

The default pin numbers used for the scan_i2c_bus() function will only work with I2C the first controller, '0' (also the default). If you are using the second I2C controller, '1', then you will also need to change the pin numbers for the SDA and SCL pins, and cannot use the default values.

Parameters:

  • i2c_controller (int) –

    The I2C controller to use for message passing. Defaults to controller '0'.

  • sda_pin (int) –

    The (GPIO) pin number used for the I2C SDA data line. Defaults to the I2C pin used by the Leeds Beckett micro-controller development board.

  • scl_pin (int) –

    The (GPIO) pin number used for the I2C SCL clocl line. Defaults to the I2C pin used by the Leeds Beckett micro-controller development board.

Source code in lbutils/helpers/i2c.py
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def scan_i2c_bus(
    i2c_controller: int = 0,
    sda_pin: int = I2C_SDA_PIN_DEFAULT,
    scl_pin: int = I2C_SCL_PIN_DEFAULT,
) -> None:
    """Scan for I2C devices on the listed bus, printing out the found device
    addresses to the console.

    Example
    -------

    The defaults are chosen to reflect the normal set-up of the Leeds Beckett
    micro-controller board, and so the `scan_i2c_bus` function can usually be called
    simply as

    ````python
    import lbutils.helpers.i2c

    scan_i2c_bus()
    ````

    The list of addresses is reported directly on the console, for instance as

    ````
    Found 1 I2C devices:
        at address  0x1d
    ````

    !!! note

        The default pin numbers used for the `scan_i2c_bus()` function will
        _only_ work with I2C the first controller, '`0`' (also the default). If you are
        using the second I2C controller, '`1`', then you _will_ also need to change the
        pin numbers for the `SDA` and `SCL` pins, and cannot use the default values.

    Parameters
    ----------

    i2c_controller: int
        The I2C controller to use for message passing. Defaults to controller
        '`0`'.
    sda_pin: int
        The (GPIO) pin number used for the I2C SDA data line. Defaults to the
        I2C pin used by the Leeds Beckett micro-controller development board.
    scl_pin: int
        The (GPIO) pin number used for the I2C SCL clocl line. Defaults to the
        I2C pin used by the Leeds Beckett micro-controller development board.
    """

    i2c = I2C(i2c_controller, sda=Pin(sda_pin), scl=Pin(scl_pin), freq=400000)

    devices = i2c.scan()

    if len(devices) != 0:
        print(f"Found {len(devices)} I2C devices:")
        for device in devices:
            print("... at address ", hex(device))
    else:
        print("No I2C devices found")