Skip to contents

Provides the R programming language with access to Raspberry Pi hardware Pulse Width Modulation (PWM)

Usage

rpi_pwm(
  pin_number = 12,
  pwm_period = 50000,
  pwm_dutycycle = 25000,
  pwm_debug = FALSE
)

Arguments

pin_number

One or two pins for hardware PWM. Must be 12, 32, 33, or 35. If two pins are selected, they must be one of these combinations: (12,33), (32,33), (12,35), or (32,35)

pwm_period

The length of a cycle. aka Frequency. 15 or greater, less than 1000000

pwm_dutycycle

The amount of time a cycle is on.

pwm_debug

If TRUE, checks Raspberry Pi OS settings for PWM and provides diagnostics

Value

void rpi_pwm() provides extensive error checking. Possible errors include:

  • Invalid PWM pin: Hardware PWM is only supplied to pins 12, 32, 33, or 35.

  • Invalid PWM pin combination: The combination of pins both select PWM0 or PWM1

  • PWM not enabled: This PWM channel is not enabled

  • (see https://mnr.github.io/rpigpior/articles/rpi_pwm.html for fixing errors)

Details

Only two PWM lines are available on the Raspberry Pi - pwm0 and pwm1. Although it is possible to create software PWM, this isn't advised due to latency of linux and R.
It might be helpful to read the article on `rpi_pwm()` located at the [rpigpior website](https://mnr.github.io/rpigpior/articles/rpi_pwm.html).

Examples

if (FALSE) { # is.rpi()
rpi_pwm(12) # provides 50% PWM to pin 12 (PWM0)

#' # provides 20% PWM to pin 12 (PWM0) and pin 33 (PWM1)
rpi_pwm(c(12, 33), pwm_period = 50000, pwm_dutycycle = 10000)
}