New! Unlimited audio, video & web asset downloads! Unlimited audio, video & web assets! From $16.50/m
Advertisement
  1. Code
  2. Ruby
Code

Using Ruby to Program Arduino

by
Length:MediumLanguages:

Through this article you will learn how we can go on to build a project where the LED goes RED if anyone tweets with a :-( (sad face) and the word rain, and it goes GREEN when the tweet contains a :-) (happy face) and the word rain!

Physical computing refers to the idea of programming things around you. Unlike typical programming for files, data, or the Internet, this is programming for things you find in the physical world. Using Arduino we can read the physical inputs and modify them based on logic and respond to it with help of motors.

What Is Arduino?

Arduino is a single process microcontroller which means that it can run one program at a time, and it can run it indefinitely until you remove the power or well replace it with another program.

Based on varying capabilities, a wide range of Arduino boards are available and they also come in various shapes and sizes. For example, an Arduino Mini is almost the size of the thumb, a Arduino Mega is bigger and has many more sensor controls. Lilypad can be woven into fabric and is also waterproof!

Let's look at an Arduino Uno, which is the most commonly used. arduino uno

It comes with a USB port that allows us to connect to a laptop and write code for it. There is also the power input, which can be used when you want to use the board as standalone and not tethered to your computer.

It has 14 digital input/output pins and six analog pins. The digital pins can be turned on and turned off based on the logic of microcontroller. They can also be used to read in the values in binary form. The analog pins can be used to do a range of values and not just on or off, so they can be used to plugin sensors like the temperature or light sensors. There are also some pulse width modulation pins provided to do a range of values using the digital pins. (Pins numbered 11, 10, 6 , 5, 3 would refer to that.)

Why Use an Arduino?

First, it's inexpensive, cross-platform, has a good IDE, and all of the software and hardware are open source. When referring to open source, here's how all of the components are licensed:

Hardware CC-SA-BY
Software GPL
Docs CC-SA-BY
Brand TM

Moreover, in addition to these points, the entire Arduino board along with its community is so vibrant and creative, that it is hard to miss. Arduino is that one most important Lego block in the entire puzzle of rapid prototyping for creatives.

How Arduino Communicates

Since the primary objective of this article is to explore how to use Ruby for interacting with Arduino, let's explore how the communication works from a software side.

All Arduino boards have at least one serial port (also known as UART) that can be used for communication between the board and a computer. It doesn't matter what programming language is on the other device, as long as it can interface via serial. Alternatively, we can also use wired LAN and WiFi interfaces by using shields.

Ruby Arduino Solutions

Since the Arduino can communicate via its serial port, the most basic approach to controlling the Arduino would be using the serial Ruby library.

Serial Port Gem

Serial Port gem is an implementation of RS232 serial ports with several low level functionalities to control signals on the line.

To determine your port_str on Linux, you can open irb and execute `ls /dev`.split("\n").grep(/usb|ACM/i).map{|d| "/dev/#{d}"}

Another option to use Ruby with Arduino would obviously be a mere abstraction of hiding these internal details and working more at object level. That is the exact approach that RAD, or Ruby Arduino Development, opted to use. Unfortunately, the project is no longer maintained.

Dino Gem

This gem takes a more loosely coupled and strongly cohesive approach to the problem of using Ruby with Arduino. By that, I mean it uses a generic library on the Arduino, which dynamically responds to the requests from the computer over the serial connection. 

This also provides much more abstraction to deal with the components involved. The Dino gem itself has a dependency on the serial port gem explaining that it uses the same gem for serial communication while providing the programmer with useful abstractions.

After installing the dino gem, run dino generate-sketch serial from your terminal. This will generate a .ino sketch. Upload this sketch to Arduino using the Arduino IDE, and then from run the below code from the computer connected to the Arduino.

You can run this file from the computer by running ruby dino_sample.rb This should blink the LED on pin 13 of the Arduino. We're now controlling hardware with Ruby!

Let's tweak this code above to make use of the Twitter API and have the device blink based on certain signals.

Triggering the LED With a Tweet

Connect to pin 13 and 12, RED LED and GREEN LED, respectively. Then connect the Arduino to the computer and upload the default Dino serial sketch which was generated in the previous step.

Now, we'll use the tweetstream gem for Twitter integration. To be able to use it successfully, you will have to create a Twitter app, and use the consumer keys and oAuth tokens for the sample and put them in a file twitter_api_config.yml.

Let's look at the code to cause the LEDs to blink based on information coming from Twitter.

The credentials will be stored in another file called twitter_api_config.yml as below.

The above code, when run, you will have LED's blinking based on the Tweet content! What we have done is connected the Arduino to a computer, uploaded the Dino serial code to the Arduino, and then are running the above code on our machine by saying ruby twitter_leds.rb

Above code is available at this github repository.

In  twitter_leds.rb we first create a connection to the Arduino board using Dino. Then we go on to create redled and greenled to the pins 13 and 12, respectively. After that, we create arrays of happy and sad words, to keep things simple. After that, we keep looking for the occurence of the word rain in any tweet, and then go on to check if its a sad word and, if so, put on the trigger the red LED rather than the green LED.

You now have LEDs being triggered by random tweets across the globe. Awesome, isn't it?

References

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.