R Meets Hardware

[This article was first published on R Programming Archives - Mark Niemann-Ross, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

R is a programming language for statistical computing and data visualization. It has been adopted in the fields of data mining, bioinformatics, and data analysis.


Data mining, bioinformatics, data analysis…

…But not hardware.

We assume R isn’t useful for flipping switches and reading sensors. But that’s an assumption, not a fact.

Let’s be honest. Computer languages are just an abstraction layer on top of the metal. Languages provide constructs to make the expression of logic easier, tailored for the different ways people approach logic and data. But all languages eventually drive the behavior of transistors, and the logic gates and microcircuits built on top of those transistors.

We assume R doesn’t care about transistors. Possibly true, but I don’t believe that’s set in stone. In fact, I’ve proved it’s not true.

R is a language just like C or Python. Unlike C or MicroPython, R requires an operating system. It is a high-level language; memory management or assembler isn’t something we talk about at the R conventions. As written, it’s not well suited for base hardware such as Arduino, espy, or RP2040.

I’ve done assembler on a 6502; partly as a lark, partly because I only had 64k of memory. That’s no longer an issue with any computer in my office. Now I can focus on abstract issues like correlation and causation. I still need to interact with the real world and sometimes datasets need to be collected from buttons and sensors.

I could wire up a data acquisition tool using sensors and microprocessors then store data to some media. Or… I can use R to control the GPIO pins on a Raspberry Pi. The latter is cleaner, but I had to do two things to make it happen: Install R on a Raspberry Pi, and develop a package for R to communicate with the Raspberry Pi GPIO.

R for Pi

Installing R on a Raspberry Pi is easy if you use R4Pi. These fine folks have compiled all the popular bits you’ll need. Download it, run the installer, and you’re done.

Alternatively… You can do it the hard way and install a Linux version of R from cran.  Be prepared for a long wait while everything is downloaded and compiled. Snore…


Running R on a Raspberry Pi is one thing. But the reason Raspberry Pi exists is the GPIO – General Purpose Input Output pins. These are wires – connections to servos, pushbuttons, lights and other electronic circuits. Real world stuff. Things that do stuff like control flow, lift weights, watch lights, feel for pushbuttons.

rpigpior is a package for R to interface with the Raspberry Pi GPIO.

Read a Pushbutton

Here’s an example of using R to read a pushbutton on GPIO board pin 40. It’s pretty simple and doesn’t use any advanced R functions:


while (TRUE) {
  if (rpi_get(40)) {
    print("Button pushed")  }
 else {
    print("Button not pushed")  

Control a Relay

The Raspberry Pi is narrow-minded about voltages. It supplies either 3.3 volts d.c. or 5 volts d.c. Not 12 VDC. Certainly not a.c and especially not household 120 VAC.

I’m building an irrigation system with off-the-shelf water valves. I need 24 VAC to drive these valves and I need to control that voltage with the 3.3 VDC signal voltage supplied by the Raspberry Pi. Adafruit sells a relay for $6.95 that does exactly that. I ordered two, and in a week I had them connected and wrote R code to open and close valves.

# running in R


# turns on relay connected to board pin 11, GPIO17
rpi_set(pin_number = 11, onOff = 1) 
# wait a bit...
rpi_set(pin_number = 11, onOff = 0) 

R for Reporting

The sprinkler system I’ve built in R checks the weather report, then decides how much to water the garden. It’s not excessively complex code, but it was simple to get the weather reports I needed and then do the calculations on how much water to provide. I’ve given it control of two irrigation valves which control the amount of water sent to the front and back yards. I also wanted to keep track of how the system was performing, so I wrote a dashboard using shiny.

There are more examples at the github repository.

Why Use R

Maybe a better question is “Why Not Use R.” Granted, you can accomplish everything I’ve shown here in any other language. But I’m comfortable with R and if I can use it for the full cycle, that keeps me from having to invest extra time in learning an alternative. And I’m all about saving time!

What about you? Any thoughts on using R to control Hardware?

The post R Meets Hardware appeared first on Mark Niemann-Ross.

To leave a comment for the author, please follow the link and comment on their blog: R Programming Archives - Mark Niemann-Ross.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)