Player FM 앱으로 오프라인으로 전환하세요!
들어볼 가치가 있는 팟캐스트
스폰서 후원


1 LIVE: Before the Chorus & Open Folk Present: In These Lines feat. Gaby Moreno, Lily Kershaw & James Spaite 33:58
Fade an LED with Arduino
Manage episode 269164147 series 2774299
Let’s expand the repertoire of output that we can use by looking at the function analogWrite().
I experienced much confusion with analogWrite(), because I suspected that it had to do with the analog pins on the Arduino. The function, however, has nothing to do with the analog pins.
There are 5 pins on most Arduino boards marked with ‘PWM’ next to the pin number (on some boards it is an “~” symbol) – these pins can be invoked to rapidly change the power being applied at the pin – this is a technique called pulse width modulation (PWM).
If you like this tutorial, click here to check out FREE Video Arduino course – thousands of people have really enjoyed it.
You Will Need LED – any color is fine 220 Ohm Resistor Alligator Clip Glacial ice cubes Step-by-Step Instructions Take the short leg of the LED and insert it in the GND pin. Take either leg of the resistor and place it in pin 9. Connect the long leg of the LED with the other leg of the resistor using an alligator clip Plug the Arduino into your computer with the USB cable Open up the Arduino IDE Open the sketch for this section. Click the Verify button (top left). The button will turn orange and then blue once finished. Click the Upload button. The button will turn orange and then blue when finished. Watch in mesmerizing amazement as the LED fades in and out. Arduino Fade an LED BoardThis image built with Fritzing.
Discuss the Sketch Below is the sketch in its entirety from the Arduino IDE:
/* Fade
This example shows how to fade an LED on pin 9 using the analogWrite() function.
This example code is in the public domain. */
int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); }
// the loop routine runs over and over again forever: void loop() { // set the brightness of pin 9: analogWrite(led, brightness);
// change the brightness for next time through the loop: brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 /* Fade This example shows how to fade an LED on pin 9 using the analogWrite() function. This example code is in the public domain. */ int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by // the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { // set the brightness of pin 9: analogWrite(led, brightness); // change the brightness for next time through the loop: brightness = brightness + fadeAmount; // reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); } The sketch starts with the usual multiline comment describing the program and how to set up the circuit. The first block of code we encounter is the declaration and initialization of three integer variables. The variable names and comments are both descriptive and helpful – remember this when naming and commenting your own code – useful comments are a pillar of success!
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by 1 2 3 4 5 int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by The brightness variable will store the value of the current brightness of the LED. fadeAmount is the rate at which the LED will fade and brighten. And of course, as the comments explain, led is simply the pin number where we have attached the LED (through a 220-ohm resistor).
Now that we have declared and initialized our variables, we move on to setting up the board with the setup() function…
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
} 1 2 3 4 5 6 7 void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); } The only thing we do here is set the mode of pin 9 as an OUTPUT using the pinMode() function. Recall that pinMode() takes two arguments – the pin number and the mode. In this case, we assign the pin number using the variable led, which we previously initialized as the number 9. By now you know that setup() only runs once – the code inside the setup() curly bracket will only be executed a single time by the Arduino.
Where the real action happens is in loop().
The first function we encounter in the loop() is analogWrite(). This function invokes the Pulse Width Modulation capabilities of the Arduino board. Pulse Width Modulation basically adjusts the power output at the pin. So you can have a lot of power or a little power applied at the pin, it’s your call, just tell the analogWrite() function which pin to modulate and how much power you want to be applied. The scale is from 0 to 255 with zero being the lowest power setting and 255 being the highest. For a discussion of what is actually happening with pulse width modulation check out the further reading section.
As alluded to above, analogWrite() takes two arguments…
analogWrite(pin, value); 1 analogWrite(pin, value); You can utilize analogWrite() with pins 3, 5, 6, 9, 10 and 11 – recall there is a “PWM” or “~” next to the pin number on the board.
In this sketch we use the arguments:
analogWrite(led, brightness); 1 analogWrite(led, brightness); The first thing we do in the loop is write a value to pin 9 (recall that led holds the number 9) where we have our LED attached (through a resistor) – and we set the value to 0 (zero is what our brightness variable initially holds). This will keep our LED dark to start with.
Key Points about the analogWrite function
The next line of code we encounter is:
brightness = brightness + fadeAmount;
( 0 ) = ( 0 ) + (5)
61 에피소드
Manage episode 269164147 series 2774299
Let’s expand the repertoire of output that we can use by looking at the function analogWrite().
I experienced much confusion with analogWrite(), because I suspected that it had to do with the analog pins on the Arduino. The function, however, has nothing to do with the analog pins.
There are 5 pins on most Arduino boards marked with ‘PWM’ next to the pin number (on some boards it is an “~” symbol) – these pins can be invoked to rapidly change the power being applied at the pin – this is a technique called pulse width modulation (PWM).
If you like this tutorial, click here to check out FREE Video Arduino course – thousands of people have really enjoyed it.
You Will Need LED – any color is fine 220 Ohm Resistor Alligator Clip Glacial ice cubes Step-by-Step Instructions Take the short leg of the LED and insert it in the GND pin. Take either leg of the resistor and place it in pin 9. Connect the long leg of the LED with the other leg of the resistor using an alligator clip Plug the Arduino into your computer with the USB cable Open up the Arduino IDE Open the sketch for this section. Click the Verify button (top left). The button will turn orange and then blue once finished. Click the Upload button. The button will turn orange and then blue when finished. Watch in mesmerizing amazement as the LED fades in and out. Arduino Fade an LED BoardThis image built with Fritzing.
Discuss the Sketch Below is the sketch in its entirety from the Arduino IDE:
/* Fade
This example shows how to fade an LED on pin 9 using the analogWrite() function.
This example code is in the public domain. */
int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); }
// the loop routine runs over and over again forever: void loop() { // set the brightness of pin 9: analogWrite(led, brightness);
// change the brightness for next time through the loop: brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 /* Fade This example shows how to fade an LED on pin 9 using the analogWrite() function. This example code is in the public domain. */ int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by // the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { // set the brightness of pin 9: analogWrite(led, brightness); // change the brightness for next time through the loop: brightness = brightness + fadeAmount; // reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); } The sketch starts with the usual multiline comment describing the program and how to set up the circuit. The first block of code we encounter is the declaration and initialization of three integer variables. The variable names and comments are both descriptive and helpful – remember this when naming and commenting your own code – useful comments are a pillar of success!
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by 1 2 3 4 5 int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by The brightness variable will store the value of the current brightness of the LED. fadeAmount is the rate at which the LED will fade and brighten. And of course, as the comments explain, led is simply the pin number where we have attached the LED (through a 220-ohm resistor).
Now that we have declared and initialized our variables, we move on to setting up the board with the setup() function…
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
} 1 2 3 4 5 6 7 void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); } The only thing we do here is set the mode of pin 9 as an OUTPUT using the pinMode() function. Recall that pinMode() takes two arguments – the pin number and the mode. In this case, we assign the pin number using the variable led, which we previously initialized as the number 9. By now you know that setup() only runs once – the code inside the setup() curly bracket will only be executed a single time by the Arduino.
Where the real action happens is in loop().
The first function we encounter in the loop() is analogWrite(). This function invokes the Pulse Width Modulation capabilities of the Arduino board. Pulse Width Modulation basically adjusts the power output at the pin. So you can have a lot of power or a little power applied at the pin, it’s your call, just tell the analogWrite() function which pin to modulate and how much power you want to be applied. The scale is from 0 to 255 with zero being the lowest power setting and 255 being the highest. For a discussion of what is actually happening with pulse width modulation check out the further reading section.
As alluded to above, analogWrite() takes two arguments…
analogWrite(pin, value); 1 analogWrite(pin, value); You can utilize analogWrite() with pins 3, 5, 6, 9, 10 and 11 – recall there is a “PWM” or “~” next to the pin number on the board.
In this sketch we use the arguments:
analogWrite(led, brightness); 1 analogWrite(led, brightness); The first thing we do in the loop is write a value to pin 9 (recall that led holds the number 9) where we have our LED attached (through a resistor) – and we set the value to 0 (zero is what our brightness variable initially holds). This will keep our LED dark to start with.
Key Points about the analogWrite function
The next line of code we encounter is:
brightness = brightness + fadeAmount;
( 0 ) = ( 0 ) + (5)
61 에피소드
모든 에피소드
×
1 Use Serial.print() to display Arduino output on your computer monitor: Part 2 6:25

1 Use Serial.print() to Display Arduino output on your computer monitor: Part 1 8:36

1 How to make a secret knock detector to trigger anything with only an Arduino and a few cheap components 12:57

1 Understanding the Arduino Sketchbook: Opening and Saving Arduino Sketches 9:41

1 An Easy Way to Learn I2C, SPI, RTC, ADCs and More with this Awesome Arduino Education Shield 5:04

1 6 Tips on Assembling an Arduino Shield (Or any Electronics Kit) 8:15

1 Using Red-Green-Blue (RGB) LEDs with Arduino (Common Cathode Type) 14:47

1 How to Make One Button Have the Functionality of Two or More with Arduino 15:40

1 The MOST guaranteed way to NOT buy a Fake Arduino (The Story of Pizza-Duino) 5:48

1 Throw out your breadboard! Dr. Duino: An Arduino Shield for debugging and developing Arduino projects 11:35

1 Shorthand Arithmetic :: Using Compound Operators (+= , -= , *= , /= ) with Arduino 12:43

1 Understanding Boolean Data Types and Using the Boolean NOT (!) operator to Switch Arduino Pin States 8:11

1 What to do when you just don't know :: Arduino, ADXL345 triple axis accelerometer and an RGB LED 14:04

1 Understanding the Arduino uber functions loop() and setup() 10:26

1 Functions: Let's make programming Arduino as easy as possible 11:00

1 The Arduino Development Toolchain - How it all gets done... 8:40

1 Everything you need to know about the Arduino IDE (for now) 11:09
플레이어 FM에 오신것을 환영합니다!
플레이어 FM은 웹에서 고품질 팟캐스트를 검색하여 지금 바로 즐길 수 있도록 합니다. 최고의 팟캐스트 앱이며 Android, iPhone 및 웹에서도 작동합니다. 장치 간 구독 동기화를 위해 가입하세요.