pca9685

package module
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2022 License: MIT Imports: 3 Imported by: 8

README

PCA9685 16-Channel 12-Bit PWM Driver

GoDoc MIT License

PCA9685 (pdf reference) is a popular controller among Arduino and Raspberry PI developers. The 16-Channel 12-bit PWM/Servo Driver will drive up to 16 servos over I2C with only 2 pins. The on-board PWM controller will drive all 16 channels. What's more, you can chain up to 62 of them to control up to 992 servos - all with the same 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-pca9685"
)

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(pca9685.Address, "/dev/12c-0")
    if err != nil {
        log.Fatal(err)
    }

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

    // Sets a single PWM channel 0
    pca0.SetChannel(0, 0, 130)

    // Servo on channel 0
    servo0 := pca0.ServoNew(0, nil)

    // Angle in degrees. Must be in the range `0` to `Range`
    for i := 0; i < 130; i++ {
        servo0.Angle(i)
        time.Sleep(10 * time.Millisecond)
    }

    // Fraction as pulse width expressed between 0.0 `MinPulse` and 1.0 `MaxPulse`
    servo0.Fraction(0.5)
}

Getting help

GoDoc documentation

Installation

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

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: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- 76 --    
    

Contact

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

License

Go-pca9685 is licensed under MIT License.

Documentation

Index

Constants

View Source
const (
	// Address default for controller
	Address byte = 0x40

	// Registers
	Mode1    byte = 0x00
	Prescale byte = 0xFE
	Led0On   byte = 0x06

	// The internal reference clock is 25mhz but may vary slightly with
	// environmental conditions and manufacturing variances. Providing a more precise
	// "ReferenceClockSpeed" can improve the accuracy of the frequency and duty_cycle computations.
	ReferenceClockSpeed float32 = 25000000.0 // 25MHz
	StepCount           float32 = 4096.0     // 12-bit
	DefaultPWMFrequency float32 = 50.0       // 50Hz
)
View Source
const (
	// The specified pulse width range of a servo has historically been 1000-2000us,
	// for a 90 degree range of motion. But nearly all modern servos have a 170-180
	// degree range, and the pulse widths can go well out of the range to achieve this
	// extended motion. The default values here of `750` and `2250` typically give
	// 135 degrees of motion. You can set `Range` to correspond to the
	// actual range of motion you observe with your given `MinPulse` and `MaxPulse` values.
	ServoRangeDef    int     = 135
	ServoMinPulseDef float32 = 750.0
	ServoMaxPulseDef float32 = 2250.0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Options added in v0.1.0

type Options struct {
	Name       string
	Frequency  float32
	ClockSpeed float32
}

Options for controller

type PCA9685

type PCA9685 struct {
	// contains filtered or unexported fields
}

PCA9685 is a Driver for the PCA9685 16-channel 12-bit PWM/Servo controller

func New added in v0.1.3

func New(i2c *i2c.Options, optn *Options) (*PCA9685, error)

New creates the new PCA9685 driver with specified i2c interface and options

func (*PCA9685) GetFreq added in v0.1.0

func (pca *PCA9685) GetFreq() float32

GetFreq returns frequency value

func (*PCA9685) Reset

func (pca *PCA9685) Reset() (err error)

Reset the chip

func (*PCA9685) ServoNew added in v0.1.3

func (pca *PCA9685) ServoNew(chn int, o *ServOptions) *Servo

ServoNew creates a new servo driver

func (*PCA9685) SetChannel

func (pca *PCA9685) SetChannel(chn, on, off int) (err error)

SetChannel sets a single PWM channel

func (*PCA9685) SetFreq

func (pca *PCA9685) SetFreq(freq float32) (err error)

SetFreq sets the PWM frequency in Hz for controller

type ServOptions added in v0.1.0

type ServOptions struct {
	AcRange  int // actuation range
	MinPulse float32
	MaxPulse float32
}

ServOptions for servo

type Servo added in v0.1.0

type Servo struct {
	// contains filtered or unexported fields
}

Servo structure

func (*Servo) Angle added in v0.1.0

func (s *Servo) Angle(a int) (err error)

Angle in degrees. Must be in the range `0` to `Range`.

func (*Servo) Fraction added in v0.1.0

func (s *Servo) Fraction(f float32) (err error)

Fraction as pulse width expressed between 0.0 `MinPulse` and 1.0 `MaxPulse`. For conventional servos, corresponds to the servo position as a fraction of the actuation range.

func (*Servo) Reset added in v0.1.3

func (s *Servo) Reset() (err error)

Reset channel

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL