AnalogWrite vs DigitalWrite

BrianDen

Member
Is there any difference when using:
digitalWrite(3, HIGH); vs analogWrite(3, 256); ?

Would this make the output blink one time per second? Using teensy 4.0
analogWriteFrequency(3, 1);
PinMode(3, OUTPUT);
analogWrite(3, 128);
 
analogWrite is 10bit by default i believe, so a value of 256 would be rather dim even more so a value of 128.

if you want to blink a LED (just on and off) it is simpler to use digitalWrite (the blink example)

Code:
/*
  Blink ADAPTED with elapsedMillis for nonblocking blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);                      // wait for a second
}

if you need it non blocking, you can use an elapsedMillis variable and check it's value to decide if the LED should be on or off i.e. something like this (not tested)

Code:
/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
// time the LED blink with a elapsedMillis variable
elapsedMillis check_seconds = 0;
bool on_off = 0;
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  // turn the LED on (HIGH is the voltage level)
if ((check_seconds < 500) && !on_off) {
    digitalWrite(LED_BUILTIN, HIGH);
    on_off = 1;
    }
  
if ((check_seconds > 500) && on_off) {                      
    digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
    on_off = 0;                      
}

if (check_seconds >= 1000) check_seconds = 0;
}
 
analogWrite is 10bit by default i believe, so a value of 256 would be rather dim even more so a value of 128.

if you want to blink a LED (just on and off) it is simpler to use digitalWrite (the blink example)

Code:
/*
  Blink ADAPTED with elapsedMillis for nonblocking blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);                      // wait for a second
}

if you need it non blocking, you can use an elapsedMillis variable and check it's value to decide if the LED should be on or off i.e. something like this (not tested)

Code:
/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
// time the LED blink with a elapsedMillis variable
elapsedMillis check_seconds = 0;
bool on_off = 0;
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  // turn the LED on (HIGH is the voltage level)
if ((check_seconds < 500) && !on_off) {
    digitalWrite(LED_BUILTIN, HIGH);
    on_off = 1;
    }
  
if ((check_seconds > 500) && on_off) {                      
    digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
    on_off = 0;                      
}

if (check_seconds >= 1000) check_seconds = 0;
}

My outputs control mosfets, where some drice 15a load. I would like to use soft start so devices dont get full voltage immediatly. So i would like to use pwm to drive them up to full voltage
 
sure, then use the analogwrite and ramp it up. not sure why you asked about analogwrite being similar to digitalwrite then? since you want the ramp up it should actually not behave like digitalwrite :)
 
Back
Top