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.
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.
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.
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.
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.
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. With your Oak plugged powered on, make sure the Arduino board selection is correct, and then upload the code with 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):
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.
Congratulations – you've uploaded your first sketch to the Oak! In this lesson, you were able to tackle all of these first steps, which is a definite accomplishment:
OUTPUT
loop()
section, which does something again and againHIGH
and LOW
voltage to a pindelay()
to affect the behavior of an LEDKeep your momentum going and take a look at the next tutorial which will get you started wiring up LED bulbs, resistors, and some more advanced coding!