Categorygithub.com/googolgl/go-mcp23017
modulepackage
0.0.0-20210225115400-eb8b77d91034
Repository: https://github.com/googolgl/go-mcp23017.git
Documentation: pkg.go.dev

# README

MCP23017 16-Bit I/O Expander with Serial Interface Driver

GoDoc MIT License

MCP23017 is a popular controller among Arduino and Raspberry PI developers. The 16-Bit I/O Expander with Serial Interface Driver will drive up to 16 pins over I2C with only 2 pins. image

Here is a library written in Go programming language for Raspberry PI and counterparts.

Golang usage

package main

import (
	"log"
	"time"

	"github.com/googolgl/go-i2c"
	"github.com/googolgl/go-mcp23017"
)

func main() {
    // Create new connection to i2c-bus on 1 line with address 0x40.
    // Use i2cdetect utility to find device address over the i2c-bus
    i2c, err := i2c.New(mcp23017.DefI2CAdr, 1)
    if err != nil {
        log.Fatal(err)
    }

    mcp, err := mcp23017.New(i2c)
    if err != nil {
        log.Fatal(err)
    }

    // Sets all pins to INPUT mode
    mcp.Set(mcp23017.AllPins()).INPUT()
    
    // Gets values all INPUT pins
    v, err := mcp.Get(mcp23017.AllPins())
    if err != nil {
    	log.Printf(err.Error())
    }
    log.Println(v)

    // Turn on pull up resistors for A0, A1, A2 and B7 pins
    mcp.Set(mcp23017.Pins{"A0", "A1", "A2", "B7"}).PULLUP()

    LedPin := mcp23017.Pins{"B0", "B1", "B2", "B3"}
    
    // Sets pins to OUTPUT mode
    mcp.Set(LedPin).OUTPUT()

    // Run cycle
    for {
        if err := mcp.Set(LedPin).HIGH(); err != nil {
		log.Printf(err.Error())
	}
        time.Sleep(3 * time.Second)

        if err := mcp.Set(LedPin).LOW(); err != nil {
		log.Printf(err.Error())
	}
        time.Sleep(3 * time.Second)
    }
}

Getting help

GoDoc documentation

Installation

$ go get -u github.com/googolgl/go-mcp23017

Troubleshooting

  • How to obtain fresh Golang installation to RPi device (either any RPi clone): If your RaspberryPI golang installation taken by default from repository is outdated, you may consider to install actual golang manually from official Golang site. Download tar.gz file containing arm64 in the name. Follow installation instructions.

  • How to enable I2C bus on RPi device: If you employ RaspberryPI, use raspi-config utility to activate i2c-bus on the OS level. Go to "Interfacing Options" menu, to active I2C bus. Probably you will need to reboot to load i2c kernel module. Finally you should have device like /dev/i2c-1 present in the system.

  • How to find I2C bus allocation and device address: Use i2cdetect utility in format "i2cdetect -y X", where X may vary from 0 to 5 or more, to discover address occupied by peripheral device. To install utility you should run apt install i2c-tools on debian-kind system. i2cdetect -y 1 sample output:

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- 76 --    
    

Change i2c bus baudrate

modprobe -r i2c_bcm2708
modprobe i2c_bcm2708 baudrate=1200000

Datasheets for supported devices

MCP23017 / MCP23S17 : http://ww1.microchip.com/downloads/en/DeviceDoc/20001952C.pdf

Contact

Please use Github issue tracker for filing bugs or feature requests.

License

Go-mcp23017 is licensed under MIT License.

# Functions

AllPins - return all pins in all banks.
New creates the new MCP23017 driver with specified i2c interface and options.

# Constants

Bit 7.
DefI2CAdr - default address for controller (0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27).
DEFVAL - Default value register (0x03).
Bit 4.
GPINTEN - Interrupt-on-change pins (0x02).
GPIO - General purpose I/O port register (0x09).
GPPU - GPIO pull-up resistor register (0x06).
Bit 3.
INTCAP - Interrupt captured value for port register (0x08).
Bit 0.
INTCON - Interrupt-on-change control register (0x04).
INTF - Interrupt flag register (0x07).
Bit 1.
IOCON - I/O expander configuration register (0x05).
0x00.
IPOL - Input polarity port register (0x01).
Bit 6.
Bit 2.
OLAT - Output latch register 0 (0x10).
Bit 5.

# Structs

No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author