User Tools

Site Tools


oak:tutorials:lesson_template

This is an old revision of the document!


In progress…


Oak: LED basics

This tutorial will build on the last by using an external LED bulb. We will also control the brightness of an LED vs. simply turning it on and off. There are excellent articles about LEDs, so if you want to know more about the nitty gritty of how LEDs work, please seek out other resources. One example is the excellent article, All about LEDs on Adafruit.

Components used

Part Quantity Identification
Oak with soldered headers 1
Breadboard1
5mm LED bulb, any color2
Jumper wires2
Resistor, 1k ohm1Red-Red-Brown

Concepts

analogWrite()

The digitalWrite() function used in the blink tutorial can send HIGH or LOW signals to a pin on the Oak. This is like turning the light switch on and off. Using analogWrite() is like adding a dimmer to your switch! You can now decide if the LED is one, and how bright it should be.

PWM

The term PWM stands for pulse-width modulation. A full description is outside the scope of this tutorial, but feel free to read up on the concept (e.g. on Wikipedia). The simplified version is that by pulsing a single voltage (like 3.3V) on and off, the LED will behave as if we sent a lower voltage. The pulses are very fast (generally faster than one can see) and thus this is a great way to control the brightness of an LED!

LED

An LED is a light emitting diode, and a diode only allows electricity to flow in one direction. This means that LEDs are also directional. There is generally one leg of an LED that is longer, which is used to indicate the positive side. The shorter leg is generally connected to ground, so electricity will flow in the positive leg, through the bulb (generating light) and through the negative leg to ground.

LEDs also typically have an optimal current, which is typically about 20-30mA for a 5mm LED. To obtain this current, we use a resistor. In the interest of keeping this simple, we're simply providing a value of 220 ohm as a middle of the road recommendation. There's some good information at Evil Mad Scientist about how these resistor values can be chosen if you'd like to learn more. What's certain is that you should never plug an LED directly into the Vin or Vcc of the Oak; too much current will destroy it!

Circuit

We will build this circuit:

  • Start by inserting the 220 ohm resistor into two different rows on the breadboard
  • Insert the LED into the breadboard so that it's long leg is in the same row as the resistor, and the other leg in a different row
  • Connect pin 9 of the Oak to the free end of the resistor with a jumper wire
  • Connect a ground pin of the Oak to the row containing the short leg of the LED

If real world pictures help, you can double check your circuit against this one:

[pic]

Code

Using the same code from the blink tutorial with one change, we can blink our new LED bulb!

void setup()
{                

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

}


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

  // turn pin 9 "on" by making the voltage HIGH)
  digitalWrite(1, HIGH);
  
  // wait a second (1000 milliseconds)
  delay(1000);
  
  // turn the LED off by making the voltage LOW
  digitalWrite(9, LOW);

  // wait another second
  delay(1000);

}

Code: 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/lesson_template.1458688773.txt.gz · Last modified: 2016/03/22 16:19 by jwhendy