Monday, March 11, 2013

Raspberry Pi and Button Press Example

Hi there. This post is not going to be on JavaScript but it will be a bit more centered on the Raspberry Pi and what I did today. As pointed out in an old blog post, I'm working on a software that creates streams of data and pass them through a topology. As data sink, I though it would be very nice to have a Raspberry Pi which gathers data from the environment, for example.
Today I started to implement something on the RPi. Nothing serious, but I wanted to test the GPIO. First of all I had to install some Python dependencies to read data from it

$sudo apt-get update
$sudo apt-get install python-dev
$sudo apt-get install python-rpi.gpio

After this, I had to learn a little bit about the GPIO. The General Purpose Input/Output is an interface available on some devices capable of getting inputs and sending outputs. In the RPi, this is the male pins situated on top of the RPi logo in the latest devices.
The following schematics shows which pin corresponds to which usage



Today what I will do is connect a simple button and trigger some event on the RPi. The event will only be a print line saying something. The purpose of this test project was to get my hands dirty a little bit with the RPi by starting with something extremely simple.

Components

- 1 Raspberry Pi
- 1 Ribbon
- 1 Breadboard
- Some wires
- 1 10k Ohm resistor
- 1 Button sensor

How to build

Place the button on the breadboard. On the row of one pin of the button, I had to connect one head of the resistor. The other head had to be connected on another row. On the same row of this head, then, I had to connect the 3v3 pin (pin 1) of the RPi. To do that I had to use the ribbon and then use a wire from the other end of the ribbon.

I have chosen to use the pin called GPIO17 on the image shown. From that pin I connected a wire which was placed on the same row in which one head of the button and one of the resistor were placed. Finally, the ground pin was connected to the other end of the button.

Code

The code was a simple piece of Python that gathered the input from the pin number 17. Here is it:

import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
pin = 17;
gpio.setup(pin, gpio.IN)

while True:
    input = gpio.input(pin)
    if input == False:
        print('button press');
        while input == False:
            input = gpio.input(pin);

Be careful and respect indentation as Python will complain otherwise. I have chosen pin 17 but any GPIO pin will work as long as you change it in your code. The code is extremely simple: with a while True it checks if the button has been pressed. If that is the case, then, it will print the message. While the button is pressed it will just fall into the second while statement.

To run the code, just do this

sudo python filename.py

And that's it. Here is a picture of what I built.


As you can see, the blue wire is the 3v3 pin, the yellow one is the GPIO17 while the black one is ground.
I hope to build some cooler stuff and show it here.

Categories: ,

23 comments:

  1. This post is not going to be on JavaScript but it will be a bit more centered on the Raspberry Pi .As pointed out in an old blog post. As data sink, it would be very nice to have a Raspberry Pi which gathers data from the environment.

    ReplyDelete
  2. 2016 fake rolex watches large pilot version of the replica watches "Little Prince" limited edition IW500916 In 2002, the country from the antique "B-UHR" replica watches to draw inspiration, launched a large size of the large pilot wrist watch IW5002. At that time the basic watch are still 36-40 mm size, 46 mm shape quite shocking

    ReplyDelete
  3. This particular post is really very interesting. It has a really great peace of Data,
    I enjoyed very well with this particular blog it has very useful information
    I like to much the style of this Article
    thats really nice and informative article.Thank You.

    hey,look, i found something very interesting article

    about garden tools and tips,
    best gas grill under 200
    best gas grill under 500
    best lawn edger
    best chaninsaw under 200
    best inflatable water slides
    Deepest inflatable pool

    ReplyDelete
  4. Are you looking for guest posting/blogging sites? then click :
    guest posting sites in india

    ReplyDelete
  5. Are you looking for guest posting/blogging sites? then click :
    guest posting sites in india

    ReplyDelete
  6. Very meaningful & well defined blog. willing to see more like this.

    ReplyDelete
  7. BULK SMS
    Digital marketing
    Connect with The Most Trusted BULKSMS Provider in India. Attractive Price with Guarantee 100% Delivery. Call :9907922122
    Bulk SMS service simply means sending out a large number of SMS for delivery to targeted mobile numbers.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Our bureau is the only area known to provide friendly and esthetical girlfriend (escort service in jaipur) and companion which you can't deny in dreamy state. It is because of the services of the sexy babes are extremely warmth, esthetical and perfect in each and every sense. The instant can always be memorable and lingering. It may be so good a grin will flash on mind memory the experience. They are largely of college going pupils of hi society WHO enjoys to serve them beforehand of fellows.

    call girls in jaipur

    ReplyDelete
  11. Our bureau is the only area known to provide friendly and esthetical girlfriend (escort service in jaipur) and companion which you can't deny in dreamy state. It is because of the services of the sexy babes are extremely warmth, esthetical and perfect in each and every sense. The instant can always be memorable and lingering. It may be so good a grin will flash on mind memory the experience. They are largely of college going pupils of hi society WHO enjoys to serve them beforehand of fellows.

    call girls in jaipur

    ReplyDelete
  12. The Government of India launched the Skill India campaign on 15th of July, 2015. This program comes under the direction of Ministry of Skill Development and Entrepreneurship. Skill India plan was introduced by the government to promote the skill development of the youth of the Indian economy especially the ones coming from the rural sector. This program is working on the certification and employment of the youth of India. The purpose is the creation of skill in youth to strengthen the infrastructure in order to support entrepreneurship development and skill development in the economy through Apprenticeship training programmes.
    electrical apprenticeship vacancies

    ReplyDelete