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
}


As an Amazon Associate I earn from qualifying purchases.

No comments:

Post a Comment

Post Scriptum

The views in this article are mine and do not reflect those of my employer.
I am preparing to cancel the subscription to the e-mail newsletter that sends my articles.
Follow me on:
X.com (Twitter)
LinkedIn
Google Scholar

Popular Recent Posts

Most Popular Articles

apt quotation..