snooty
Posts: 24
Joined: Thu Nov 22, 2012 3:21 am

pi4j momentary switch?

Thu Nov 22, 2012 3:34 am

Hi everyone,

I wasn't too sure where to put this but I thought as it's pi4j related here might be best.

I'm messing around with the examples on the pi4j website in preparation for a project I want to try, the only problem is my programming is alright but I have no clue about electronics. I'm looking at the simple example for listening to the state of a pin from a button push http://pi4j.com/example/listener.html but I don't have this momentary switch to try it out:

Image

All I have at hand is a mini button from the kit I got with the raspberry pi and it won't work for me:

Image

Can anyone please tell me how to get this switch to function with the example or where to buy one that will work (in the uk)?

Thanks.

User avatar
Burngate
Posts: 5967
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: pi4j momentary switch?

Thu Nov 22, 2012 10:50 am

It's not easy to see in your photo, but you appear to have 10k resistors from one end of the switches to +3v3 and 1k resistors from the GPIO end of the switches to ground.
When the switch is closed, they form a potential devider, with 91% of the 3v3 across the 10k and 9% across the 1k.
So closing the switch puts about 0.33v onto the GPIO input

So, if I'm right, swap the 10k and 1k resistors and it should all work

Edit: I've had another look. I'm convinced the resistors from the 3v3 rail are 10k, but now I think the others are 270Ω which makes it worse
Whatever, swap them over

snooty
Posts: 24
Joined: Thu Nov 22, 2012 3:21 am

Re: pi4j momentary switch?

Thu Nov 22, 2012 12:00 pm

ok, I'm gonna have a go again now.

thanks, for the help. much appreciated.

snooty
Posts: 24
Joined: Thu Nov 22, 2012 3:21 am

Re: pi4j momentary switch?

Thu Nov 22, 2012 1:08 pm

So I tried to follow your advice but it still won't work.

I've uploaded some actual images instead of the one from the kit website as the resistors appear to be different, I should've did that at the start.

Album http://imgur.com/a/6gEDN

Image

Image

User avatar
piglet
Posts: 902
Joined: Sat Aug 27, 2011 1:16 pm

Re: pi4j momentary switch?

Thu Nov 22, 2012 2:28 pm

This is pretty similar question to mine here: http://www.raspberrypi.org/phpBB3/viewt ... 63&t=18677

That thread should have all you need.

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: pi4j momentary switch?

Tue Nov 27, 2012 3:28 am

In the Pi4J example, it uses the +3.3V pin connected to any momentary switch and then also connects to one of the GPIO pins. The software uses the internal pull-down resistor supported on the hardware to prevent "floating"

So it looks to me like you could use your push button switch just connected without any additional resistors just like in the Pi4J example.
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

Return to “Other programming languages”