User Tools

Site Tools


tutorials:learn:arduino:lesson3.html

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tutorials:learn:arduino:lesson3.html [2010/10/21 14:56]
daigo
tutorials:learn:arduino:lesson3.html [2016/01/28 18:05] (current)
Line 12: Line 12:
 |{{  http://​www.ladyada.net/​images/​parts/​clear5mmled_t.jpg?​nolink&​200x85 ​ |}}| \\ LEDs \\ For this lesson, a red, green and blue LED are best. Make sure you get a "​5mm"​ or "​3mm"​ LED, with two legs, as shown in the example image. "​Ultrabright"​ LEDs (1000 mcd rating or higher) are preferred \\ |Any electronics supply store | |{{  http://​www.ladyada.net/​images/​parts/​clear5mmled_t.jpg?​nolink&​200x85 ​ |}}| \\ LEDs \\ For this lesson, a red, green and blue LED are best. Make sure you get a "​5mm"​ or "​3mm"​ LED, with two legs, as shown in the example image. "​Ultrabright"​ LEDs (1000 mcd rating or higher) are preferred \\ |Any electronics supply store |
 |{{ http://​www.ladyada.net/​images/​parts/​resleft_t.gif?​nolink&​61x87 |}}{{ http://​www.ladyada.net/​images/​parts/​rBrown_t.gif?​nolink&​10x87 |}}{{ http://​www.ladyada.net/​images/​parts/​rBlack_t.gif?​nolink&​10x87 |}}{{ http://​www.ladyada.net/​images/​parts/​rRed_t.gif?​nolink&​10x87 |}}{{ http://​www.ladyada.net/​images/​parts/​spacer_t.gif?​nolink&​8x87 |}}{{ http://​www.ladyada.net/​images/​parts/​rGold_t.gif?​nolink&​10x87 |}}{{ http://​www.ladyada.net/​images/​parts/​resright_t.gif?​nolink&​66x87 |}}|Three 1KΩ Resistors (brown black red gold) \\ Any values from 300 Ω to 2KΩ are probably OK. | \\ Any electronics supply store  \\ $1 \\ | |{{ http://​www.ladyada.net/​images/​parts/​resleft_t.gif?​nolink&​61x87 |}}{{ http://​www.ladyada.net/​images/​parts/​rBrown_t.gif?​nolink&​10x87 |}}{{ http://​www.ladyada.net/​images/​parts/​rBlack_t.gif?​nolink&​10x87 |}}{{ http://​www.ladyada.net/​images/​parts/​rRed_t.gif?​nolink&​10x87 |}}{{ http://​www.ladyada.net/​images/​parts/​spacer_t.gif?​nolink&​8x87 |}}{{ http://​www.ladyada.net/​images/​parts/​rGold_t.gif?​nolink&​10x87 |}}{{ http://​www.ladyada.net/​images/​parts/​resright_t.gif?​nolink&​66x87 |}}|Three 1KΩ Resistors (brown black red gold) \\ Any values from 300 Ω to 2KΩ are probably OK. | \\ Any electronics supply store  \\ $1 \\ |
-|{{  http://​www.ladyada.net/​images/​pshield/​miniproto_t.jpg?​nolink&​339x250 ​ |}}|Arduino Prototyping Shield with tiny breadboard ​ \\         | \\ [[http://​www.adafruit.com/​index.php?​main_page=product_info&​cPath=17&​products_id=51|Adafruit ​\\ ]]$15 \\ + \\ [[http://​www.adafruit.com/​index.php?​main_page=product_info&​cPath=18&​products_id=65|Adafruit]]$7.50 \\ |+|{{  http://​www.ladyada.net/​images/​pshield/​miniproto_t.jpg?​nolink&​339x250 ​ |}}|Arduino Prototyping Shield with tiny breadboard ​ \\         | \\ [[http://​www.adafruit.com/​index.php?​main_page=product_info&​cPath=17&​products_id=51|Adafruit]]$15 \\ + \\ [[http://​www.adafruit.com/​index.php?​main_page=product_info&​cPath=18&​products_id=65|Adafruit]]$7.50 \\ |
 |[[http://​www.ladyada.net/​images/​parts/​breadboard-half.jpg|{{ ​ http://​www.ladyada.net/​images/​parts/​breadboard-half.jpg?​nolink&​332x241 ​ |}}]] | \\ Standard solderless breadboard ​ \\ If you dont have a protoshield,​ this is a substitute| \\ [[http://​www.adafruit.com/​index.php?​main_page=product_info&​cPath=18&​products_id=64|Adafruit]] \\ $5 \\ Many hobby shops and electronics stores will have these | |[[http://​www.ladyada.net/​images/​parts/​breadboard-half.jpg|{{ ​ http://​www.ladyada.net/​images/​parts/​breadboard-half.jpg?​nolink&​332x241 ​ |}}]] | \\ Standard solderless breadboard ​ \\ If you dont have a protoshield,​ this is a substitute| \\ [[http://​www.adafruit.com/​index.php?​main_page=product_info&​cPath=18&​products_id=64|Adafruit]] \\ $5 \\ Many hobby shops and electronics stores will have these |
 |{{  http://​www.ladyada.net/​images/​tools/​100ftsolid_t.jpg?​nolink&​150x150 ​ |}}| \\ Hookup Wire \\ Get 22  gauge solid-core wire in red, black and some other color. Make sure its //not// stranded wire! \\ |Any hardware store | |{{  http://​www.ladyada.net/​images/​tools/​100ftsolid_t.jpg?​nolink&​150x150 ​ |}}| \\ Hookup Wire \\ Get 22  gauge solid-core wire in red, black and some other color. Make sure its //not// stranded wire! \\ |Any hardware store |
Line 71: Line 71:
 The resistor is the most basic and also most common electronic part. An electronic gadget, such as an mp3 player has easily a //​thousand//​ resistors inside of it! The resistor is the most basic and also most common electronic part. An electronic gadget, such as an mp3 player has easily a //​thousand//​ resistors inside of it!
  
-{{  http://​www.ladyada.net/​images/​parts/​22K.jpg?​nolink&​594x116 ​ |}} \\ //Behold...a resistor! //+{{  http://​www.ladyada.net/​images/​parts/​22K.jpg?​nolink&​594x116 ​ |}} 
 +<class center>Behold...a resistor!</class>
  
  
Line 124: Line 125:
 If you recall from [[http://​www.ladyada.net/​learn/​arduino/​lesson1.html|lesson 1]], Arduino NG users had to make sure that they inserted the LED in the right way. If you place the LED in backwards it won't work. Diecimila Arduino users already have the LED (a very very small one) soldered onto the circuit board the right way.  If you recall from [[http://​www.ladyada.net/​learn/​arduino/​lesson1.html|lesson 1]], Arduino NG users had to make sure that they inserted the LED in the right way. If you place the LED in backwards it won't work. Diecimila Arduino users already have the LED (a very very small one) soldered onto the circuit board the right way. 
  
-{{  http://​www.ladyada.net/​images/​arduino/​diecledplaced.jpg?​nolink&​500x334 ​ |}} \\ //Look again! Its a tiny LED //+{{  http://​www.ladyada.net/​images/​arduino/​diecledplaced.jpg?​nolink&​500x334 ​ |}}  
 +<class center> ​//Look again! Its a tiny LED //</​class>​
  
  
Line 152: Line 154:
 Place the resistor and LED as shown. Make sure the longer leg of the LED is to the right, connected to the resistor. The resistor doesn'​t have a direction, so it doesnt matter which way it goes in.  Place the resistor and LED as shown. Make sure the longer leg of the LED is to the right, connected to the resistor. The resistor doesn'​t have a direction, so it doesnt matter which way it goes in. 
  
-[[http://​www.ladyada.net/​images/​arduino/​basicshieldled.jpg|{{ ​ http://​www.ladyada.net/​images/​arduino/​basicshieldled_t.jpg?​nolink&​500x375 ​ |}}]] \\ //Click for a high resolution photo if necessary!//​+[[http://​www.ladyada.net/​images/​arduino/​basicshieldled.jpg|{{ ​ http://​www.ladyada.net/​images/​arduino/​basicshieldled_t.jpg?​nolink&​500x375 ​ |}}]]  
 +<class center>//Click for a high resolution photo if necessary!//​</​class>​
  
  
Line 172: Line 175:
   *Try another LED in case this one is damaged   *Try another LED in case this one is damaged
   *Make sure the parts are as shown in the image above, if you have a wire in one row and the resistor in the other, they aren't connected and it wont work!  ​   *Make sure the parts are as shown in the image above, if you have a wire in one row and the resistor in the other, they aren't connected and it wont work!  ​
 +
 ==== Scheming schematic ​ ==== ==== Scheming schematic ​ ====
  
Line 185: Line 189:
 Each electronic component has a schematic symbol, which is a simplified drawing of the part. For resistors the symbol looks like this: Each electronic component has a schematic symbol, which is a simplified drawing of the part. For resistors the symbol looks like this:
  
-{{  http://​www.ladyada.net/​images/​arduino/​resistorsymbol.jpg?​nolink&​221x64 ​ |}} \\ //Resistor symbol// ​+{{  http://​www.ladyada.net/​images/​arduino/​resistorsymbol.jpg?​nolink&​221x64 ​ |}}  
 +<class center>//Resistor symbol// ​</​class>​
  
  
Line 191: Line 196:
 And the symbol for LED's look like this: And the symbol for LED's look like this:
  
-{{  http://​www.ladyada.net/​images/​arduino/​ledsymbol.jpg?​nolink&​217x78 ​ |}} \\ //LED symbol, positive pin on the left, negative pin on the right //+{{  http://​www.ladyada.net/​images/​arduino/​ledsymbol.jpg?​nolink&​217x78 ​ |}} 
 +<class center>//LED symbol, positive pin on the left, negative pin on the right //</​class>​
  
  
Line 201: Line 207:
 Power and ground also have symbols: Power and ground also have symbols:
  
-{{ http://​www.ladyada.net/​images/​arduino/​powergndsym.jpg?​nolink&​139x87 |}} \\ //Power and Ground symbols // +{{ http://​www.ladyada.net/​images/​arduino/​powergndsym.jpg?​nolink&​139x87 |}}  
 +<class center> ​//Power and Ground symbols // </​class>​
  
  
Line 207: Line 214:
 The only thing we need to do now is indicate how the LED and resistor are hooked up and show the 5V and ground connections. ​ The only thing we need to do now is indicate how the LED and resistor are hooked up and show the 5V and ground connections. ​
  
-{{  http://​www.ladyada.net/​images/​arduino/​lesson3sch1.jpg?​nolink&​71x260 ​ |}} \\ //A barebones schematic// ​+{{  http://​www.ladyada.net/​images/​arduino/​lesson3sch1.jpg?​nolink&​71x260 ​ |}} 
 +<class center> ​//A barebones schematic// ​</​class>​
  
  
Line 213: Line 221:
 Next to symbols, we often write important information like what the resistor value is, what color and size the LED should be, and the voltage associated with the power supply. Next to symbols, we often write important information like what the resistor value is, what color and size the LED should be, and the voltage associated with the power supply.
  
-{{  http://​www.ladyada.net/​images/​arduino/​lesson3sch2.jpg?​nolink&​168x260 ​ |}} \\ //A well documented schematic!//​+{{  http://​www.ladyada.net/​images/​arduino/​lesson3sch2.jpg?​nolink&​168x260 ​ |}} 
 +<class center> ​//A well documented schematic!//​</​class>​
  
 For practice, try drawing your own schematic on a piece of paper. ​ For practice, try drawing your own schematic on a piece of paper. ​
Line 273: Line 282:
  
  
-  ​**Spend some time experimenting with different pins.** Connect the LED to different pin sockets, and modify the sketch so that the LED blinks. +**Spend some time experimenting with different pins.** Connect the LED to different pin sockets, and modify the sketch so that the LED blinks. ​\\ 
-  **Change around your wiring so that it matches this schematic**:​ \\ {{  http://​www.ladyada.net/​images/​arduino/​lesson3sch6.jpg?​nolink&​176x292 ​ |}} \\ Make sure to modify you sketch so that the **ledPin** is 13 again. Re-compile and upload it to the Arduino. What does the LED do? \\ //Highlight the text below to see the answer //+**Change around your wiring so that it matches this schematic**:​ \\ {{  http://​www.ladyada.net/​images/​arduino/​lesson3sch6.jpg?​nolink&​176x292 ​ |}} \\  
 +Make sure to modify you sketch so that the **ledPin** is 13 again. Re-compile and upload it to the Arduino. What does the LED do? \\ //Highlight the text below to see the answer // \\
 <class white> It blinks just like before </​class>​ <class white> It blinks just like before </​class>​
-  ​**If you have a Diecimila Arduino, what do you notice about the breadboard LED and the on-board LED?** \\ //Highlight the text below to see the answer //**  ​\\ ​+**If you have a Diecimila Arduino, what do you notice about the breadboard LED and the on-board LED?** \\ //Highlight the text below to see the answer //  \\ 
 <class white>​They are alternating when they blink </​class>​ <class white>​They are alternating when they blink </​class>​
- **Why do you think that is? \\ **//​Highlight the text below to see the answer ​ \\ +**Why do you think that is? \\ ** //Highlight the text below to see the answer ​// \\ 
 <class white>​When the pin is LOW (connected to ground) the breadboard LED is on: current is flowing from +5V to ground through the pin. When the pin is HIGH (connected to +5V) the on-board LED is on, just like before. ​ </​class>​ <class white>​When the pin is LOW (connected to ground) the breadboard LED is on: current is flowing from +5V to ground through the pin. When the pin is HIGH (connected to +5V) the on-board LED is on, just like before. ​ </​class>​
  
Line 286: Line 296:
 OK sure you've had plenty of practice messing around with LEDs. It's time to go full color! Find a red, green and blue LED. If you have the Arduino Starter Pack they will be the three clear LEDs. You can't tell which one is which until they are lit so just build the circuit and then rearrange them if needed. ​ OK sure you've had plenty of practice messing around with LEDs. It's time to go full color! Find a red, green and blue LED. If you have the Arduino Starter Pack they will be the three clear LEDs. You can't tell which one is which until they are lit so just build the circuit and then rearrange them if needed. ​
  
-{{  http://​www.ladyada.net/​images/​arduino/​lesson3sch7.jpg?​nolink&​399x262 ​ |}} \\ //Red, green and blue LED schematic//+{{  http://​www.ladyada.net/​images/​arduino/​lesson3sch7.jpg?​nolink&​399x262 ​ |}}  
 +<class center> ​//Red, green and blue LED schematic//</​class>​
  
 In this schematic we will have three LEDs connected to three different pins: #10, #11 and #12. In this schematic we will have three LEDs connected to three different pins: #10, #11 and #12.
Line 312: Line 323:
   digitalWrite(greenPin,​ LOW);    // sets the Green LED off   digitalWrite(greenPin,​ LOW);    // sets the Green LED off
   delay(500); ​                    // waits for half a second   delay(500); ​                    // waits for half a second
-}</​code>​//You can just copy and paste this text into your Arduino software ​window.//+}</​code>​ 
 +<class center>You can just copy and paste this text into your Arduino software ​</class>
  
-**Quick quiz  \\ What does this sketch do? Compile and upload the sketch to test your hypothesis. ​\\ **//​Highlight the text below to see the answer ​ ​\\ ​//It blinks the two LEDs connected to pins 11 and 12 at the same time +**Quick quiz**  ​\\ ​ 
 +**What does this sketch do? Compile and upload the sketch to test your hypothesis. **\\  
 +//Highlight the text below to see the answer // \\ 
 +<class white>It blinks the two LEDs connected to pins 11 and 12 at the same time </​class>​
  
- <​img title="​Click to play" alt="​Video thumbnail. Click to play." src="​http://​blip.tv/​file/​get/​Ladyada-redgreenBlink704.MP4.jpg"​ border="​0"​ title="​Click To Play" /> \\ Click To Play play_blip_movie_392145();​ If you are having problems getting this sketch to work, double check:+{{bliptv>392145}}  
 +\\ 
 + 
 +If you are having problems getting this sketch to work, double check:
  
  
Line 323: Line 341:
   *Are the resistors in the right sockets?   *Are the resistors in the right sockets?
   *Are the LEDs connected to ground on the other side?  ​   *Are the LEDs connected to ground on the other side?  ​
-  *Is the breadboard wired up right? Check your connections. **Exercises!**+  *Is the breadboard wired up right? Check your connections. ​ 
 + 
 + 
 +**Exercises!** 
 + 
 + 
 +**Change the code so that the LEDs alternate their blinks:​**\\ 
 + 
 +{{bliptv>​392150}} 
 +**//​Highlight the text below to see one possible solution//​** \\ 
 +<class white>​Change the second **digitalWrite**() procedure call to set the pin LOW, and the fourth call to set the pin HIGH. </​class>​ 
 + 
 +**Change the loop() procedure code so that both LEDs are on for 500 ms, then only the red LED is on for 500 ms, then both LEDs are off, and finally only the green LED is on for 500 ms** \\  
 +//Highlight the text below to see one answer// \\ 
  
 +<​code>​
 +void loop() ​                      // run over and over again 
 +
 +   ​digitalWrite(redPin,​ HIGH); ​    // sets the Red LED on 
 +   ​digitalWrite(greenPin,​ HIGH); ​  // sets the Green LED on 
 +   ​delay(500); ​                    // waits for half a second ​
 +   ​digitalWrite(redPin,​ HIGH); ​    // sets the Red LED on 
 +   ​digitalWrite(greenPin,​ LOW);    // sets the Green LED off 
 +   ​delay(500); ​                    // waits for half a second ​
 +   ​digitalWrite(redPin,​ LOW);      // sets the Red LED off 
 +   ​digitalWrite(greenPin,​ LOW);    // sets the Green LED off 
 +   ​delay(500); ​                    // waits for half a second ​
 +   ​digitalWrite(redPin,​ LOW);      // sets the Red LED off 
 +   ​digitalWrite(greenPin,​ HIGH); ​  // sets the Green LED on 
 +   ​delay(500); ​                    // waits for half a second ​
 + }
 +</​code>  ​
  
-  ***Change the code so that the LEDs alternate their blinks:**** \\ <​img title="​Click to play" alt="​Video thumbnail. Click to play." src="​http://​blip.tv/​file/​get/​Ladyada-redgreenAlternate457.MP4.jpg"​ border="​0"​ title="​Click To Play" /> \\ Click To Play play_blip_movie_392150();​ **//​Highlight the text below to see one possible solution//​** \\ **Change the second **digitalWrite**() procedure call to set the pin LOW, and the fourth call to set the pin HIGH. 
-  ***Change the loop() procedure code so that both LEDs are on for 500 ms, then only the red LED is on for 500 ms, then both LEDs are off, and finally only the green LED is on for 500 ms** \\ //Highlight the text below to see one answer//** **         \\ <code C>void loop() ​                      // run over and over again \\ { \\   ​digitalWrite(redPin,​ HIGH); ​    // sets the Red LED on \\   ​digitalWrite(greenPin,​ HIGH); ​  // sets the Green LED on \\   ​delay(500); ​                    // waits for half a second \\   ​digitalWrite(redPin,​ HIGH); ​    // sets the Red LED on \\   ​digitalWrite(greenPin,​ LOW);    // sets the Green LED off \\   ​delay(500); ​                    // waits for half a second \\   ​digitalWrite(redPin,​ LOW);      // sets the Red LED off \\   ​digitalWrite(greenPin,​ LOW);    // sets the Green LED off \\   ​delay(500); ​                    // waits for half a second \\   ​digitalWrite(redPin,​ LOW);      // sets the Red LED off \\   ​digitalWrite(greenPin,​ HIGH); ​  // sets the Green LED on \\   ​delay(500); ​                    // waits for half a second \\ }</​code>  ​ 
  
 ==== Full color adventures! ​ ==== ==== Full color adventures! ​ ====
Line 354: Line 401:
   digitalWrite(greenPin,​ LOW);    // sets the Green LED off   digitalWrite(greenPin,​ LOW);    // sets the Green LED off
   delay(500); ​                    // waits for half a second   delay(500); ​                    // waits for half a second
-}</​code>//​You can just copy and paste this text into your Arduino software window.//+}</code
 +<class center>//You can just copy and paste this text into your Arduino software window.//</​class>​
  
  
Line 382: Line 430:
  
  
-{{ ../​proj1/​AdditiveColorMixing.png?​nolink&​236x236 |}} \\ //A additive (light) color mixing diagram// ​     According to this diagram, if we have both red and blue light mixed together we should get a violet light. ​     **Quick quiz **+{{ http://www.ladyada.net/learn/​proj1/​AdditiveColorMixing.png?​nolink&​236x236 |}}  
 +<class center>//A additive (light) color mixing diagram// ​    </​class>​ 
 + 
 + According to this diagram, if we have both red and blue light mixed together we should get a violet light. ​     ​ 
 + 
 +**Quick quiz **
  
 **Modify your code to create the following colored light:​** ​ **Modify your code to create the following colored light:​** ​
 Violet (red & blue), Turquoise (blue & green) and yellow (green & red)      Violet (red & blue), Turquoise (blue & green) and yellow (green & red)     
- **Now you are ready for the "final project"​ of this very long lesson. You are going to make a color changing light! ** \\ Modify the sketch so that the emitted light goes in order: red, yellow, green, turquioise, blue violet and back to red. It should pause about half a second between each color change. ​      ​**Hint:​** One way to make the color mixing work better is to //diffuse// the light, in a light box. You can make a light box out of plain paper, scissors and some tape. Just make a paper box and cut a hole in it. Fill the box with tissue paper. The tissue acts as a diffuser, helping the light mix nicely {{  http://​www.ladyada.net/​images/​arduino/​diffusercut.jpg?​nolink&​500x375 ​ |}}    [[http://​www.ladyada.net/​images/​arduino/​tissuediffuse.jpg|{{ ​ http://​www.ladyada.net/​images/​arduino/​tissuediffuse_t.jpg?​nolink&​500x345 ​ |}}]] <​img title="​Click to play" alt="​Video thumbnail. Click to play." src="​http://​blip.tv/​file/​get/​Ladyada-colorCycle325.flv.jpg"​ border="​0"​ title="​Click To Play" /\\ Click To Play play_blip_movie_392136();​  ​                +**Now you are ready for the "final project"​ of this very long lesson. You are going to make a color changing light! ** \\ 
 +Modify the sketch so that the emitted light goes in order: red, yellow, green, turquioise, blue violet and back to red. It should pause about half a second between each color change. ​       
 +**Hint:** One way to make the color mixing work better is to //diffuse// the light, in a light box. You can make a light box out of plain paper, scissors and some tape. Just make a paper box and cut a hole in it. Fill the box with tissue paper. The tissue acts as a diffuser, helping the light mix nicely ​ 
 +{{  http://​www.ladyada.net/​images/​arduino/​diffusercut.jpg?​nolink&​500x375 ​ |}}    ​ 
 +[[http://​www.ladyada.net/​images/​arduino/​tissuediffuse.jpg|{{ ​ http://​www.ladyada.net/​images/​arduino/​tissuediffuse_t.jpg?​nolink&​500x345|}}]]  
 + 
 +{{bliptv>392136}}  ​                
  
/home/ladyada/public_html/wiki/data/attic/tutorials/learn/arduino/lesson3.html.1287673004.txt.gz · Last modified: 2016/01/28 18:05 (external edit)