II. Blinky: The "Hello World" for Microcontrollers

Overview

This guide will walk you through the basic steps needed to program the Photon, to control its built-in blue LED, to wire a new LED, and to make the LEDs blink and change brightness.

Goals

  • Introduce microcontroller programming basics
  • Introduce or review the fundamental Electrical Engineering principles (voltage, current, and circuit safety)
  • Verify your hardware and account setup
  • Introduce digital and analog output

Recommended Preparation

  1. Safety First! The electronics used here are relatively safe, but it’s important to respect electricity.
    • Read All About Circuit’s chapter on Safety
    • Develop proactive lab habits, including:
      • Don’t adjust wiring while a circuit is powered.
      • Ask for help when in doubt and have someone review your work before powering a circuit.
      • Make sure “exposed connections” can’t contact anything conductive, including protective foam used for shipping electronics, antic-static bags used for shipping electronics, metal cases on laptops, metal tables, etc.
      • Electricity is faster than you are and faster than your computer! Don’t connect a circuit if the Photon isn’t configured for it — it could destroy the circuit or the Photon. Always Flash an empty sketch on the Photon before using it for a new project.
  2. Microcontrollers are one of the most common types of computers. Learn what they are.
  3. Review fundamentals of Electronics: Voltage, Current, Resistance, Circuits, and LEDs.

Part 1: Hardware, Programming Setup, and Console Debugging

Project Parts

You will need to get the following parts out of your kit:

  • The Photon
    Photon Box
  • The Breadboard
    Solderless Breadboard
  • The USB Cable
    Micro USB Cable

Assembly

  1. Remove your Photon from its pack and carefully pull the protective foam off its back.
  2. Install your Photon on the breadboard.
    Photon on Breadboard

    Be sure to push it firmly down so it’s flush with the breadboard.

  3. Plug the USB cord to both the Photon and your computer.
  4. Notice that the Photon’s board has a small blue highlight on the pin marked D7:
    Photon's Blue Light

    The electronic part in the center of this dot is a Light Emitting Diode, which, as the name implies, can emit light. The following section will program this light to turn on.

Programming

  1. Open the Particle Web IDE (login with your Particle.io login)
  2. Enter the name blinky for your program:
    Name Sketch
  3. Type in two lines of code (the panel showing the name of the program may disappear when you enter code):
    Code

    The code is:

    pinMode(D7, OUTPUT);
    digitalWrite(D7, HIGH);
    
  4. Make sure your Photon is connected to the cloud. It should be “breathing” (pulsing) cyan.
  5. Click on the “Flash” icon to compile and deploy your code. This could take about a minute:
    Deploy
  6. You should notice that the light comes on. The setup() function runs once when the program first starts. Typically setup() is used to “setup” the Photon by initializing values and configuring hardware. In this case it’s indicating that the pin named D7 is being used as an output and setting the voltage to HIGH (ON) initially, which should turn the light on.
  7. The loop() function is repeatedly called in a loop. Update the loop to include:
    digitalWrite(D7, LOW);
    delay(1000);
    digitalWrite(D7, HIGH);
    delay(1000);
    

Part 2: Circuits, Ohm’s Law, and Fundamental Circuit Constraints

Project Parts

You will need to get the following parts out of your kit:

  • The pack of semiconductors (LEDs in particular)
    Semiconductors (LEDs)
  • The pack of Resistive Elements
    Resistors
  • A few Wires
    Wires

Parts 2: Part Selection

  1. Pick either a red or green LED. Based on the data for them:
    • They both drop the voltage by more than 1.8V
    • They both have a maximum current of 20mA
    • They both have a recommended current of 16-18mA
  2. Pick a resistor.

    There are several constraints that dictate the amount of resistance that should be used with an LED connected to a microcontroller. If there’s too much resistance the LED won’t light (or will be very dim). A small resistance (or no resistance) will cause more current to flow than a high resistance and too much current could:

    Your kit comes with three values of resistor: 330, 1K (i.e., 1,000 ), and 10K (i.e., 10,000)

    Questions
    The Photon’s Output Voltage is 3.3V and the LED will reduce this by 1.8V (or more), leaving a 1.5V potential across the resistor. Use Ohm’s law to answer the following questions:

    1. What will the current be if a very low resistance (i.e., 1 ) is used? Does this exceed the limits of the LED or the Photon?
    2. What will the current be for each of the provided resistance values (330, 1K, and 10K)?
    3. Which value is the best choice here and why? (Why is one value preferable to the others?)
    4. The Photon has seven digital outputs. Assuming that all seven will be used for LEDs and all should be supplied equal current, what’s the minimum resistance that would be safe for both the Photon and the LEDs assuming you can use any resistance value?
    5. Repeat the previous question assuming that you can only use commonly available resistors from this kit.

    Use the resistor color code to pick the proper resistance.

Assembly

  1. Unplug the Photon before changing the circuit

  2. Construct a complete circuit using the breadboard. The current should:
    • Leave a Digital Output Pin, like D0,
    • Go through a resistor (with the value selected above)
    • Go into the Anode of the LED (the longer wire)
    • Go out the Cathode of the LED (the shorter wire)
    • Return to one of the grounds (GND) of the Photon

    The Schematic view is:
    LED Circuit Schematic

    The Breadboard may be wired like this:
    LED Circuit Breadboard

  3. Now plug it back in and update the program to turn on newly added LED. It should look like this:
    LED Circuit

Going Further

  1. Try to make one of the LEDs blink an SOS pattern (…—…).
  2. Use analogWrite() to control the brightness of the LED.
    • Use a for-loop in conjunction with delay() to try to reproduce the “breathing” pattern of the main LED.
  3. Explore the other digital and analog output parts in your kit: Your kit of parts also includes:

ACM Curriculum

Reference

See Particle’s Blink an LED Code Example


Tweet