Interfacing...

Arduino and Ruby

The SerialPort gem seems to do the job.

simplest example

Here's a simple example of how to read from the arduino (now efficiently) via ruby using the SerialPort gem. You may have to change the port_str param (or use an integer for that param as port number). See the readme within the ruby-serialport download.

  1. #simplest ruby program to read from arduino serial,
  2. #using the SerialPort gem
  3. #(http://rubygems.org/gems/serialport)
  4.  
  5. require "serialport"
  6.  
  7. #params for serial port
  8. port_str = "/dev/ttyUSB0"  #may be different for you
  9. baud_rate = 9600
  10. data_bits = 8
  11. stop_bits = 1
  12. parity = SerialPort::NONE
  13.  
  14. sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
  15.  
  16. #just read forever
  17. while true do
  18.    while (i = sp.gets.chomp) do       # see note 2
  19.       puts i
  20.       #puts i.class #String
  21.     end
  22. end
  23.  
  24. sp.close                       #see note 1

  • note 1: As with the File api, you can instead use SerialPort.new() or .open() with a block to have the resource automatically closed for you (more robust).
  • note 2: gets add a new line after any variable that memorize. If this is not desired you can call the method chomp on the gets result and the newline is suppressed


arduino_firmata rubygem

Firmata protocol implementation on Ruby. You can embed analog/digital read/write function in Ruby.

Install Rubygem

% gem install arduino_firmata

Install Firmata

Arduino IDE -> [File] -> [Examples] -> [Firmata] -> [StandardFirmata]

Sample

LED Blink

  1. require "rubygems"
  2. require "arduino_firmata"
  3.  
  4. arduino = ArduinoFirmata.connect
  5. puts "firmata version #{arduino.version}"
  6.  
  7. loop do
  8.   arduino.digital_write 13, true
  9.   sleep 0.5
  10.   arduino.digital_write 13, false
  11.   sleep 0.5
  12. end

Digital Read

  1. arduino = ArduinoFirmata.connect
  2. arduino.pin_mode 2, ArduinoFirmata::INPUT
  3.  
  4. loop do
  5.   if arduino.digital_read 2
  6.     puts "pin 2 on"
  7.   else
  8.     puts "pin 2 off"
  9.   end
  10.   sleep 0.1
  11. end

Analog I/O

  1. arduino = ArduinoFirmata.connect
  2.  
  3. loop do
  4.   an = arduino.analog_read 0
  5.   puts an
  6.   arduino.analog_write 11, an/4
  7.   sleep 0.1
  8. end

Servo Motor

  1. arduino = ArduinoFirmata.connect
  2.  
  3. loop do
  4.   angle = rand 180
  5.   puts "servo angle #{angle}"
  6.   arduino.servo_write 9, angle
  7.   sleep 1
  8. end

Sysex Send

  1. arduino.sysex 0x01, [13, 5, 2]  # command, data_array

Sysex Receive

  1. arduino.on :sysex do |command, data|
  2.   puts "command : #{command}"
  3.   puts "data    : #{data.inspect}" # data_array
  4. end

see tutorial


Dino: Bootstrap your Arduino

What is it

Dino is a ruby gem designed to get you up and running with your Arduino in seconds. It is an open source project and actively accepts pull requests on Github.

Gem Goal

  • Maintain excellent documentation through Fritzing diagrams and working examples.
  • 0 start up time: plug your Arduino into your computer burn one C++ file and everything should start working.
  • Strong Domain Specific Language for working with components.
  • Exhaustive support for components.
  • Fun

Example

A simple blinking light program

  1. require 'dino'
  2.  
  3. board = Dino::Board.new(Dino::TxRx.new)
  4. led = Dino::Components::Led.new(pin: 13, board: board)
  5.  
  6. [:on, :off].cycle do |switch|
  7.   led.send(switch)
  8.   sleep 0.5
  9. end

More Info

Dino was presented at RubyConf 2012. The talk can be found at Confreaks - Arduino the Ruby Way, and the slides on SpeakerDeck


Arduino rubygem

Arduino gem is a prototyping API for Arduino in Ruby. Helps prototype Arduino programs quickly from the computer, without the need to burn programs to the board frequently. It's a "burn once, write many programs" solution.


See also

others using the SerialPort gem:

Share