dboy1612
Posts: 10
Joined: Mon Jan 20, 2014 1:55 pm

Attach Big Red Button to GPIO

Thu Jul 09, 2015 3:25 pm

I recently picked up four of these from Adafruit;
https://www.adafruit.com/products/473

At the moment I'd like to connect just one directly to the GPIO on my RPi B (no breadboard). How would I go about doing this correctly? I have the button wired up and am trying to access using Node and https://github.com/fivdi/onoff and using the latest Rasbian.

I'm fairly new to messing with the GPIOs, at the moment I have one end on 3.3v and the other on GPIO 4. Code is dead simple, but am not receiving anything. What am I doing wrong? Thanks!

Code: Select all

var Gpio = require('onoff').Gpio;
var button = new Gpio(4, 'in', 'both');

console.log('Starting Watch for Button');
button.watch(function (err, value) {
  if (err) {
    console.log('ERROR: ' + error)
  }
  console.log('BUTTON PRESSED!');
});

P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Re: Attach Big Red Button to GPIO

Thu Jul 09, 2015 8:29 pm

I don't know enough about software to comment on your code, but I'd add a reasonably high value resistor (say 10k) between the gpio pin and 0v so it looks like:
3.3v ....switch....gpio....resistor....0v
This makes sure the gpio pin stays properly low when the switch isn't pressed.

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Attach Big Red Button to GPIO

Thu Jul 09, 2015 8:34 pm

P_Monty wrote:I don't know enough about software to comment on your code, but I'd add a reasonably high value resistor (say 10k) between the gpio pin and 0v so it looks like:
3.3v ....switch....gpio....resistor....0v
This makes sure the gpio pin stays properly low when the switch isn't pressed.
This. Or set the internal pull-down resistor. No idea how to do that in Nod.

And put something like a 1K resistor between the switch and the gpio pin, just to protect the gpio if it gets set to output for any unexpected reason. gpio as output set to 0V and button pressed connected to 3.3V = dead gpio.

fivdi
Posts: 208
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: Attach Big Red Button to GPIO

Thu Jul 09, 2015 9:02 pm

GPIOs on the Raspberry Pi have their internal pull-up or pull-down resistor activated. The full details can be seen in Table 6-31 on pages 102 and 103 of this document: http://www.farnell.com/datasheets/1521578.pdf.

In this case, GPIO 4 is being used and it has it's internal pull-up resistor activated. This means that when the button is not pressed, the GPIO will have the value HIGH. When the button is pressed, GPIO 4 will be connected to 3.3V and still have the value HIGH. In other words, nothing happens when the button is pressed.

Using an external 10K resistor as suggested above will solve the problem. Alternatively, you could try one of the GPIOs that have their internal pull-down resistors activated, for example GPIO 17. This is very likely to give poor results though, using an external 10K pull-down resistor is a better idea.

The program being used will print "BUTTON PRESSED" every time the button is pressed or released. Something like this may be better:

Code: Select all

var Gpio = require('onoff').Gpio;
var button = new Gpio(4, 'in', 'both');

console.log('Starting Watch for Button');
button.watch(function (err, value) {
  if (err) {
    console.log('ERROR: ' + error)
  }
  if (value === 0) {
    console.log('BUTTON PRESSED!');
  else {
    console.log('BUTTON RELEASED!');
  }
});

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Attach Big Red Button to GPIO

Thu Jul 09, 2015 9:07 pm

fivdi wrote:GPIOs on the Raspberry Pi have their internal pull-up or pull-down resistor activated. The full details can be seen in Table 6-31 on pages 102 and 103 of this document: http://www.farnell.com/datasheets/1521578.pdf.
...
That is the situation immediately after power-up.

Subsequently the gpios may individually have pull-ups/pull-downs/or no pulls set by software (e.g. device tree, kernel modules, user programs).

fivdi
Posts: 208
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: Attach Big Red Button to GPIO

Thu Jul 09, 2015 9:13 pm

@joan: good point.

Return to “Troubleshooting”