

Pins 3 and 11 on the Leonardo also run at 980 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz. The frequency of the PWM signal on most pins is approximately 490 Hz. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). Can be used to light a LED at varying brightnesses or drive a motor at various speeds.

Writes an analog value (PWM wave) to a pin. Once you do that the timer hardware outputs the requested duty cycle (from 0 to 255) where 0 is always off, 255 is always on, and some value inbetween gives you PWM (pulsed output).įor more information about the timers see my page about timers.
#Arduino analogwrite set to 0 code
This is proven by the relevant code in wiring_analog.c: if (val = 0)ĪnalogWrite basically configures the hardware timers to output PWM. Now the output is off for 8 µs - compared to the earlier image where it was on for 8 µs.ĪnalogWrite (0) is the same as digitalWrite (LOW).ĪnalogWrite (255) is the same as digitalWrite (HIGH). The output is HIGH all the time except for a brief period. Now you can see that the output is HIGH exactly half the time, and LOW the rest of the time. Let's try analogWrite(127) - half-way from 0 to 255: The output is high for 8 µs, which is exactly 1/256 of 2048 µs which is the period of the timer. You also see that the frequency is 490 Hz which is what the reference page for analogWrite says it will be. You can see that the voltage level is 0V most of the time, and going to 5V for short periods. Here's a screenshot of digitalWrite (LOW):ĪnalogWrite really should have been named PWMwrite since it configures the processor timers to output PWM (pulse-width modulation). For a Uno or Mega that would be 0V or 5V (or close to it). PinMode(ledPin, OUTPUT) // sets the digital pin as outputĭigitalWrite(ledPin, HIGH) // sets the LED onĭigitalWrite(ledPin, LOW) // sets the LED offĭigitalWrite sets the output pin to either LOW or HIGH (where those voltages depend on the V cc of the processor. Here, 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.Įxample Code: int ledPin = 13 // LED connected to digital pin 13 Value = analogRead(analogIN) // read the value of analogIN (values between from 0 to 1023)ĪnalogWrite(outLed, value/4) // sets the read value on outLed (values between from 0 to 255)ĭigitalWrite: The digitalWrite() method sets the value of a digital pin as HIGH or LOW.

PinMode(outLed, OUTPUT) // set the PWM pin as OUTPUT Int value = 0 //variable to store the read value Val: int value of duty cycle between from 0(always off) to 255(always on)Įxample Code: int outLed = 10 //LED connected to digital pin 10 The analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time). AnalogWrite(): The analogWrite() method sets the value of a PWM output pin.
