arduino:arduino_crash_course:analog_output
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
arduino:arduino_crash_course:analog_output [2012/11/06 00:20] – created mithat | arduino:arduino_crash_course:analog_output [2017/12/06 00:55] (current) – [Analog Output] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Analog Output ====== | ====== Analog Output ====== | ||
- | An analog signal is one that can have any value over a continuous range. Arduino employs an approximation to analog outputs and calls these outputs " | + | An analog signal is one that can have any value over a continuous range. Arduino employs an approximation to analog outputs and calls these outputs " |
===== PWM ===== | ===== PWM ===== | ||
- | Arduino achieves analog-like outputs by using a technique called **Pulse Width Modulation** (**PWM**). PWM is technique where a Boolean output is switched on and off very quickly (in the case of the Arduino Uno, it's about TODO times per second). The ratio of the time the output is in the HIGH state to the total period time (i.e., time HIGH + time LOW) is called the duty cycle. If the duty cycle is 100%, then the output will appear to be at maximum output. If the duty cycle is 50%, then the output will appear to be at half the maximum output; a duty cycle of 25% makes the output appear to be at a quarter of maximum. | + | Arduino achieves analog-like outputs by using a technique called **Pulse Width Modulation** (**PWM**). PWM is technique where a Boolean output is switched on and off very quickly (in the case of the Arduino Uno, it's about 490 times per second). The ratio of the time the output is in the HIGH state to the total period time (i.e., time HIGH + time LOW) is called the **duty cyle**. If the duty cycle is 100%, then the output will appear to be at maximum output. If the duty cycle is 50%, then the output will appear to be at half the maximum output; a duty cycle of 25% makes the output appear to be at a quarter of maximum. |
- | So one way that Arduino " | + | So one way that Arduino " |
+ | Check the documentation for your Arduino device to determine which outputs can be used as analog outputs and which can only be used as digital outputs. | ||
+ | |||
+ | <WRAP center round important 60%> | ||
+ | Arduino outputs are not unlimited super-sources! Each model has different specifications regarding the maximum current that a pin can output (or absorb). Exceeding these limits may permanently damage the Arduio microcontroller chip. | ||
+ | </ | ||
===== Example ===== | ===== Example ===== | ||
- | The following | + | The example below (taken verbatim from the [[http:// |
+ | |||
+ | <file c Fade.ino> | ||
+ | /* | ||
+ | | ||
+ | |||
+ | 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, | ||
+ | } | ||
+ | |||
+ | // the loop routine runs over and over again forever: | ||
+ | void loop() | ||
+ | // set the brightness of pin 9: | ||
+ | analogWrite(led, | ||
+ | |||
+ | // 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); | ||
+ | } | ||
+ | </ | ||
arduino/arduino_crash_course/analog_output.1352161201.txt.gz · Last modified: 2012/11/06 00:20 by mithat