Controlling Your World With Ruby

Scott H Lee
2 min readJan 14, 2021
Arduino

The Arduino microcontroller comes in many flavors and colors, all of which fit in the palm of your hand, or can even be sewn into clothing. One thing that they all have in common is the ability to monitor and control the outside world. A number of input and output pins can monitor digital and analog sensors and actuate motors and relays. With add-on boards, called shields, any number of different functionalities can be added, such as wireless communications, touchscreens, relay control, internet , and so much more. For these reasons, Arduino boards and similar platforms have become favorites for robotics enthusiasts and programmers of the Internet of Things.

Of course, Arduino is shipped with a GUI for serial programming, but many other languages have been ported to interact with them. If you are a Ruby programmer, fear not. With the installation of some gems and add on packages, you too can control and monitor your world. Artoo is a cool gem that allows Ruby to control and interact with lots of different hardware platforms. With Artoo, the Gort CLI, which provides serial communications through a USB port, and some configuration, you can be controlling l.e.d.’s, monitoring button pushes, and monitoring and controlling your thermostat in Ruby code stored in standard .rb files. Control your smart house with applications written and built in Ruby. Monitor and control everything with an interactive web interface programmed in Rails and Javascript.

The following example demonstrates some simple features:

arduino_pushbutton.rb

run with a call to the file: ruby arduino_pushbutton.rb

require 'artoo'connection :arduino, adaptor: :firmata, port: '/dev/ttyACM0'device :led, driver: :led, pin: 13
device :button, driver: :button, pin: 2, interval: 0.01
work do
puts "Press the button connected on pin #{ button.pin }..."
on button, :push => proc { led.on }
on button, :release => proc { led.off }
end

Of course, there is a learning curve to get acquainted with the hardware and the commands provided by the Artoo gem, but with a little bit of work and a familiar language to utilize, Ruby programmers can have access to a whole new world of programming possibilities.

--

--