{"id":6931,"date":"2021-12-23T06:23:33","date_gmt":"2021-12-23T06:23:33","guid":{"rendered":"https:\/\/www.qec.tw\/?p=6931"},"modified":"2021-12-28T09:26:10","modified_gmt":"2021-12-28T09:26:10","slug":"analogwrite","status":"publish","type":"post","link":"https:\/\/www.qec.tw\/zh\/86duino-reference\/analogwrite\/","title":{"rendered":"analogWrite()"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">[Analog I\/O]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Description<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Writes an analog value (<a href=\"http:\/\/arduino.cc\/en\/Tutorial\/PWM\" target=\"_blank\" rel=\"noopener\">PWM wave<\/a>) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to\u00a0<code>analogWrite()<\/code>, the pin will generate a steady square wave of the specified duty cycle until the next call to\u00a0<code>analogWrite()<\/code>\u00a0(or a call to\u00a0<code>digitalRead()<\/code>\u00a0or\u00a0<code>digitalWrite()<\/code>\u00a0on the same pin). On the 86Duino, the frequency of the PWM signal is 1000 Hz.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On the 86Duino Zero, this function works on pins 3, 5, 6, 9, 10, 11, and 13. On the 86Duino EduCake, it works on pins 3, 5, 6, 9, 10, 11, 13, 31, and 32. On the 86Duino One, it works on pins 3, 5, 6, 9, 10, 11, 13, 29, 30, 31, and 32. On the 86Duino PLC, it works on pins 1, 3, 4, 7.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You do not need to call\u00a0<code>pinMode()<\/code>\u00a0to set the pin as an output before calling\u00a0<code>analogWrite()<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The\u00a0<em>analogWrite<\/em>\u00a0function has nothing to do with the analog input pins or the\u00a0<em>analogRead<\/em>\u00a0function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>analogWrite(pin, value)<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Parameters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pin<\/code>: the pin to write to.<br><code>value<\/code>: the duty cycle \u2013 between 0 (always off) and 255 (always on) by default.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can call\u00a0<code>analogWriteResolution()<\/code>\u00a0to set a higher PWM resolution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Returns<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">nothing<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example Code<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sets the output to the LED proportional to the value read from the potentiometer.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>int ledPin = 9;      \/\/ LED connected to digital pin 9\nint analogPin = 3;   \/\/ potentiometer connected to analog pin 3\nint val = 0;         \/\/ variable to store the read value\n \nvoid setup()\n{\n  pinMode(ledPin, OUTPUT);   \/\/ sets the pin as output\n}\n \nvoid loop()\n{\n  val = analogRead(analogPin);   \/\/ read the input pin\n  analogWrite(ledPin, val \/ 4);  \/\/ analogRead values go from 0 to 1023, \n                                 \/\/ analogWrite values from 0 to 255\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">See also<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>[Language]\u00a0<a href=\"https:\/\/www.qec.tw\/86duino-reference\/analogread\/\">analogRead<\/a>()<\/li><li>[Language]\u00a0<a href=\"https:\/\/www.qec.tw\/86duino-reference\/analogwriteresolution\/\">analogWriteResolution<\/a>()<\/li><li>[Example]\u00a0<a href=\"http:\/\/arduino.cc\/en\/Tutorial\/PWM\" target=\"_blank\" rel=\"noopener\">Tutorial: PWM<\/a><\/li><\/ul>\n\n\n<hr \/>\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.qec.tw\/86duino\/86duino-language-reference\/\">Language Reference Home<\/a><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em>The text of the 86Duino reference is a modification of&nbsp;<a href=\"http:\/\/arduino.cc\/en\/Reference\/HomePage\" target=\"_blank\" rel=\"noopener\">the Arduino reference<\/a>&nbsp;and is licensed under a&nbsp;<a href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/3.0\/\" target=\"_blank\" rel=\"noopener\">Creative Commons Attribution-ShareAlike 3.0 License<\/a>. Code samples in the reference are released into the public domain.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Analog I\/O] Description Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6932,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[195],"tags":[162],"class_list":["post-6931","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-86duino-reference","tag-86duino"],"_links":{"self":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/6931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/comments?post=6931"}],"version-history":[{"count":0,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/6931\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media\/6932"}],"wp:attachment":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media?parent=6931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/categories?post=6931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/tags?post=6931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}