User Tools

Site Tools


oak:tutorials:lesson_template

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
oak:tutorials:lesson_template [2016/03/22 18:04]
jwhendy
oak:tutorials:lesson_template [2016/03/22 20:39] (current)
jwhendy
Line 1: Line 1:
-===== Oak: LED basics ===== 
  
-[[http://​digistump.com/​wiki/​_media/​oak/​tutorials/​oak-1-led.png|{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​oak-1-led.png?​400}}]]+======OakRGB LED Lesson======
  
-This tutorial will build on the [[oak:tutorials:​blink|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, [[https://learn.adafruit.com/all-about-leds/overview|All about LEDs]] on Adafruit.+[Fritzing or photo of final main circuit for this lesson] 
 +Fritzing parts here: https://github.com/digistump/OakCore/​issues/​38
  
-===== Components ​used =====+Description of the lesson - ex: This lesson will show you how to safely connect a Red, Green, Blue LED to your Oak and control it from a sketch.  
 + 
 +=====Components ​Used:=====
  
 ^ Part       ^ Quantity ​         ^Identification^ ^ Part       ^ Quantity ​         ^Identification^
 | Oak with soldered headers |1| | | Oak with soldered headers |1| |
-| Breadboard|1| | + Breadboard| 1| | 
-|5mm LED bulb, any color|2| | + 5mm RGB Common Cathod ​LED| 1| | 
-|Jumper wires|2+ 330R Resistors3Orange-Orange-Red|
-|Resistor, 220 ohm|1|Red-Red-Brown|+
  
-===== Concepts =====+[NOTE: You can find an index of the parts in the starter and ultimate kits here: [[oak:​tutorials:​kit_parts|Starter and Ultimate Kit Parts List]]]
  
-=== analogWrite() ​===+=====Concepts:​=====
  
-The ''​digitalWrite()''​ function used in the blink tutorial can send ''​HIGH''​ or ''​LOW''​ signals to a pin on the OakThis 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 outputand what output level to send. Microcontrollers use integers from 0 - some limit based on the bits of resolution ​they use. To keep things simplejust remember that the Oak can use ''​analogWrite()''​ from 0 (offto 1023 (full power).+In this section you would cover the concepts new for this lessonFor this lesson that may include PWM (both what it is, how it works, and how to change ​it in code with tiny code examples)LEDs (briefly ​what they areas the first single LED lesson should cover that more in depth), RGB LEDs (how they are different then a single LED, common cathode vs annode, etc), resistors ​(again in brief as the first single LED lesson should cover that more in depth).
  
-=== PWM ===+Example of a concept:
  
-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 [[https://​en.wikipedia.org/​wiki/​Pulse-width_modulation|Wikipedia]]). The simplified version is that by pulsing a single voltage ​(like 3.3Von 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+**PWM**: In this lesson we will use Pulse Width Modulation ​(PWM) to control the 3 different color LED emitters inside the RGB LED. By powering each color from separate pin of the Oak (through a resistorwe can use the Oak to control how much power goes to each colorwhich allows us to control ​the color of the RGB LED.
- +
-=== LED === +
- +
-An LED is 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 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 ===== +
- +
-We will build this circuit: +
- +
-[[http://​digistump.com/​wiki/​_media/​oak/​tutorials/​oak-1-led.png|{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​oak-1-led.png?​400}}]] +
- +
-  * 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 resistorand 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: +
- +
-[[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: blink, revisited === +
- +
-Using the same code from the blink tutorial with one change, we can blink our new LED bulb!+
  
 +PWM can be used in your code like this:
 <​code>​ <​code>​
-void setup() +analogWrite(0,1023); 
-{                ​+more code here... 
 +</​code>​ 
 +Where analogWrite(0,​1023) means turn Pin0 fully on. A value from 0-1023 can be used to turn the pin on at a level from 0-100%.
  
-  // initialize digital pin 9 as an output. +What is actually happening with PWM: PWM creates ​an output ​that averages out to a certain voltage/​power level by turning the pin on and off very quicklyThe setting you set of 0-100% ​(0-1023is called the duty cycle, at 100% the pin remains ​on 100% of the time, at 50% it remains on half of the time.
-  pinMode(9, OUTPUT); //LED on Oak+
  
-}+Etc, etc, diagrams and images are welcome but each concept should be no more than two short paragraphs and a code block or image.
  
 +=====Circuit:​=====
  
-// the loop() routine runs over and over again +[Fritzing diagram of hooking ​the Oak to the RGB LED with resistors ​and the breadboard]
-void loop() +
-{+
  
-  // turn pin 9 "​on"​ by making the voltage HIGH) +Steps to make these connectionsthings to watch out for (polarityresistor codes etc)
-  digitalWrite(9HIGH); +
-   +
-  // wait a second (1000 milliseconds) +
-  delay(1000);​ +
-   +
-  // turn the LED off by making the voltage LOW +
-  digitalWrite(9LOW);+
  
-  // wait another second 
-  delay(1000);​ 
  
-+=====Code:=====
-</​code>​ +
- +
-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. +
- +
-=== Code: changing brightness ​=== +
- +
-With the next code, we will switch to using ''​analogWrite''​ so that we can control the desired brightness:+
  
 <​code>​ <​code>​
-void setup() +Code to show example of it working
-{                 +
- +
-  // 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() +
-+
- +
-  // start with pin 9 off by sending a 0 +
-  analogWrite(9,​ 0); +
-  delay(500);​ +
- +
-  // set pin 9 to 100/1023, or about 10% power +
-  analogWrite(9,​ 100); +
-  delay(500);​ +
-   +
-  // 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>​
  
-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. +Explanation ​of code
- +
-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: +
- +
-[gif of progression] +
- +
-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 =====+=====Conclusion:=====
  
-Congratulations on taking another step with your Oak! You've now wired up an external LEDlearned about LED polarity and the need for a resistorand 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!+Where to go from herepotential usesetc.
oak/tutorials/lesson_template.txt · Last modified: 2016/03/22 20:39 by jwhendy