rpural
Posts: 30
Joined: Fri Feb 24, 2012 2:30 pm
Location: Minnesota

GPIO and a lowly switch...

Thu Jul 26, 2012 8:42 pm

I thought I was getting a handle on the whole GPIO thing, starting off with the two articles in the MagPi magazine and trying to apply and extend the logic from there, but I'm off base somewhere, and need someone with some electronics experience to bump me back on track...

I get that the LED configuration in the magazine is kind of backwards, with the GPIO pin "pushing against" the 3V3 pin, so that when you set the GPIO pin to False, the light comes on, and setting it to True turns the light off. Seems backward to me, and I don't know why they did it that way; they didn't explain why they chose that method. The straight-forward method does work, hooking the GPIO pin to the LED, to the resistor, to the GND, and True is on, False is off. So experiments one and two have paid off.

Experiment three: I didn't have a switch; Nothing I could find had legs that would fit in a breadboard configuration, and I hadn't gotten around to soldering anything up as yet, so I set my mind to the task of replacing the switch in the MagPi article and came up with... Photo sensor! Again, works like a champ, although it works backward from what I expected. If I cover it up, my LED start blinking, all under program control. Take my hand off to light it up, and it stops blinking. So... Either the photo sensor works backward from how I thought, or the switch in the article works backward from how I thought. Could be either; I'm not an electrician.

Then I pulled out the soldering iron and set to creating a breadboard-able switch. So much the same as the one in the article, it has four pins, with the two on the left passing through and the two on the right passing through. Pressing the switch connects all four contacts across the center. This is where things fall apart.

No matter how I hook this switch up, either like the MagPi article, or in the most simple of configurations, the program behaves as if it always sees the switch as pressed. The simplest circuit I've tried is GPIO pin to switch to GND. Actually, the simplest is GPIO pin hooked to nothing at all. The program acts like the pin is always True, no matter what I do.

A photo of the breadboard just before the last switch can be found here: http://www.facebook.com/photo.php?fbid= ... =1&theater

Could someone out there help me explain this anomaly, so I can sleep at night again? I can provide additional photos, and a copy of the code I'm running follows:

Code: Select all

pi@rpural:~/python$ cat ledbackground.py
#!/usr/bin/python

import time
import random
import thread
import RPi.GPIO as GPIO

go1 = 12
go2 = 11
go3 = 13

gi1 = 15
gi2 = 16

mindelay = .01
maxdelay = .25

runtime = 15

timerlock = thread.allocate_lock()

def blinker(gp, final):
	while timerlock.locked(): 
		state = random.choice((True, False,))
		wait = random.random() * maxdelay + mindelay
		GPIO.output(gp, state)
		time.sleep(wait)
	else:
		GPIO.output(gp, final)

def runner():
	while timerlock.locked():
		GPIO.output(go3, True)
		time.sleep(.15)
		GPIO.output(go2, False)
		GPIO.output(go3, False)
		time.sleep(.15)
		GPIO.output(go1, False)
		GPIO.output(go2, True)
		time.sleep(.15)
		GPIO.output(go1, True)
		time.sleep(.5)

GPIO.setup(go1, GPIO.OUT)
GPIO.setup(go2, GPIO.OUT)
GPIO.setup(go3, GPIO.OUT)

GPIO.setup(gi1, GPIO.IN)
GPIO.setup(gi2, GPIO.IN)

GPIO.output(go1, True)
GPIO.output(go2, False)
GPIO.output(go3, False)
time.sleep(.5)
GPIO.output(go2, True)
time.sleep(.5)
GPIO.output(go2, False)
time.sleep(.5)
GPIO.output(go2, True)

while True:
	if GPIO.input(gi1):
		timerlock.acquire()
		thread.start_new_thread(blinker, (go1, True,))
		thread.start_new_thread(blinker, (go2, True,))
		thread.start_new_thread(blinker, (go3, False,))
		time.sleep(runtime)
		timerlock.release()
	if GPIO.input(gi2):
		timerlock.acquire()
		thread.start_new_thread(runner, ())
		time.sleep(runtime)
		timerlock.release()
	time.sleep(1)

pi@rpural:~/python$
--
Bob Nix
nix@earthling.net

User avatar
jbeale
Posts: 3902
Joined: Tue Nov 22, 2011 11:51 pm

Re: GPIO and a lowly switch...

Thu Jul 26, 2012 11:22 pm

I don't see the switch in the photo, but I assume this is a small square switch with four legs that fit into the breadboard like a DIP package, something like this: http://www.webpages.ttu.edu/mhelm/Digit ... Switch.jpg

Are you sure that you have the switch plugged in the right way? If it is plugged in 90 degrees from the correct way, it will still fit in the breadboard, but the switch will be shorted (on each side) by the breadboard strips and it will behave as you said, always connected.

The usual way to use a switch as input directly is to wire a resistor, say 2k ohms from +3V3 to the switch, and the other side of the switch going to ground, so the resistor gets connected to ground when the button is pressed.

+3V3 ----2k_Resistor----- N1 ----SWITCH----GND.

In this drawing 'N1' is just a label for a node or circuit connection which goes to the GPIO input pin. The voltage at that node N1 is 3V3 (when not pressed ) and about 0V when the switch is pressed (about 1.6 mA current flows through the resistor, if it's 2 k). The GPIO input should register 1 when the button is not pressed, and 0 when pressed.

The value of the resistor is not critical, 1k or 10k or 100k would work as well. A very low value resistor, eg 100 ohms would draw 33 mA and start to heat up. A very high value, like 1M would be more susceptible to noise, static pickup, humidity etc.

rpural
Posts: 30
Joined: Fri Feb 24, 2012 2:30 pm
Location: Minnesota

Re: GPIO and a lowly switch...

Fri Jul 27, 2012 2:49 am

I haven't been able to find a switch like your photo, but I soldered leads on to a small switch similar to this one. I've tested it with a meter, and I'm fairly sure I have it mounted correctly (i.e. not 90 degrees out of phase). I'll post a photo tomorrow, and try a few more things, per your notes.
--
Bob Nix
nix@earthling.net

User avatar
Grumpy Mike
Posts: 1005
Joined: Sat Sep 10, 2011 7:49 pm
Location: English Lake District

Re: GPIO and a lowly switch...

Fri Jul 27, 2012 8:38 am

I get that the LED configuration in the magazine is kind of backwards, with the GPIO pin "pushing against" the 3V3 pin, so that when you set the GPIO pin to False, the light comes on, and setting it to True turns the light off. Seems backward to me, and I don't know why they did it that way; they didn't explain why they chose that method.
This is known as sinking current and is more normal that what a beginner might think which is known as sourcing the current. It is more normal to sink current because traditionally logic pins have more current sink capability than source capability. It is also considered safer in the event of a short.

rpural
Posts: 30
Joined: Fri Feb 24, 2012 2:30 pm
Location: Minnesota

Re: GPIO and a lowly switch...

Fri Jul 27, 2012 7:59 pm

jbeale wrote:+3V3 ----2k_Resistor----- N1 ----SWITCH----GND.

In this drawing 'N1' is just a label for a node or circuit connection which goes to the GPIO input pin. The voltage at that node N1 is 3V3 (when not pressed ) and about 0V when the switch is pressed (about 1.6 mA current flows through the resistor, if it's 2 k). The GPIO input should register 1 when the button is not pressed, and 0 when pressed.

The value of the resistor is not critical, 1k or 10k or 100k would work as well. A very low value resistor, eg 100 ohms would draw 33 mA and start to heat up. A very high value, like 1M would be more susceptible to noise, static pickup, humidity etc.
Having the right concept is everything. :D Per your description, I wired 3V3 -> resistor -> leg of the switch -> GPIO pin, and from the other leg of the switch to GND. Worked the first time tried. The test in Python was "if not GPIO.input(gi2):".
Attachments
IMG_0330.JPG
Breadboard w/ switch, photo sensor and lights
IMG_0330.JPG (36.18 KiB) Viewed 3155 times
--
Bob Nix
nix@earthling.net

User avatar
jbeale
Posts: 3902
Joined: Tue Nov 22, 2011 11:51 pm

Re: GPIO and a lowly switch...

Fri Jul 27, 2012 9:53 pm

great, glad you got it working! Just FYI that same body style of pushbutton in your photo is also sold with through-hole legs which you could use without having to solder the wires, but I guess you got some soldering practice in as well :-)

for example,
http://www.ebay.com/itm/10-Pcs-6mm-x-6m ... 0830702275

Return to “Interfacing (DSI, CSI, I2C, etc.)”