Thursday, July 21, 2011

Arduino Mega 2560 - use of pins


pins 2 to 13: Provide 8-bit pulse width modulation (PWM) output with the analogWrite(pin, value) function where value is between 0 (off) and 255 (full on). Used to light a LED at varying brightnesses or drive a motor at various speeds.The frequency of the PWM signal is approximately 490 Hz.


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
}