knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

rpigpior

Project Status: Active – The project has reached a stable, usable
state and is being actively
developed.

R-CMD-check

rpigpior allows the R programming language access to the Raspberry Pi GPIO using libgpiod. Because libgpiod is already included in Raspbian, there is nothing else to install.

rpigpior provides these tools:

Installation

rpigpior is hosted on github. Until it arrives at cran, you can install the development version of rpigpior from GitHub with:

library(remotes)
remotes::install_github("mnr/rpigpior")

Most documentation will tell you to use devtools, but install.packages("devtools") on a Raspberry Pi is an onerous task.

However...if you installed R on your Raspberry Pi using r4pi, you can use devtools just like normal.

# Start R with "sudo R" to install packages
# install.packages("devtools")

# You might want to switch back to non-admin R
library(devtools)
install_github("mnr/rpigpior")

Once you've installed, use library just like any other R package:

library(rpigpior)

Is this a Raspberry Pi?

It's helpful to check if your code is running on a Raspberry Pi.

if ( is.rpi() ) {
   print("Yes, this is a RPi")
} else {
   print ("No, this is not a RPi")
}

Reading a pin

If you connect a switch to board pin 40, this code will read it:

library(rpigpior)

rpi_get(40) # reads board pin 40
rpi_get(c(7,40)) # reads board pins 7 and 40

> GPIO004  GPIO21
        0       1

That assumes the switch to board pin 40 is "pushed" or closed. By the way, there is a diagram of this located in the articles section of the website (as identified in the package description)

Turning pins on and off

If you have a LEDs connected to board pin 19, 21, and 23, this code will turn them on, then one of them off, then all of them off

library(rpigpior)

toggleThese <- c(19,21,23)
rpi_set(toggleThese,1)
Sys.sleep(1)
rpi_set(toggleThese, c(1,0,1))
Sys.sleep(1)
rpi_set(toggleThese, 0)

Citation:

citation("rpigpior")

Related notes:

Related mailing list: linux-gpio\@vger.kernal.org



mnr/rpigpior documentation built on May 3, 2024, 12:14 a.m.