Link Search Menu Expand Document

Introduction to Arduino

Prelab due 9/11/22 at 11:00 am

Writeup due 9/18/22 at 11:00 am

About

This lab introduces you to the Arduino platform and to working with basic circuits. By the end of this lab, you will have been introduced to the Arduino IDE and some basic Arduino programming. You will also have used a circuit diagram to wire up some circuits with LEDs, resistors, and push buttons.

Lab 1 Rubric

Resources

Materials

Included in your kits:

  • Arduino MKR 1000 and micro-USB cable
  • Breadboard
  • 3 LEDs (any colors)
  • 5 resistors (to be determined by you during the lab)
  • 2 push buttons
  • Jumpers/wires

Provided:

  • None necessary

Steps

  1. Install or open the Arduino IDE:

    Install the Arduino IDE.

    When the IDE is installed, open it. Under the Board Manager menu, make sure Board reads Arduino MKR1000. If not, use the menu to select it. You might have to use the Boards Manager link in the menu to install the Arduino SAMD Boards core. More information on the board manager is here.

  2. Load your first program onto the Arduino:

    1. Under the File > Examples menu in your IDE, open 01. Basics > Blink. This opens an example Sketch (Arduino program) called Blink. Take a moment to read through the code, including the documentation at the top, with your partners.

    2. Connect your Arduino to a USB drive on your computer using a micro USB cable. You can keep the Arduino pins in the foam it came with for this step (some students have found that they needed to take the Arduino out of the foam)

    3. Use the upload button to upload the Sketch to the board. Instructions on how to use the Arduino IDE to upload are here.

      Troubleshooting: if you get an error about a device not found on a port, try selecting a different port in the Tools > Port menu. If you continue to have connectivity support, please try the steps on this page

    4. Observe the on-board LED (near the 5V pin) blinking, with the LED toggling between ON and OFF every 1 second.

  3. Edit the sketch to make the LED blink twice as slowly. Upload the code and verify that the LED is blinking slower.

  4. Edit the sketch to make the LED gradually change from blinking slowly, to blinking quickly, and back:

    1. Reference the Arduino Language Reference on Structure for the syntax of for loops, if statements, etc.

    2. Observe that the loop() function of the sketch will repeat forever. Instead of making a for loop inside this function, use global variable(s) that you initialize in the setup() function and manipulate in the loop() function, such that you only call digitalWrite(...) twice during each iteration of the loop() function.

    3. The delay between toggling the LED on and off should change from 2000 ms to 100 ms and back to 2000 ms in increments of 100ms. Concretely, the LED should be on for 2000 ms, then off for 2000 ms, then on for 1900 ms, then off for 1900ms, then on for 1800 ms, then off for 1800 ms, and so on, counting down to 100 ms, and then counting back up to 2000 ms.

    4. Upload, observe, and debug your code. When you are confident that it works, get checked off by a TA.

  5. Run the same code using an external LED, by connecting your first circuit on the breadboard:

    1. You will connect a physical LED to the MKR1000. Study the pin labels on your Arduino. Some have special roles, such as GND (ground pin), VIN (input voltage, if you were, for example, supplying battery power), and TX and TR (for serial communication). There are also 7 analog pins (A0-A6) and 8 pins just for digital I/O (simply labeled 0-7). Because an LED requires a digital (on/off) signal, we will be using one of the digital I/O pins, namely 4.

    2. Before wiring up your circuit, it is good practice to connect the ground rails to ground. From the prelab, you learned how a breadboard is connected internally. Thus, to connect both ground rails to the ground of the Arduino, you should connect the GND pin to one of the rails, and connect the rails to each other.

      Lab01grounding

      When plugging the Arduino into the breadboard, make sure the two rows of Arduino pins are separated by the middle channel of the breadboard. Also make sure that the Arduino is seated firmly in the breadboard (you might have to apply some pressure to the Arduino. To avoid damage, it is good practice to press down on the black plastic pin headers rather than the metal).

      Now, you can ground any circuit by connecting it to any hole on either the top or bottom ground rail!

    3. Disconnect your arduino from power (unplug the cable) and wire up the circuit. Refer to the prelab for a reminder on how the circuit diagram corresponds to the physical circuit. Use the same resistor that you computed in the prelab.

      Lab01Circuit01

      Before powering up the circuit, go through the circuit checklist.

    4. In your code, create a constant global variable for your LED pin, with value 4. Change all appearances of LED_BUILTIN in your code to this variable.

    5. Connect your Arduino to the computer and upload your code, verifying that the LED you added lights up instead of the on-board LED.

      Get checked off by a TA

  6. Practice wiring up a button:

    Lab01Circuit02a

    We will implement the switch switch using a push button. A push button enables a connection between the voltage source (VCC) and the input pin when pressed. To do this, a push button is connected like this (the orange wire is going to pin 5 of the MKR1000, the red wire is connected to VCC, and the black wire is connected to GND):

    button

    Image credit: Brian Carbonette on Arduino Project Hub

    The orientation in which the button is plugged in to the circuit matters. Notice that, on your button, there are two sides that do not have legs attached, and two sides that each have two legs attached. The sides with the legs should sit on either side of the DIP support (the breadboard’s “ditch”).

    Lab01ButtonBreadboard

    1. Wire up your circuit. Make sure the button is connected to VCC, not 5V!!! Do not use the same resistor as in the breadboard graphic above, but instead use the resistor color codes as a reference to find the 10kΩ resistor.

    2. Open the serial monitor by pressing the button at the top of the screen. Some students have had to open the monitor before loading the code, and some students have had to do it after.

    3. Study and run the following skeleton code:

      /*
      * Prints the result of the button press to the serial monitor.
      */
      
      int BUTTON_PIN = 5;
      
      void setup() {
         Serial.begin(9600);
         while (!Serial); // Wait for Serial to initialize
         pinMode(BUTTON_PIN, INPUT);
      }
      
      void loop() {
         Serial.println(digitalRead(BUTTON_PIN));
      }
      
    4. Observe the output change as you press and release the button

  7. Now, implement a binary counter using this circuit:

    Lab01Circuit02

    The resistors connected to each of the LEDs have a resistance of 1 kΩ. The resistors connected to each of the buttons have a resistance of 10 kΩ.

    Remember that crossing lines are only physically connected when there is a solid dot at the junction of the lines. Otherwise, interpret the wires as crossing over each other without being connected.

    1. Each of the 3 LEDs represents a binary digit. The most significant digit is connected to pin 3. As an example, for the LEDs connected to pins 3-5, refer to the LEDs as L2, L1, and L0, respectively. If L1 and L0 are on but L2 is off, this displays 011 and represents 3 in binary. Wire up your circuit and use the checklist to check it.

    2. The push button on pin 7 is to decrement the counter, and the push button on pin 6 is to increment the counter. If the decrement button is pushed, the LED binary counter should decrement by 1. If the decrement button is pushed when the counter is displaying a 0 (all LEDs off, representing binary 000), nothing should happen. Similarly, if the increment button is pushed, the LED binary counter should increment by 1, and if it is pushed when the counter is displaying 7 (all LEDs on, representing binary 111), nothing should happen. Assume that only one button is pushed at a time. Also assume that the counter starts at 0.

    3. Start a new sketch (using Examples > 01. Basics > Bare Minimum gives you skeleton code to start with) and implement the functionality described above. Remember to use pinMode(...) to define pins as inputs or outputs. The input from the buttons can be ready using digitalRead(...).

    4. Upload your sketch to the Arduino and verify if it works. If it does, congrats! Get checked off by the TA, and you are done with the in-class portion of the lab. Otherwise, spend at least 15 minutes debugging with your partner before asking a TA for help. We suggest using Serial.println(…) and Tools > Serial Monitor as in Step 6 to print debug information.

      Hint: your first instinct may be to poll digitalRead(...) for a high signal, making the numbers increment rapidly as long as the button is pushed, rather than checking for the edge where it changes from 0 to 1. How do you detect this change?

  8. Turn in your work:

    1. Save your binary counter sketch as lab01_bincount. Upload this to the “Lab 1 Code” assignment on Gradescope (include all partner(s) on the submission).

    2. INDIVIDUALLY, complete the Lab 1 writeup assignment on Gradescope.