Use digitalWrite() and analogWrite() on same pin

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
PhilippCaspari
 
Posts: 11
Joined: Tue Nov 09, 2021 6:04 pm

Use digitalWrite() and analogWrite() on same pin

Post by PhilippCaspari »

Hey folks,

I have old code that I previously used on an Arduino Nano succesfully. Now I wanted to transition to an "Adafruit Feather nRF52840 express", but the board does not execute the following commands, like the Nano did. Here is the code:

Code: Select all

#include <Adafruit_TinyUSB.h>

void setup() {
  pinMode(2, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(2, HIGH);
  delay(5000);
  Serial.println("Ontime over, now Dim");
  analogWrite(2, 64);
  delay(2500);
  Serial.println("Dimtime over, now ON");
}
I check the code with an LED on the output pin. The pin stays always in the same state of the first command, but does not change with the command. I seems like I have to decide, if I want to use an output pin only for PWM Signals, or only for HIGH and LOW pin states. Changing between analog and digital output does not work like on arduino, why is that the case?

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Use digitalWrite() and analogWrite() on same pin

Post by dastels »

Well, it's a completely different MCU with completely different pin management circuitry.

Analog writing the value for 100% PWM duty cycle (usually 255) will do what you're trying to do by digital writing HIGH.

Dave

User avatar
PhilippCaspari
 
Posts: 11
Joined: Tue Nov 09, 2021 6:04 pm

Re: Use digitalWrite() and analogWrite() on same pin

Post by PhilippCaspari »

Thank you for your quick reply Dave :) So there is no possibility to switcht between digital output and analog output on a pin, once one of these methods is previously used on a pin?

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”