Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: AnalogWrite vs DigitalWrite

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    14

    AnalogWrite vs DigitalWrite

    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);

  2. #2
    Senior Member
    Join Date
    Feb 2018
    Posts
    210
    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;
    }

  3. #3
    Junior Member
    Join Date
    Jan 2022
    Posts
    14
    Quote Originally Posted by lokki View Post
    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

  4. #4
    Senior Member
    Join Date
    Feb 2018
    Posts
    210
    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

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,408
    Quote Originally Posted by BrianDen View Post
    Would this make the output blink one time per second? Using teensy 4.0
    analogWriteFrequency(3, 1);
    Unfortunately no, 1 Hz PWM won't work.

    PWM is meant for kHz to MHz range and has a fairly high minimum frequency.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •