The other day I decided that I needed to get out of my new comfort zone of controlling LEDs over the web (that crap never gets old, trust me.. I toggle those lights all day long, so much so that it annoys my family). My next project was going to involve input. From what I could tell, the easiest form of input is a button. So, I rigged up a button to my breadboard and Raspberry Pi, as shown in the following picture:
I first put the button on the breadboard so that it straddled the empty canyon in the middle, which means each of the sets of legs (I’m sure experts would call them terminals or something cooler than “legs”) are on separate sides of the breadboards. This is important so we don’t allow current to flow between two legs in the same row. I then ran a jumper wire from Pin 24 on the cobbler (you could use Pin 24 right on the GPIO, too) to one of the top terminals on the button. Then I connected the 3V3 power pin from the cobbler to the positive power bus on the breadboard.
I connected one of the button’s terminals to the power bus and the other terminal right above that to ground with a 10K resistor. From what little this software guy knows about how this all works, the button input works by letting us query whether the button is connected to the 3.3V source or to ground. Things get ugly and messy if it is floating somewhere in between, what us software guys would jokingly call a “tri-state boolean”. So, we put a resistor on the terminal to connect the input side of the switch to ground. One lesson I have learned that seems to apply here is that electricity follows the path of least resistance, so when we push down on the button, current will flow to the input pin and not to ground (which is a path of much higher (10K, actually) resistance. The main reason for the resistor is we want all of the current to flow to the input pin, so it detects full voltage and not some fuzzy state in-between ground and 3.3V.
I like to think of resistors as dams that slow or control the flow of water. I realize this could be a stupid analogy, but, I’m a software guy.
Finally I ran a jumper from the cobbler’s GND to the ground rail on the breadboard.
Next it was time to write some Python code that did something useful when I pushed the button. In my case, I wanted to wait for the first detected button press and then send an HTTP request out through the Spark Cloud to my Spark Core, which would tell the spark core to light up … An internet button!
Here’s a rough sketch of the Python I used:
import RPi.GPIO as GPIO import time import os GPIO.setmode(GPIO.BCM) GPIO.setup(24, GPIO.IN) while True: inputValue = GPIO.input(24) time.sleep(0.3) print("Pushed a button!!") os.exit()
The sleep in there was there to keep the poller from detecting multiple presses while the button is held down. Polling for the button state is a kind of fuzzy thing that chafes my deterministic software engineering side, but I know there are better ways to code for this that I’ll likely get to in the future.
So, I now had my code and I executed it on my Raspberry Pi. I pushed my button, the message went out over the cloud, and the lights on my Spark Core lit up!! I squealed with geek joy, and am now plotting my next project: exposing an analog sensor to the cloud.