User Tools

Site Tools


oak:tutorials:blink

This is an old revision of the document!


Oak: blinking the on-board LED

A common first project with a microcontroller is to “blink” an LED. It's very simple, is a good way to get familiar with the basics of writing code, and many boards (including the Oak) include an on-board LED, so no additional components are necessary. A tutorial using additional LEDs can be found here.

Components used

Part Quantity Identification
Oak with soldered headers 1

While we only need an Oak for this lesson, be sure to make sure you have completed the steps in the “Getting Started” section of the tutorials. In particular, ensure that your Oak is associated with particle.io and that your Arduino IDE is properly configured.

Concepts

digitalWrite()

digitalWrite(pin_number, value) is the command used to send a HIGH or LOW voltage to the pin of a microcontroller. In the case of the Oak, pin 1 is connected to a small surface mounted LED on the board. We will be turning the voltage on and off to this pin to turn this LED on and off.

pinMode()

Microcontrollers can often change the functionality of a pin internally by using code. For pins that are meant to “listen,” for example when connected to a sensor, we use pinMode(pin_number, INPUT). When we plan to “send” a signal, we use pinMode(pin_number, OUTPUT). Since we will be sending voltage to the LED, we will use OUTPUT for the mode of pin 1.

delay()

The delay(milliseconds) function is used to take a break from the code that's being run. Code is written in lines, and each line is read and execute one at a time. When the microcontroller encounters the delay() function, it waits for that many milliseconds before moving on to the next line. This is important for timing the blinking of the LED.

Code

This code will make the on-board LED connected to pin 1 blink. It will turn on for 1 second, and then off for one second, over and over:

// comments are inserted by putting two forward
// slashes at the beginning of a line

// the setup() loop sets pinModes, starts things like Serial,
// and is run once when the Oak powers on
void setup()
{                

  // initialize digital pin 1 as an output.
  pinMode(1, OUTPUT); //LED on Oak

}


// the loop() routine runs over and over again
void loop()
{

  // turn pin 1 "on" by making the voltage HIGH)
  // the light will turn on here
  digitalWrite(1, HIGH);
  
  // wait a second (1000 milliseconds)
  // since we told the voltage to be on,
  // it will stay on while the Oak waits
  delay(1000);
  
  // turn the LED off by making the voltage LOW
  digitalWrite(1, LOW);

  // wait another second
  // since the voltage was low when the Oak
  // started to wait, it will stay off during this time
  delay(1000);

}

This code tells the Oak that we will be using pin 1 as an output, and then loops over and over sending a HIGH signal to pin 1 (turns the LED on), waits 1 second, and then turns the LED off by sending a LOW signal.

You can find a template like this in the Arduino IDE under File → Examples → Oak Examples → Start. Otherwise, start a new sketch and copy/paste the above into your file. Make sure the port and board selections are correct and then upload the code (the button above the text window with a right arrow icon). You should see this in the message area at the bottom of the Arduino window:

Sketch uses 259,578 bytes (24%) of program storage space. Maximum is 1,040,368 bytes.
Global variables use 51,048 bytes (62%) of dynamic memory, leaving 30,872 bytes for local variables. Maximum is 81,920 bytes.
Using config file at: /path/to/.oak/config.json
Sending file to cloud, to flash device-name (Device ID: 123456789)
Flashing..........
Rebooting Oak.........
Oak Ready

Once the Oak receives the code, it will blink as shown here (if the image isn't animated, click to open in a new tab):

Code: changing LED behavior

Now that you have a general idea of how to turn a light on and off, what else could you do? How might you make the blink shorter or longer? What do you think this changed code might do? (Hint: compare the delay values after each step!)

void setup()
{                

  pinMode(1, OUTPUT); //LED on Oak

}

// the loop() routine runs over and over again
void loop()
{

  // faster blink
  digitalWrite(1, HIGH);
  delay(100);
  
  // longer off time
  digitalWrite(1, LOW);
  delay(2000);

}

With this code, the light will blink for a shorter time and stay off longer.

Code: a for() loop

You can also define the delays using variables and loops to make things more interesting:

// now we've added two variables to hold our
// on and off time delays
// the "int" variable is able to hold integer values
int on_time;
int off_time;

void setup() 
{                

  pinMode(1, OUTPUT); //LED on Oak

}

void loop()
{

  // the for() loop will initialize a new temporary variable, i
  // it runs until i >= 500, adding 50 each time
  for(int i = 0; i < 500; i = i+50)
  {

    // the on time will equal i (0, 50, 100, ...)
    on_time = i;
    
    // the off time will equal 500 - i (500, 450, 400, ...)
    off_time = 500 - i;

    digitalWrite(1, HIGH);
    delay(on_time);
  
    digitalWrite(1, LOW);
    delay(off_time);
  
  }
  
}

Here is what happens each time the loop runs:

time through loop i on_time off_time
100500
25050450
3100100300
....
....
1045045050

When the for() loop above runs, i is equal to 0. It calculatesIf you upload this code, you will see this (click for enlarged view in new tab):

Congratulations! You've taken your first steps with the Oak. Take a look at some of the other tutorials for more!

oak/tutorials/blink.1458662752.txt.gz · Last modified: 2016/03/22 09:05 by jwhendy