Posted in Arduino | 0 comments

Now that we know the goodies in our kits, let’s start to put them to work. The first thing we’ll do is get a button to light up—the stereotypical first thing to do with an Arduino. It’s good for us, because it will confirm all the parts are working without having a super complex system yet.

Simple Push Button Light

  1. First, unplug the GEMMA from your computer’s USB port, if you have it plugged in.
  2. Then, open the Adafruit Arduino software, if you don’t have it open yet.
  3. Go under File and choose New. A blank code sketch will open.
  4. Copy and paste the following 48 lines of code into your new sketch.
    /*
     Input Pullup Serial
     
     This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a 
     digital input on pin 2 and prints the results to the serial monitor.
     
     The circuit: 
     * Momentary switch attached from pin 2 to ground 
     * Built-in LED on pin 13
     
     Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal 
     20K-ohm resistor is pulled to 5V. This configuration causes the input to 
     read HIGH when the switch is open, and LOW when it is closed. 
     
     created 14 March 2012
     by Scott Fitzgerald
     
     http://www.arduino.cc/en/Tutorial/InputPullupSerial
     
     This example code is in the public domain
     
     */
     
    const int buttonPin = 2;     // the number of the pushbutton pin
    const int ledPin =  1;      // the number of the default LED pin
    
    void setup(){
      //configure pin2 as an input and enable the internal pull-up resistor
      pinMode(buttonPin, INPUT_PULLUP);
      pinMode(ledPin, OUTPUT); 
    
    }
    
    void loop(){
      //read the pushbutton value into a variable
      int sensorVal = digitalRead(buttonPin);
      
      // Keep in mind the pullup means the pushbutton's
      // logic is inverted. It goes HIGH when it's open,
      // and LOW when it's pressed. Turn on ledPin when the 
      // button's pressed, and off when it's not:
      if (sensorVal == HIGH) {
        digitalWrite(ledPin, LOW);
      } 
      else {
        digitalWrite(ledPin, HIGH);
      }
    }
  5. From the Tools menu, choose Board, and then Adafruit Gemmaboard
  6. From the Tools menu again, choose Programmer, and then USPtinyISPprogrammer
  7. verify

    The verify button

    Click the small check mark button in the upper left-hand corner of the sketch window. It will verify that the code is accurate. If not, you will see an error message on the bottom.

  8. Save your sketch somewhere that makes sense on your computer with a good filename.
  9. Now get out the GEMMA, the push button, and two alligator clips (one white and one green).button wiring
  10. Clip the white alligator clip to the D2 pin on the GEMMA.
  11. Clip the other side of the white clip one of the metal feet of the button.

    The dashed line represents the electrical connection between the alligator clips and the button. Notice the current cannot flow when the button is not pressed as there is no connection.

    The dashed line represents the electrical connection between the alligator clips and the button. Notice the current cannot flow when the button is not pressed as there is no connection. DO NOT put both clips on the same side of the button.

  12. Now look at the button. The two of the feet connect in a line. This is one side of the button. The other side has a symmetrical line with the other two feet. When untouched, the button stops electricity from flowing from one side to the other. When you press the button, the circuit is completed and electricity flows.
  13. Clip the green alligator clip to one of the feet on the other side of the button.
  14. Clip the other side of the alligator clip to the GND pin on the GEMMA. This stands for ground.
  15. At this point we have the code written and the circuit complete. It’s time to upload the code to the GEMMA. Plug in the GEMMA to your computer’s USB port.
  16. The green power light should be solid. The red light should be pulsing. This is a sign that you can upload code.
  17. By now, the red light is probably not pulsing any more. That means we cannot upload code. First look where the upload button is on the sketch window—the arrow button. Get ready to click this.
  18. First, press the physical button on the GEMMA to start the pulsing red light, opening the door to upload code.gemma button
  19. upload

    The upload button

    Then, quickly, click the upload button on the sketch window. There should be a message at the bottom of the sketch window that the code is first compiling, and then uploading.compiling

  20. If you are having trouble, try unplugging and re-plugging the GEMMA. While it’s pulsing red, click the upload button.
  21. Once it finishes uploading, the GEMMA runs the code.
  22. This one is simple, press the push button you wired. When it is pushed, the red light on the GEMMA should light up. Release, and it will turn off. Try it a few times.
  23. SUCCESS. You have now learned how to copy and paste code, upload code, wire a circuit, and run a program on a microcontroller. Good work. But now let’s do something more flashy. (ugh, groaner pun)

part2-leftpart4-right
Cover photo by Ali Kholi