Git Product home page Git Product logo

raspi-gpio-rb's Introduction

banner

raspi-gpio-rb

๐Ÿ”Œ A simple and light interface to interact with GPIO pins of the Raspberry Pi.

This library uses /sys/class/gpio interface to communicate with GPIO pins. It doesn't require super-user rights, therefore everyone on the OS can use it.

๐Ÿ“Œ Requirements

This library requires an updated version of Ruby, and a Raspberry Pi with Raspbian installed on.

The raspi-gpio library has been tested on models : 3B

๐Ÿ”ง Setup

Gemfile

If you setup the library for medium or big projects, it's recommended to write it in your Gemfile.

gem 'raspi-gpio', github: 'sergio1990/raspi-gpio-rb'

After, use again the install command, but without the package name.

gem install

Build

You can also compile it by yourself. First, clone the repository.

git clone https://github.com/sergio1990/raspi-gpio-rb.git  # HTTP
          [email protected]:sergio1990/raspi-gpio-rb.git      # SSH

Then, build the gemspec file to create the gem.

gem build ./raspi-gpio.gemspec

Finally, install it on your system.

gem install ./raspi-gpio-2.x.x.gem

โŒจ Basic interactions

First of all, we must include the library in our project. That can be achieved really easily with the require keyword.

require 'raspi-gpio'

# ...

Defining a pin

A GPIO pin is defined with three things :

  • a pin number (the number of total pins depend on your Raspberry model - please, visit the pinout guide)
  • a direction (in or out)
  • a value (low or high)

The GPIO class is the way the library provides to register our pins. The initialization method takes 2 arguments :

  • the pin number to use
  • the direction (default : out)

We can use the OUT and IN constants for the direction.

pin = GPIO.new(9, GPIO::OUT)

The direction of a pin can be changed at any point of the code using the mode property.

pin.mode = GPIO::IN

Reading pin's value

To read the pin value, we're going to use the value property.

pin.value

# Example output : 0
  • If the pin direction is IN, it'll read the power that goes into it
  • If it's in OUT, it'll output the value that we defined earlier

Outputing a value

To set the value of a pin, the GPIO class has a value property that can take two values :

  • 0, no power (low)
  • 1, maximum power (high)

The LOW and HIGH constants are here to let us set them while keeping code clarity.

# Imagine that a LED is connected to our pin.

pin.value = GPIO::HIGH

# Output : the LED is on

pin.value = GPIO::LOW

# Output : the LED is off

Cleanup pin

It a good practice to unexport the pin once you're done working with it. For this, just call a cleanup method on the pin's instance:

pin.cleanup

๐Ÿ” License

See the license file.

raspi-gpio-rb's People

Contributors

sergio1990 avatar

Watchers

James Cloos avatar

Forkers

maffooclock

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.