{"id":14978062,"url":"https://github.com/clockvapor/rpi_gpio","last_synced_at":"2025-04-04T22:07:59.278Z","repository":{"id":25232094,"uuid":"28656574","full_name":"ClockVapor/rpi_gpio","owner":"ClockVapor","description":"Ruby conversion of RPi.GPIO Python module","archived":false,"fork":false,"pushed_at":"2021-11-28T10:29:26.000Z","size":236,"stargazers_count":221,"open_issues_count":12,"forks_count":32,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-04-04T22:07:55.512Z","etag":null,"topics":["gpio","gpio-pins","pwm","raspberry-pi","raspberry-pi-3","raspberry-pi-4","raspberrypi","rpi","rpi-gpio","ruby"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/rpi_gpio","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ClockVapor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-31T05:04:56.000Z","updated_at":"2025-02-20T15:43:48.000Z","dependencies_parsed_at":"2022-08-21T01:20:45.738Z","dependency_job_id":null,"html_url":"https://github.com/ClockVapor/rpi_gpio","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClockVapor%2Frpi_gpio","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClockVapor%2Frpi_gpio/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClockVapor%2Frpi_gpio/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClockVapor%2Frpi_gpio/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ClockVapor","download_url":"https://codeload.github.com/ClockVapor/rpi_gpio/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247256112,"owners_count":20909240,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gpio","gpio-pins","pwm","raspberry-pi","raspberry-pi-3","raspberry-pi-4","raspberrypi","rpi","rpi-gpio","ruby"],"created_at":"2024-09-24T13:56:48.452Z","updated_at":"2025-04-04T22:07:59.258Z","avatar_url":"https://github.com/ClockVapor.png","language":"C","readme":"# rpi_gpio v0.5.0\n\nRuby conversion of [RPi.GPIO Python module](https://pypi.python.org/pypi/RPi.GPIO)\n\n## Features\n\nManipulate your Raspberry Pi's GPIO pins from Ruby!\n\n- Boolean input/output\n- Software-driven PWM (written in C for speed)\n- Event-driven input (blocking and non-blocking)\n\nUp-to-date with RPi.GPIO Python module version 0.7.0, so it works on all Raspberry Pi models!\n\n## Sample Usage\n\nI 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](http://sourceforge.net/p/raspberry-gpio-python/wiki/Examples/) for the original Python module's documentation.\n\n#### Download the gem\n\nThe easiest way to download the gem is to use [Bundler](http://bundler.io/) with a Gemfile. In your Gemfile, include the line \n```ruby\ngem 'rpi_gpio'\n```\nThen 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'`.\n\n#### Pin numbering\n\nBefore you can do anything with the GPIO pins, you need to specify how you want to number them.\n```ruby\nRPi::GPIO.set_numbering :board\n# or\nRPi::GPIO.set_numbering :bcm\n````\n`: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.\n\n#### Input\n\nTo receive input from a GPIO pin, you must first initialize it as an input pin:\n```ruby\nRPi::GPIO.setup PIN_NUM, :as =\u003e :input\n# or\nRPi::GPIO.setup [PIN1_NUM, PIN2_NUM, ...], :as =\u003e :input\n```\nThe pin number will differ based on your selected numbering system and which pin you want to use.\n\nYou can use the additional hash argument `:pull` to apply a pull-up or pull-down resistor to the input pin like so:\n```ruby\nRPi::GPIO.setup PIN_NUM, :as =\u003e :input, :pull =\u003e :down\n# or\nRPi::GPIO.setup PIN_NUM, :as =\u003e :input, :pull =\u003e :up\n# or (not necessary; :off is the default value)\nRPi::GPIO.setup PIN_NUM, :as =\u003e :input, :pull =\u003e :off\n```\n\nNow you can use the calls\n```ruby\nRPi::GPIO.high? PIN_NUM\nRPi::GPIO.low? PIN_NUM\n```\nto receive either `true` or `false`.\n\nIf you prefer to use a callback when a pin edge is detected, you can use the `watch` method:\n```ruby\nRPi::GPIO.watch PIN_NUM, :on =\u003e :rising do |pin, value| # :on supports :rising, :falling, and :both\n  ...\nend\n```\n\n`watch` also supports the optional `bounce_time` parameter found in the Python module to prevent duplicate events from firing:\n```ruby\nRPi::GPIO.watch PIN_NUM, :on =\u003e :falling, :bounce_time =\u003e 200 do |pin, value|\n  ...\nend\n```\n\nTo stop watching a pin, use `stop_watching`:\n```ruby\nRPi::GPIO.stop_watching PIN_NUM\n```\n\nIf you want to block execution until a pin edge is detected, there's `wait_for_edge`:\n```ruby\nputs 'Waiting to start...'\nRPi::GPIO.wait_for_edge PIN_NUM, :rising # :rising, :falling, and :both are also supported here\nputs 'Here we go!'\n```\n\n`wait_for_edge` accepts optional `bounce_time` and `timeout` arguments too:\n```ruby\nputs 'Waiting to start...'\nvalue = RPi::GPIO.wait_for_edge PIN_NUM, :falling, :bounce_time =\u003e 200, :timeout =\u003e 5000\nif value.nil? # nil is returned if the timeout is reached\n  print 'You took too long. '\nend\nputs 'Here we go!'\n```\n\n#### Output\n\nTo send output to a GPIO pin, you must first initialize it as an output pin:\n```ruby\nRPi::GPIO.setup PIN_NUM, :as =\u003e :output\n# or\nRPi::GPIO.setup [PIN1_NUM, PIN2_NUM, ...], :as =\u003e :output\n```\nNow you can use the calls\n```ruby\nRPi::GPIO.set_high PIN_NUM\nRPi::GPIO.set_low PIN_NUM\n```\nto set the pin either high or low.\n\nYou can use the additional hash argument `:initialize` to set the pin's initial state like so:\n```ruby\nRPi::GPIO.setup PIN_NUM, :as =\u003e :output, :initialize =\u003e :high\n# or\nRPi::GPIO.setup PIN_NUM, :as =\u003e :output, :initialize =\u003e :low\n```\n\n#### PWM (pulse-width modulation)\n\nPulse-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](#output).\n```ruby\npwm = RPi::GPIO::PWM.new(PIN_NUM, PWM_FREQ)\n```\nThe `PWM_FREQ` is a value in hertz that specifies the amount of pulse cycles per second.\n\nNow you can call the following method to start PWM:\n```ruby\npwm.start DUTY_CYCLE\n```\n`DUTY_CYCLE` is a value from `0.0` to `100.0` indicating the percent of the time that the signal will be high.\n\nOnce running, you can get/set the PWM duty cycle with\n```ruby\npwm.duty_cycle # get\npwm.duty_cycle = NEW_DUTY_CYCLE # set\n```\nget/set the PWM frequency with\n```ruby\npwm.frequency # get\npwm.frequency = NEW_FREQUENCY # set\n```\nand get the PWM GPIO number with\n```ruby\npwm.gpio\n```\nNote 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.\n\nTo stop PWM, use\n```ruby\npwm.stop\n```\n\nTo check if a PWM object is currently running, use\n```ruby\npwm.running?\n```\n\n#### Cleaning up\n\nAfter 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\n```ruby\nRPi::GPIO.clean_up PIN_NUM\n```\nto release a specific pin, or\n```ruby\nRPi::GPIO.clean_up\n```\nto release all allocated pins.\n\nAlternatively, you can call\n```ruby\nRPi::GPIO.reset\n```\nto clean up all pins and to also reset the selected numbering mode.\n\n## Credits\n\nOriginal Python code by Ben Croston modified for Ruby by Nick Lowery\n\nCopyright (c) 2014-2020 [Nick Lowery](https://github.com/ClockVapor)\n\nView LICENSE for full license.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclockvapor%2Frpi_gpio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclockvapor%2Frpi_gpio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclockvapor%2Frpi_gpio/lists"}