# Analog Output (PWM)

#### Circuitry

You are now going try a more advanced method of controlling LEDs. So far, you have simply turned the LED on or off. Would you like to adjust the brightness of an LED? Can you do that with an Arduino? Yes, you can.

The circuit for this example is the same as of our first example: #### Code

Now enter the code:

Plug in the Arduino, click Verify, then Upload if no errors are found. The brightness of the breadboard LED should smoothly increase, then smoothly decrease and then repeat.

#### Understanding the Code

• Most of the code should be easy to figure out once you have understood the previous examples.
• The meat of the program is in the for loop. The process inside this loop is carried for every angle ranging from to 179°.
• The angle is converted to radians using the in-built `radians(angle°)` function.
• The sine value thus calculated ranges from 0 to 1.  This is multiplied by 255 to get a value between 0 and 255. The Arduino’s analog output is 8-bit and requires a value between 0 (LOW) and 255 (HIGH).
• The obtained value is then typecasted to an integer in the same line using `int()`
• The `analogWrite(ledPin, ledVal);` statement writes `ledVal` to the specified `ledPin`.

The concept here is that you are creating a sine wave (positive half-cycle of course) and having the brightness of the LED follow the path of that wave. This is what makes the light pulsate instead of just flare to full brightness and fade back down again.

#### Pulse Width Modulation

Six pins (3,5,6,9,10,11) on the Arduino UNO board are marked as PWM pins. These pins differ from the remaining digital pins in that they are able to send out a PWM signal. PWM stands for Pulse Width Modulation, which is a technique for getting analog results from digital means. On these pins, the Arduino sends out a square wave by switching the pin on and off very fast. The pattern of on/offs can simulate a varying voltage between 0 and 5v. It does this by changing the amount of time that the output remains high (on) versus off (low). The duration of the on time is known as the pulse width.

For example, if you were to send the value 0 out to Digital PWM Pin 11 using analogWrite(), the ON period would be zero, or it would have a 0 percent duty cycle. If you were to send a value of 64 (25 percent of the maximum of 255) the pin would be ON for 25 percent of the time and OFF for 75 percent of the time. The value of 191 would have a duty cycle of 75 percent; a value of 255 would have a duty cycle of 100 percent.

The pulses run at a speed of approximately 500Hz or 2 milliseconds each. So, in your sketch, the LED is being turned on and off very fast. If the Duty Cycle was 50% (a value of 127), the LED would pulse on and off at 500Hz and would display at half the maximum brightness. This is basically an illusion that you can use to your advantage by allowing the digital pins to output a simulated analog value to your LEDs.

Note that even though only six of the pins have the PWM function, you can easily write software to give a PWM output from all of the digital pins if you wish.
{"ft_lang_label":"__label__en","ft_lang_prob":0.941787,"math_prob":0.9215194,"size":3060,"snap":"2023-14-2023-23","text_gpt3_token_len":728,"char_repetition_ratio":0.11747382,"word_repetition_ratio":0.010452962,"special_character_ratio":0.24117647,"punctuation_ratio":0.08722741,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98086655,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-05-28T21:55:42Z\",\"WARC-Record-ID\":\"<urn:uuid:6ae892f5-1685-446a-9cdd-5c67aeebd479>\",\"Content-Length\":\"18046\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0dd9593d-83f5-4732-94cc-e0f8ea691e75>\",\"WARC-Concurrent-To\":\"<urn:uuid:f534e549-75a7-42fa-b92a-ab085b9b9396>\",\"WARC-IP-Address\":\"172.67.179.196\",\"WARC-Target-URI\":\"https://www.robotix.in/tutorial/arduino/anaout/\",\"WARC-Payload-Digest\":\"sha1:UZZBO5BHZ5QZZCUDMUA7KHHODN6NRERZ\",\"WARC-Block-Digest\":\"sha1:WWASLDWZM6GZD2WOZHGG2CAY3TEVQ6OB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224644571.22_warc_CC-MAIN-20230528214404-20230529004404-00235.warc.gz\"}"}