PCA9685.Device (pca9685 v1.1.0)
GenServer managing a PCA9685 device connection.
Options
:bus(String.t/0) - Required. The I2C bus name (e.g."i2c-1").:address- Required. The I2C address of the device (0-127, e.g.0x40).:pwm_freq- The PWM frequency in Hz. The default value is50.:oscillator_freq(pos_integer/0) - The PCA9685 internal oscillator frequency in Hz. Nominally 25MHz but varies per chip (23-27MHz). Calibrate for precise pulse width control. The default value is25000000.:oe_pin(non_neg_integer/0) - GPIO pin number for output enable (directly connected to OE\ pin).:name(term/0) - GenServer name for registration (e.g.MyPWMor{:via, Registry, key}).
Summary
Functions
Sets all channels to the specified duty cycle (0-4095).
Sets the channel (0-15) to a specified duty cycle (0-4095).
Reads all channels' duty cycles.
Reads a channel's duty cycle.
Reads a channel's PWM pulse width in microseconds.
Disables PWM output for this device.
Enables PWM output for this device.
Sets a channel's PWM pulse width in microseconds.
Returns the currently configured PWM frequency.
Configures the PWM frequency (24-1526 Hz).
Starts a PCA9685 device GenServer.
Functions
@spec all(GenServer.server(), 0..4095, 0..4095) :: :ok | {:error, term()}
Sets all channels to the specified duty cycle (0-4095).
@spec channel(GenServer.server(), 0..15, 0..4095, 0..4095) :: :ok | {:error, term()}
Sets the channel (0-15) to a specified duty cycle (0-4095).
@spec get_all(GenServer.server()) :: {:ok, [{0..4095, 0..4095}]} | {:error, term()}
Reads all channels' duty cycles.
Returns a list of 16 {on, off} tuples, one for each channel.
@spec get_channel(GenServer.server(), 0..15) :: {:ok, {0..4095, 0..4095}} | {:error, term()}
Reads a channel's duty cycle.
Returns {on, off} values (0-4095).
@spec get_pulse_width(GenServer.server(), 0..15) :: {:ok, pos_integer()} | {:error, term()}
Reads a channel's PWM pulse width in microseconds.
This is the inverse of pulse_width/3, converting the raw register values
back to microseconds based on the configured oscillator and PWM frequency.
@spec output_disable(GenServer.server()) :: :ok | {:error, term()}
Disables PWM output for this device.
Fails if there is no oe_pin specified in the device configuration.
@spec output_enable(GenServer.server()) :: :ok | {:error, term()}
Enables PWM output for this device.
Fails if there is no oe_pin specified in the device configuration.
@spec pulse_width(GenServer.server(), 0..15, pos_integer()) :: :ok | {:error, term()}
Sets a channel's PWM pulse width in microseconds.
This is useful for servo control where pulse widths are typically:
- 1000µs (1ms) for minimum position
- 1500µs for centre position
- 2000µs (2ms) for maximum position
The conversion accounts for the configured oscillator frequency and PWM frequency to calculate the correct register values.
Example
# Move servo on channel 0 to centre position
PCA9685.Device.pulse_width(pid, 0, 1500)
@spec pwm_freq(GenServer.server()) :: pos_integer()
Returns the currently configured PWM frequency.
@spec pwm_freq(GenServer.server(), pos_integer()) :: :ok | {:error, term()}
Configures the PWM frequency (24-1526 Hz).
@spec start_link(keyword()) :: GenServer.on_start()
Starts a PCA9685 device GenServer.
See module documentation for available options.