Welcome back, evil geniuses in training. In my last tutorial I taught you how to make your C.H.I.P. into a mindless minion, blinking an LED until you say stop...or 100 seconds elapses. But we evil geniuses crave power and control, not just mindless obedience, so today we'll modify our project so that your LED answers to you...with a button. That's right, when you press the button, your LED will bend to your will and light up. Let's get started. In addition to your CHIP and LED from the last tutorial, you'll need a breadboard, button, and some jumper wires. If you've ever owned an Arduino starter kit, there's a good chance you have all of these already.
Let's start by hooking our button up to our CHIP. The digital IO pins on the CHIP default to a value of 1 (or high), so to change the value of that pin we need to connect it to ground (or low). Place your momentary switch somewhere on your breadboard spanning the break and connect one corner of it to a GND pin on the CHIP (I chose the one closest to the micro-USB port). Now connect the diagonal corner of the switch to pin XIO-P1 on your CHIP. Sound confusing? Take a look at the breadboard diagram below. If you have everything hooked up correctly (you can ignore the LED for now, if you'd like), then when the button is pressed pin XIO-P1 (pin 1 in our script library) will connect to ground, thus changing the value of that pin.
Now whenever I press this button the value of GPIO1 will change from 1 to 0. Don't believe me? Let's try it out.
$ git clone firstname.lastname@example.org:b2fcec3817ea5d85288f.git chip-bash $ source chip-bash/gpio.sh $ gpio_enable 1 $ gpio_mode 1 in $ gpio_read 1 1 $ gpio_read 1 # This time holding the button down 0
So what did I just do there? I cloned the BASH functions used in the previous tutorial and sourced them into my environment so that I could use them. Next I enabled pin 1 and set its mode to input. I then used gpio_read to get the value of the pin, which is 1 by default. Lastly, while holding down the button, I read the value again and got back 0. Success, we're now reading a button from pin 1! It's time to buy our evil genius volcanic base of operations.
But wait, I promised you a button that would light up an LED. To do that let's finish hooking up our circuit. As before I've hooked up my LED such that the long lead (anode) is connected to one of the 3.3V source pins and the short (cathode) is connected to pin XIO-P0 (pin 0). I chose to move it to my breadboard to give me more space, but you could also leave it directly connected as before. Once again, should we use a resistor? Probably, but when dealing with this little power the odds of burning something out round roughly down to zero (much like the lottery). Believe me, I hooked it up wrong several times in the course of writing this evil experiment and each time my CHIP turned off and had no trouble booting back up once I'd fixed my mistake. Of course, I take no responsibility if you take my bad advice and burn out your board or LED...I am evil after all.
Now that everything's hooked up, let's write another loop that reads from our input pin and writes its value to our output pin. The result of this will be that the LED will be off when the button isn't pressed and on when it is. The loop below will run forever, so just press ctrl-c to kill it (you may need to press it a few times).
$ gpio_enable 0 $ gpio_mode 0 out $ while true ; do VAL=$(gpio_read 1) ; gpio_write 0 $VAL ; done # Press ctrl-c to kill this infinite loop
If all went to plan, your LED will now be turning on and off with the push of a button. What exactly is that loop doing? Well,
while true tells it to run forever or until interrupted with ctrl-c. The
VAL=$(gpio_read 1) reads the value of pin 1 into a temporary variable VAL. And then
gpio_write 0 $VAL writes the value we just saved to pin 0. Yes, there's more succinct ways to write that, but I hope this is less confusing to those of you new to BASH scripting.
There you have it, you're now one step closer to your goal of total world domination. You can both build a mindless machine for blinking an LED or a computer that bends to your will and lights an LED only when a button is pressed. And all of this with a $9 computer and BASH.
CHIP GPIO functions for BASH.
Did you replicate this project? Share it!I made one
Love this project? Think it could be improved? Tell us what you think!