All Projects → ClockVapor → Rpi_gpio

ClockVapor / Rpi_gpio

Licence: mit
Ruby conversion of RPi.GPIO Python module

Programming Languages

c
50402 projects - #5 most used programming language
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Rpi gpio

Raspberrypi tempmon
Raspberry pi CPU temperature monitor with many functions such as logging, GPIO output, graphing, email, alarm, notifications and stress testing. Python 3.
Stars: ✭ 52 (-71.89%)
Mutual labels:  raspberry-pi, raspberrypi, raspberry-pi-3, rpi
Gassistpi
Google Assistant for Single Board Computers
Stars: ✭ 911 (+392.43%)
Mutual labels:  raspberry-pi, raspberrypi, raspberry-pi-3, rpi
Openauto
AndroidAuto headunit emulator
Stars: ✭ 1,926 (+941.08%)
Mutual labels:  raspberry-pi, raspberrypi, raspberry-pi-3
Rpi Battery Monitor
Monitor battery voltage from a Raspberry Pi
Stars: ✭ 51 (-72.43%)
Mutual labels:  raspberry-pi, raspberry-pi-3, rpi
Aasdk
Library to build AndroidAuto headunit emulator
Stars: ✭ 167 (-9.73%)
Mutual labels:  raspberry-pi, raspberrypi, raspberry-pi-3
Enclosure Picroft
Mycroft interface for Raspberry Pi environment
Stars: ✭ 649 (+250.81%)
Mutual labels:  raspberry-pi, raspberry-pi-3, rpi
Raspberrypipkg
DEPRECATED - DO NOT USE | Go here instead ->
Stars: ✭ 758 (+309.73%)
Mutual labels:  raspberrypi, raspberry-pi-3, rpi
Spypi
An (un-)ethical hacking-station based on Raspberry Pi and Python
Stars: ✭ 167 (-9.73%)
Mutual labels:  raspberry-pi, raspberrypi, raspberry-pi-3
gpio
A RaspberryPi GPIO library written in PHP.
Stars: ✭ 16 (-91.35%)
Mutual labels:  rpi, gpio, raspberrypi
Homekitcam
A project to make a Raspberry Pi driven, HomeKit Enabled camera.
Stars: ✭ 69 (-62.7%)
Mutual labels:  raspberry-pi, raspberrypi, raspberry-pi-3
Opencv 3.2.0 Compiling On Raspberry Pi
Download, Compile, Build, and Install OpenCV 3.2.0 with Extra Modules on RPI running Jessie
Stars: ✭ 65 (-64.86%)
Mutual labels:  raspberry-pi, raspberry-pi-3, rpi
Gpiozero
A simple interface to GPIO devices with Raspberry Pi
Stars: ✭ 1,302 (+603.78%)
Mutual labels:  raspberry-pi, raspberrypi, gpio
Rppal
A Rust library that provides access to the Raspberry Pi's GPIO, I2C, PWM, SPI and UART peripherals.
Stars: ✭ 463 (+150.27%)
Mutual labels:  raspberry-pi, raspberrypi, gpio
Rpisurv
Raspberry Pi surveillance
Stars: ✭ 293 (+58.38%)
Mutual labels:  raspberry-pi, raspberrypi, raspberry-pi-3
Raspap Webgui
Simple wireless AP setup & management for Debian-based devices
Stars: ✭ 3,383 (+1728.65%)
Mutual labels:  raspberry-pi, raspberrypi, rpi
Debian Pi Aarch64
This is the first 64-bit system in the world to support all Raspberry Pi 64-bit hardware!!! (Include: PI400,4B,3B+,3B,3A+,Zero2W)
Stars: ✭ 2,505 (+1254.05%)
Mutual labels:  raspberry-pi, raspberrypi, raspberry-pi-3
Cattlepi
effortlessly boot, configure, update and monitor your raspberry pi ☁️
Stars: ✭ 250 (+35.14%)
Mutual labels:  raspberry-pi, raspberrypi, rpi
Linux-System-Info-Webpage
Material Design Dashboard for Linux System Info. Great for RPi and other linux Distros
Stars: ✭ 19 (-89.73%)
Mutual labels:  rpi, raspberrypi, raspberry-pi-3
Openvoiceos
OpenVoiceOS is a minimalistic linux OS bringing the open source voice assistant Mycroft A.I. to embbeded, low-spec headless and/or small (touch)screen devices.
Stars: ✭ 64 (-65.41%)
Mutual labels:  raspberry-pi, raspberry-pi-3, rpi
Crankshaft
Crankshaft: A turnkey GNU/Linux solution that transforms a Raspberry Pi to an Android Auto head unit.
Stars: ✭ 1,703 (+820.54%)
Mutual labels:  raspberry-pi, raspberrypi, rpi

rpi_gpio v0.5.0

Ruby conversion of RPi.GPIO Python module

Features

Manipulate your Raspberry Pi's GPIO pins from Ruby!

  • Boolean input/output
  • Software-driven PWM (written in C for speed)
  • Event-driven input (blocking and non-blocking)

Up-to-date with RPi.GPIO Python module version 0.7.0, so it works on all Raspberry Pi models!

Sample Usage

I aimed to make the gem's usage exactly the same as its Python counterpart -- only with a few semantic differences to utilize Ruby's readability. If anything is confusing, you can always check here for the original Python module's documentation.

Download the gem

The easiest way to download the gem is to use Bundler with a Gemfile. In your Gemfile, include the line

gem 'rpi_gpio'

Then you can run bundle install to automatically download and compile the gem for your system. To include the gem in a Ruby file, use the line require 'rpi_gpio'.

Pin numbering

Before you can do anything with the GPIO pins, you need to specify how you want to number them.

RPi::GPIO.set_numbering :board
# or
RPi::GPIO.set_numbering :bcm

:board numbering refers to the physical pin numbers on the Pi, whereas :bcm numbering refers to the Broadcom SOC channel numbering. Note that :bcm numbering differs between Pi models, while :board numbering does not.

Input

To receive input from a GPIO pin, you must first initialize it as an input pin:

RPi::GPIO.setup PIN_NUM, :as => :input
# or
RPi::GPIO.setup [PIN1_NUM, PIN2_NUM, ...], :as => :input

The pin number will differ based on your selected numbering system and which pin you want to use.

You can use the additional hash argument :pull to apply a pull-up or pull-down resistor to the input pin like so:

RPi::GPIO.setup PIN_NUM, :as => :input, :pull => :down
# or
RPi::GPIO.setup PIN_NUM, :as => :input, :pull => :up
# or (not necessary; :off is the default value)
RPi::GPIO.setup PIN_NUM, :as => :input, :pull => :off

Now you can use the calls

RPi::GPIO.high? PIN_NUM
RPi::GPIO.low? PIN_NUM

to receive either true or false.

If you prefer to use a callback when a pin edge is detected, you can use the watch method:

RPi::GPIO.watch PIN_NUM, :on => :rising do |pin, value| # :on supports :rising, :falling, and :both
  ...
end

watch also supports the optional bounce_time parameter found in the Python module to prevent duplicate events from firing:

RPi::GPIO.watch PIN_NUM, :on => :falling, :bounce_time => 200 do |pin, value|
  ...
end

To stop watching a pin, use stop_watching:

RPi::GPIO.stop_watching PIN_NUM

If you want to block execution until a pin edge is detected, there's wait_for_edge:

puts 'Waiting to start...'
RPi::GPIO.wait_for_edge PIN_NUM, :rising # :rising, :falling, and :both are also supported here
puts 'Here we go!'

wait_for_edge accepts optional bounce_time and timeout arguments too:

puts 'Waiting to start...'
value = RPi::GPIO.wait_for_edge PIN_NUM, :falling, :bounce_time => 200, :timeout => 5000
if value.nil? # nil is returned if the timeout is reached
  print 'You took too long. '
end
puts 'Here we go!'

Output

To send output to a GPIO pin, you must first initialize it as an output pin:

RPi::GPIO.setup PIN_NUM, :as => :output
# or
RPi::GPIO.setup [PIN1_NUM, PIN2_NUM, ...], :as => :output

Now you can use the calls

RPi::GPIO.set_high PIN_NUM
RPi::GPIO.set_low PIN_NUM

to set the pin either high or low.

You can use the additional hash argument :initialize to set the pin's initial state like so:

RPi::GPIO.setup PIN_NUM, :as => :output, :initialize => :high
# or
RPi::GPIO.setup PIN_NUM, :as => :output, :initialize => :low

PWM (pulse-width modulation)

Pulse-width modulation is a useful tool for controlling things like LED brightness or motor speed. To utilize PWM, first create a PWM object for an output pin.

pwm = RPi::GPIO::PWM.new(PIN_NUM, PWM_FREQ)

The PWM_FREQ is a value in hertz that specifies the amount of pulse cycles per second.

Now you can call the following method to start PWM:

pwm.start DUTY_CYCLE

DUTY_CYCLE is a value from 0.0 to 100.0 indicating the percent of the time that the signal will be high.

Once running, you can get/set the PWM duty cycle with

pwm.duty_cycle # get
pwm.duty_cycle = NEW_DUTY_CYCLE # set

get/set the PWM frequency with

pwm.frequency # get
pwm.frequency = NEW_FREQUENCY # set

and get the PWM GPIO number with

pwm.gpio

Note that this number corresponds to :bcm numbering of the GPIO pins, so it will be different than pin number you used if you created the PWM with :board numbering.

To stop PWM, use

pwm.stop

To check if a PWM object is currently running, use

pwm.running?

Cleaning up

After your program is finished using the GPIO pins, it's a good idea to release them so other programs can use them later. Simply call

RPi::GPIO.clean_up PIN_NUM

to release a specific pin, or

RPi::GPIO.clean_up

to release all allocated pins.

Alternatively, you can call

RPi::GPIO.reset

to clean up all pins and to also reset the selected numbering mode.

Credits

Original Python code by Ben Croston modified for Ruby by Nick Lowery

Copyright (c) 2014-2020 Nick Lowery

View LICENSE for full license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].