ServoKit.PCA9685 (servo_kit v0.2.0) View Source

Controls the PCA9685 PWM driver

Link to this section Summary

Types

Current duty cycle value of each channel. The tuple index corresponds to the channel.

t()

Functions

Calculates the PWM frequency in Hz based on specified prescale value and reference clock speed.

Initializes the servo driver and returns the initial state.

Calculates the PWM frequency prescale based on the formula in data sheet 7.3.5.

Calculates 12-bit pulse range from a percentage value.

Performs the software reset. See PCA9685 data sheet 7.1.4 and 7.6.

Sets a single PWM channel or all PWM channels by specifying the duty cycle in percent.

Sets the PWM frequency to the provided value in hertz. The PWM frequency is shared by all the channels.

Puts the board into the sleep mode.

Wakes the board from the sleep mode.

Sets a single PWM channel or all PWM channels by specifying when to switch on and when to switch off in a period. These values must be between 0 and 4095.

Link to this section Types

Specs

channel() :: 0..15 | :all

Specs

duty_cycles() ::
  {number(), number(), number(), number(), number(), number(), number(),
   number(), number(), number(), number(), number(), number(), number(),
   number(), number()}

Current duty cycle value of each channel. The tuple index corresponds to the channel.

Specs

frequency() :: 24..1526

Specs

options() :: [
  bus_name: ServoKit.Transport.bus_name(),
  address: ServoKit.Transport.address(),
  reference_clock_speed: pos_integer(),
  frequency: frequency()
]

Specs

t() :: %ServoKit.PCA9685{
  duty_cycles: duty_cycles(),
  i2c_address: ServoKit.Transport.address(),
  i2c_ref: ServoKit.Transport.bus(),
  mode1: pos_integer(),
  prescale: pos_integer(),
  reference_clock_speed: pos_integer()
}

Link to this section Functions

Link to this function

frequency_from_prescale(prescale, reference_clock_speed)

View Source

Specs

frequency_from_prescale(pos_integer(), pos_integer()) :: pos_integer()

Calculates the PWM frequency in Hz based on specified prescale value and reference clock speed.

Examples

iex> PCA9685.frequency_from_prescale(255, 25_000_000)
24

iex> PCA9685.frequency_from_prescale(121, 25_000_000)
50

iex> PCA9685.frequency_from_prescale(60, 25_000_000)
102

Specs

init(options()) :: {:ok, t()} | {:error, any()}

Initializes the servo driver and returns the initial state.

Link to this function

prescale_from_frequecy(freq_hz, reference_clock_speed)

View Source

Specs

prescale_from_frequecy(24..1526, integer()) :: 3..255

Calculates the PWM frequency prescale based on the formula in data sheet 7.3.5.

Formula

prescale_value = round(osc_value / (4096 * update_rate)) - 1

Examples

iex> PCA9685.prescale_from_frequecy(24, 25_000_000)
253

iex> PCA9685.prescale_from_frequecy(50, 25_000_000)
121

iex> PCA9685.prescale_from_frequecy(100, 25_000_000)
60

iex> PCA9685.prescale_from_frequecy(1526, 25_000_000)
3
Link to this function

pulse_range_from_percentage(percent)

View Source

Specs

pulse_range_from_percentage(number()) :: {0, 0..4095}

Calculates 12-bit pulse range from a percentage value.

Examples

iex> PCA9685.pulse_range_from_percentage(0.0)
{0, 0}

iex> PCA9685.pulse_range_from_percentage(50.0)
{0, 2048}

iex> PCA9685.pulse_range_from_percentage(100.0)
{0, 4095}

Performs the software reset. See PCA9685 data sheet 7.1.4 and 7.6.

Link to this function

set_pwm_duty_cycle(state, percent, list)

View Source

Specs

set_pwm_duty_cycle(t(), number(), [{:ch, channel()}]) ::
  {:ok, t()} | {:error, any()}

Sets a single PWM channel or all PWM channels by specifying the duty cycle in percent.

Examples

ServoKit.PCA9685.set_pwm_duty_cycle(driver, 50.0, ch: 0)
ServoKit.PCA9685.set_pwm_duty_cycle(driver, 50.0, ch: :all)
Link to this function

set_pwm_frequency(state, freq_hz)

View Source

Specs

set_pwm_frequency(t(), frequency()) :: {:ok, t()} | {:error, any()}

Sets the PWM frequency to the provided value in hertz. The PWM frequency is shared by all the channels.

Examples

ServoKit.PCA9685.set_pwm_frequency(state, 50)

Puts the board into the sleep mode.

Wakes the board from the sleep mode.

Link to this function

write_pulse_range(state, ch, arg)

View Source

Specs

write_pulse_range(t(), :all | byte(), {char(), char()}) :: t()

Sets a single PWM channel or all PWM channels by specifying when to switch on and when to switch off in a period. These values must be between 0 and 4095.