Pages: [1]   Go Down
Author Topic: "analogWrite" vs "digitalWrite"  (Read 2861 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the difference between analogWrite() and digitalWrite()?  I will show two different blocks of code that gave me the exact same output on my hardware. There are the codes;

int led=1, brightness=255;
pinmode (led, brighness);
digitalwrite(led, brightness);

and

int led=1;
pinmode(led, output);
digitalwrite(led, high);

When I write these codes I can change digital to analog and get the exact same results. Why is this? What is the difference? Is one preferable over the other?

and yes i know that half the code is missing and that there are no caps, but you should be able to understand what I'm trying to say.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Pin 1 is not a PWM pin (3, 5, 6, 9, 10, and 11 are on the UNO).  For a non-PWM pin, analogWrite() with a value below 128 is tha same a digitalWrite(LOW) and analogWrite() with a value above 127 is tha same a digitalWrite(HIGH).

You don't need pinMode() for PWM pins.  The analogWrite() function takes care of that.

The PWM value 255 is full brightness.  You won't see a difference between that and digitalWrite(HIGH).
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 206
Posts: 12859
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


@alexval1323, please do not cross-post.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The PWM value 255 is full brightness.
That depends how the LED is wired.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: