Page 1 of 1

Pi edge detection

Posted: Wed May 22, 2013 9:24 pm
by muckypup
I think I am missing something here...

From what I can gather the following should work, but catting value is not blocking and I seem to get a random 0 or 1 even though I have said I am only interested on a rising edge and I have nothing connected to the pin so should be 0 all the time.

# echo "18" > export
# cd gpio18
# echo "rising" > edge
# cat value
1
# cat value
0

Anyone have any idea what I am doing wrong?

Thanks
Steve

Re: Pi edge detection

Posted: Thu May 23, 2013 10:46 am
by gordon@drogon.net
muckypup wrote:I think I am missing something here...

From what I can gather the following should work, but catting value is not blocking and I seem to get a random 0 or 1 even though I have said I am only interested on a rising edge and I have nothing connected to the pin so should be 0 all the time.

# echo "18" > export
# cd gpio18
# echo "rising" > edge
# cat value
1
# cat value
0

Anyone have any idea what I am doing wrong?

Thanks
Steve
Why do you think the pin should read 0 with nothing connected?

It's an easy mistake to make though - the pin are high imedance inputs and will react to lots of wibbly wobbly stuff nearby...

If you want to ensure it reads 0 with nothing connected then either use an external resistor to tie it to 0v, or enable the internal pull-down resistors..

Install wiringPi, then

gpio -g mode 18 down

will enable the internal pull-down resistor.

You can also:

gpio -g wfi 18 rising

which will setup and wait for the rising interrupt on pin BCM_GPIO 18 to save all that bash typing :)

You can test it easilly too - if you have 2 windows open, and in one, wait for the interrupt, then in another: gpio -g mode 18 up

that will change the pull-down into a pull-up resistor and trigger the edge.

http://wiringpi.com/

-Gordon

Re: Pi edge detection

Posted: Thu May 23, 2013 8:42 pm
by muckypup
I think my lack of understanding of electronics is showing... :D

Can the internal pull up and pull down resistors be enabled from the /sys/class/gpio special files?

S.

Re: Pi edge detection

Posted: Thu May 23, 2013 9:02 pm
by gordon@drogon.net
muckypup wrote:I think my lack of understanding of electronics is showing... :D

Can the internal pull up and pull down resistors be enabled from the /sys/class/gpio special files?

S.
Not that I'm aware of. (Although recent kernels may have changed that) Just use the gpio command that's part of wiringPi...

-Gordon

Re: Pi edge detection

Posted: Thu May 23, 2013 9:13 pm
by muckypup
I am trying to set this up for use in c# / mono.

I have found some info and a nice diagram (http://www.cl.cam.ac.uk/projects/raspbe ... _switches/) so will order a few bits off ebay and have a go.

S.

Re: Pi edge detection

Posted: Wed Jun 05, 2013 8:34 pm
by muckypup
With some good advice (and a couple of resistors) I have a nice stable input. However it is still ignoring the 'edge' request. I have set gpioNN/edge to "rising" and read from gpioNN/Value but it still doesn't block giving me 0 all the time unless I have the switch pressed.

I was under the impression that with edge set to "rising", reading from value would block until a rising edge, ie the switch being depressed? Or do I have it wrong?

Steve

Re: Pi edge detection

Posted: Wed Jun 05, 2013 9:56 pm
by jojopi
A read from "value" will always return the current value.

What will block, if edge detect interrupt is enabled and has not occurred, is a poll() or select() for priority/exception condition on the file. (A poll/select for read condition will never block because the "value" file is always readable.)

I do not think there is a good way to do the poll or select in bash. I assume there is a way to do it in Mono, but since the results will not be portable outside Linux/Pi anyway I would recommend using C with wiringPi, or plain C, instead.

Re: Pi edge detection

Posted: Thu Jun 06, 2013 7:18 am
by gordon@drogon.net
jojopi wrote:A read from "value" will always return the current value.

What will block, if edge detect interrupt is enabled and has not occurred, is a poll() or select() for priority/exception condition on the file. (A poll/select for read condition will never block because the "value" file is always readable.)

I do not think there is a good way to do the poll or select in bash. I assume there is a way to do it in Mono, but since the results will not be portable outside Linux/Pi anyway I would recommend using C with wiringPi, or plain C, instead.
The latest version of wiringPi's gpio command can do a blocking operation on a pin (aka wait for interrupt) e.g.

gpio wfi 0 rising

will wait for a rising edge on pin 0 (bcm_gpio 17)

-Gordon

Re: Pi edge detection

Posted: Thu Jun 06, 2013 6:29 pm
by muckypup
Ah.. yes of course! I have not done kernel special files for..err.. lets just say over 20 years. :oops:

A Poll() on a FD looking for POLLPRI. Now how to replicate that in c# is another matter as I have not written C in 15 years. Moved to a managed language so I could concentrate on the solution not the implementation :o

Some digging required methinks.

Thanks for the help.

S.