Discover this podcast and so much more

Podcasts are free to enjoy without a subscription. We also offer ebooks, audiobooks, and so much more for just $11.99/month.


ratings:
Length:
21 minutes
Released:
Mar 13, 2017
Format:
Podcast episode

Description

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
Released:
Mar 13, 2017
Format:
Podcast episode

Titles in the series (61)

Video lessons on learning programming and electronics with Arduino. This is part of our Arduino Crash Course and Arduino Course for Absolute Beginners. It's designed to take someone with little or no experience in programming and electronics and get them fast-tracked to learning the skills to prototype using Arduino. We'll include some lessons from the first edition and the second edition of our training course.