This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
|
oak:tutorials:lesson_template [2016/03/22 16:19] jwhendy |
oak:tutorials:lesson_template [2016/03/22 18:04] jwhendy |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | In progress... | ||
| - | |||
| - | ----- | ||
| - | |||
| ===== Oak: LED basics ===== | ===== Oak: LED basics ===== | ||
| Line 16: | Line 12: | ||
| |5mm LED bulb, any color|2| | | |5mm LED bulb, any color|2| | | ||
| |Jumper wires|2| | | |Jumper wires|2| | | ||
| - | |Resistor, 1k ohm|1|Red-Red-Brown| | + | |Resistor, 220 ohm|1|Red-Red-Brown| |
| ===== Concepts ===== | ===== Concepts ===== | ||
| Line 22: | Line 18: | ||
| === analogWrite() === | === 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. | + | 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 on, and how bright it should be. The function takes parameters for a pin on which to send the output, and what output level to send. Microcontrollers use integers from 0 - some limit based on the bits of resolution they use. To keep things simple, just remember that the Oak can use ''analogWrite()'' from 0 (off) to 1023 (full power). |
| === PWM === | === PWM === | ||
| Line 32: | Line 28: | ||
| 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. | 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 [[http://www.evilmadscientist.com/2012/resistors-for-leds/|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! | + | LEDs 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 [[http://www.evilmadscientist.com/2012/resistors-for-leds/|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 ===== | ===== Circuit ===== | ||
| Line 47: | Line 43: | ||
| If real world pictures help, you can double check your circuit against this one: | If real world pictures help, you can double check your circuit against this one: | ||
| - | [pic] | + | [[http://digistump.com/wiki/_media/oak/tutorials/oak-1-led.jpg|{{http://digistump.com/wiki/_media/oak/tutorials/oak-1-led.jpg?500}}]] |
| ===== Code ===== | ===== Code ===== | ||
| Line 70: | Line 66: | ||
| // turn pin 9 "on" by making the voltage HIGH) | // turn pin 9 "on" by making the voltage HIGH) | ||
| - | digitalWrite(1, HIGH); | + | digitalWrite(9, HIGH); |
| | | ||
| // wait a second (1000 milliseconds) | // wait a second (1000 milliseconds) | ||
| Line 84: | Line 80: | ||
| </code> | </code> | ||
| - | === Code: for() loop === | + | This code does just what the on-board blink sketch did: it sends a ''HIGH'' voltage to a pin (9 in this case instead of 1), waits a second, then turns off the power to that pin by sending a ''LOW'' signal, waiting for an additional second before repeating. By changing the pin, and connecting an LED to that pin, we've blinked our LED bulb. |
| - | You can also define the delays using variables and loops to make things more interesting: | + | === Code: changing brightness === |
| - | <code> | + | With the next code, we will switch to using ''analogWrite'' so that we can control the desired brightness: |
| - | // 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() | + | <code> |
| + | void setup() | ||
| { | { | ||
| - | pinMode(1, OUTPUT); //LED on Oak | + | // initialize digital pin 9 as an output. |
| + | // this stays the same for analogWrite() | ||
| + | pinMode(9, OUTPUT); //LED on Oak | ||
| } | } | ||
| + | |||
| + | // the loop() routine runs over and over again | ||
| void loop() | void loop() | ||
| { | { | ||
| - | // the for() loop will initialize a new temporary variable, i | + | // start with pin 9 off by sending a 0 |
| - | // it runs until i >= 500, adding 50 each time | + | analogWrite(9, 0); |
| - | for(int i = 0; i < 500; i = i+50) | + | delay(500); |
| - | { | + | |
| - | // the on time will equal i (0, 50, 100, ...) | + | // set pin 9 to 100/1023, or about 10% power |
| - | on_time = i; | + | analogWrite(9, 100); |
| - | + | delay(500); | |
| - | // 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); | + | |
| - | + | ||
| - | } | + | |
| | | ||
| + | // increase to about 25% power | ||
| + | analogWrite(9, 250); | ||
| + | delay(500); | ||
| + | |||
| + | // 50% power | ||
| + | analogWrite(9, 500); | ||
| + | delay(500); | ||
| + | |||
| + | // full power | ||
| + | analogWrite(9, 1023); | ||
| + | delay(500); | ||
| + | |||
| } | } | ||
| </code> | </code> | ||
| - | Here is what happens each time the loop runs: | + | This code may look a lot longer and possibly intimidating, but we've simply used the same basic structure from above (do something, then wait) over and over. The analog functions and their corresponding delays have been grouped tighter to show that these are pairs of commands. |
| - | ^time through loop ^i ^on_time ^off_time| | + | Since the Oak can send values from 0-1023, this sketch cycles through some various PWM values to show you the effect. Sending ''100'' means that the ''analogWrite'' signal is high 100/1023 or about 9.8% of the time. When you upload this sketch, you'll see a demonstration of your LED going from off to full brightness in steps: |
| - | |1|0|0|500| | + | |
| - | |2|50|50|450| | + | |
| - | |3|100|100|300| | + | |
| - | |.|.|.|.| | + | |
| - | |.|.|.|.| | + | |
| - | |10|450|450|50| | + | |
| - | 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): | + | [gif of progression] |
| - | [[http://digistump.com/wiki/_media/oak/tutorials/loop-changing-blink.gif|{{oak:tutorials:loop-changing-blink.gif?300}}]] | + | Play around with the code above. Perhaps change the order (high to low), the timing between each step (altering the ''delay()'' value used), or the number of steps (what about a more gradual transition?). |
| + | ===== Conclusion ===== | ||
| - | Congratulations! You've taken your first steps with the Oak. Take a look at some of the other [[http://digistump.com/wiki/oak?redirect=1|tutorials]] for more! | + | Congratulations on taking another step with your Oak! You've now wired up an external LED, learned about LED polarity and the need for a resistor, and gotten started with ''analogWrite()''. Please take a look at the [[oak:tutorials:advanced-leds|advanced LED tutorial]] (multiple LEDs, using a ''for()'' loop, and an alternative to ''delay()'') or some of the other [[http://digistump.com/wiki/oak?redirect=1|tutorials]] for more! |