Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. analogWrite only works on pins 9 and 10; on all other pins it will write a digital value of 0 or 5 volts.
pin: the pin to write to.
value: the value between 0 and 255. 0 corresponds to constant low output (no voltage); 255 is a constantly on output (5 volts). For values in-between, the pin rapidly alternates between 0 and 5 volts - the higher the value, the more often the pin is high (5 volts).
nothing
Analog pins unlike digital ones, do not need to be declared as INPUT nor OUTPUT
int ledPin = 9; // LED connected to digital pin 9
int analogPin = 3; // potentiometer connected to analog pin 3
int val = 0; // variable to store the read value
void setup()
{
pinMode(ledPin, OUTPUT); // sets the pin as output
}
void loop()
{
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}
Sets the output to the LED proportional to the value read from the potentiometer.