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.
https://en.wikipedia.org/wiki/R_%28programming_language%29
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…
rpigpior
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:
library(rpigpior) 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 library(rpigpior) # 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.
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.
Continue reading: R Meets Hardware
Analyzing the Purpose and Potential of R Programming for Hardware Control
The text discusses the application of the R programming language beyond its traditional uses in statistical computing, data visualization, data mining, and bioinformatics. It highlights the potential for R, given its nature as a logical abstraction layer above metal, to interface with hardware constructs such as GPIO pins on a Raspberry Pi.
Breaking Down the Metal Barrier
Primarily, all programming languages essentially drive the behavior of transistors, and subsequently, the logic gates and microcircuits built on them. While R, being a high-level language, traditional thought suggests that it is best suited for tasks that do not involve dealing with the base hardware. However, the text challenges this assumption and demonstrates practical ways of adopting R for hardware control.
R for Raspberry Pi
The text coats how to install R onto a Raspberry Pi, which usually amongsta user-friendly installer package or a cumbersome, manual installation of a Linux version of R from the CRAN repository. Post-installation, the user can utilize the rpigpior package to interface R with the Raspberry Pi’s GPIO pins. This enables control over a multitude of devices like servos, pushbuttons, lights among others.
Implications and Future Developments
As the text demonstrates, by breaking down the assumption that R’s usage lies primarily in software development and statistical analysis can open up an array of opportunities. Firstly, there could be a proliferation of packages akin to rpigpior that would enable R to interface with various hardware devices. Secondly, it could lead to the development of more robust methods to handle critical hardware control systems such as irrigation systems.
R could essentially become a one-stop-shop for developers who are comfortable with it. Data collection, data analysis, and hardware control could all be handled within a single programming environment, which is a massive convenience.
Actionable Advice
If you’re proficient in R, look beyond its established functionalities and explore the possibility of using R for hardware control applications. Start by understanding how to interface R with your desired hardware, and if suitable packages aren’t available, consider developing your own.
If you’re venturing into this domain for the first time, focus on simple proof-of-concept projects before moving onto complex applications. For organisations with a proficient R team, encourage them to explore these potentials as it can streamline operations, saving both time and resources.
Embrace The Future
Remember, it is our assumptions that often limit the progress and application of technology. Challenge them, think outside the box, and embrace the future of R in hardware control.