digitalWrite does not work on UNO R4 Wifi

Hello,

I'm new to Arduino and currently doing some of the courses in the "Student Kit - Lesson Dimmer Switch - Code Creation – Blinking Indicator" “Lesson4_V4”.

In this activity, 'else if conditional structure' is introduced, and I have tried everything to get this to run on my Arduino R4 UNO WIFI, but I just can't get 'digitalWrite()' to work properly with the following code:

int readValue = 0;
int writeValue = 0;
void setup() {
  pinMode(9, OUTPUT);             // declare the first LED pin as output
  pinMode(10, OUTPUT);            // declare the second LED pin as output
  pinMode(11, OUTPUT);            // declare the third LED pin as output
  Serial.begin(9600);
}
 
void loop() {
  readValue = analogRead(A0);     // store the value from the potentiometer
  writeValue = readValue / 4;     // divide the readValue by 4 and store as the writeValue
  if (readValue < 300) {          // if the value from the potentiometer is less than 300 then:
    digitalWrite(9,LOW);          // turn off the first LED
    digitalWrite(10,LOW);         // turn off the second LED
    digitalWrite(11,LOW);         // turn off the third LED
  }
  else if (readValue < 600) {     //if the value from the potentiometer is less than 600 then:
    analogWrite(9, writeValue);   // turn on the first LED
    digitalWrite(10,LOW);         // turn off the second LED
    digitalWrite(11,LOW);         // turn off the third LED
  }
  else if (readValue < 900) {     //if the value from the potentiometer is less than 900 then:
    analogWrite(9, writeValue);   // turn on the first LED
    analogWrite(10, writeValue);  // turn on the second LED
    digitalWrite(11,LOW);         // turn off the third LED
  }
  else {
    analogWrite(9, writeValue);   // turn on the first LED
    analogWrite(10, writeValue);  // turn on the second LED
    analogWrite(11, writeValue);  // turn on the third LED
  }
  Serial.print(readValue);
  Serial.print(" : ");
  Serial.println(writeValue);
  delay(100);
}

With this code the Arduino lights up the LED's in sequences according to my 'if' and 'else if' statements for 300, 600 and 900 turn on 1st, 2nd and 3rd LED. But when the potentiometer is dialed down they remain ON.

After some troubleshooting I went to tinkercad to set up the same configuration on a virtual Arduino UNO R3, with the exact same wiring and code it runs without any issues.

So I adjusted my code for my R4 UNO by instead using analogWrite(PIN,0); instead of digitalWrite(PIN,LOW);

int readValue = 0;
int writeValue = 0;
void setup() {
  pinMode(9, OUTPUT);             // declare the first LED pin as output
  pinMode(10, OUTPUT);            // declare the second LED pin as output
  pinMode(11, OUTPUT);            // declare the third LED pin as output
  Serial.begin(9600);
}
 
void loop() {
  readValue = analogRead(A0);     // store the value from the potentiometer
  writeValue = readValue / 4;     // divide the readValue by 4 and store as the writeValue
  if (readValue < 300) {          // if the value from the potentiometer is less than 300 then:
    analogWrite(9,0);          // turn off the first LED
    analogWrite(10,0);         // turn off the second LED
    analogWrite(11,0);         // turn off the third LED
  }
  else if (readValue < 600) {     //if the value from the potentiometer is less than 600 then:
    analogWrite(9, writeValue);   // turn on the first LED
    analogWrite(10,0);         // turn off the second LED
    analogWrite(11,0);         // turn off the third LED
  }
  else if (readValue < 900) {     //if the value from the potentiometer is less than 900 then:
    analogWrite(9, writeValue);   // turn on the first LED
    analogWrite(10, writeValue);  // turn on the second LED
    analogWrite(11,0);         // turn off the third LED
  }
  else {
    analogWrite(9, writeValue);   // turn on the first LED
    analogWrite(10, writeValue);  // turn on the second LED
    analogWrite(11, writeValue);  // turn on the third LED
  }
  Serial.print(readValue);
  Serial.print(" : ");
  Serial.println(writeValue);
  delay(100);
}

With this updated code it runs without any issues on my R4 UNO WIFI and on the virtual R3 UNO. Both turning LED's ON for respective values and turning them OFF. But I still don't understand why the digitalWrite(PIN,LOW); does work on R3 UNO but not on R4 UNO WIFI?

Welcome to the forum

What are you seeing or not seeing that leads you to the conclusion that digitalWrite() does not work ?

I know for a fact that it it does work so the problem is likely to be in your code or circuit. How is the potentiometer connected ? What do you see when you print the value of readValue and writeValue ?

See the following:
Calls to digitalWrite() don't work after a previous analogWrite() #58

Thank you UKHeloBob!

Unfortunately, I fail to see how this could be an issue due to circuit/wiring.

Leading to my conlcusion: If this was a physical limitation due to wiring this issue would be consistent to both scenarios; running analogWrite(); and digitalWrite(); as both have the same conditions & connected to the same circuit:

  if (readValue < 300) {          // if the value from the potentiometer is less than 300 then:
    digitalWrite(9,LOW);          // turn off the first LED
    digitalWrite(10,LOW);         // turn off the second LED
    digitalWrite(11,LOW);         // turn off the third LED
  if (readValue < 300) {          // if the value from the potentiometer is less than 300 then:
    analogWrite(9,0);          // turn off the first LED
    analogWrite(10,0);         // turn off the second LED
    analogWrite(11,0);         // turn off the third LED

I see the "readValue " : " writeValue" in SM, when readValue exceed the parameters 300, 600, 900; LED1, LED2 and LED3 turn on respectively for both devices (R3 Uno and R4 Uno Wifi) but when readValue decrease below the parameters the LED's only turn off on R3 Uno with the same circuit/wiring.

This led me to believe the code was cause of the issue; hence I copy/pasted the code provided from the official Arduino Course but I still receive this issue on my R4 Uno.

The TinkerCad below is what I used to troubleshoot with, where I don't have any issues running this setup (circuit + code). But running the same thing doesn't work on my R4 Wifi, until I adjust the code with analogWrite(); instead of digitalWrite();

Bear in mind that you never said how the problem manifested itself so all bets are off as far as I am concerned. For instance, do you have current limiting resistors in series with the LEDs and are they wired to 5V or GND and we have no idea what pin A0 is connected to

Thank you!

So if I'm understanding correctly, you can either call 'pinMode()' again directly in the 'void loop() {}' if a digitalWrite() is to be used after a generated PWM on a pin with analogWrite().

Otherwise it doesn't work for UNO R4.

By declaring

int readValue = 0;
int writeValue = 0;
void setup() {
  pinMode(9, OUTPUT);             // declare the first LED pin as output
  pinMode(10, OUTPUT);            // declare the second LED pin as output
  pinMode(11, OUTPUT);            // declare the third LED pin as output
  Serial.begin(9600);
}
 
void loop() {
  readValue = analogRead(A0);     // store the value from the potentiometer
  writeValue = readValue / 4;     // divide the readValue by 4 and store as the writeValue
  if (readValue < 300) {          // if the value from the potentiometer is less than 300 then:
    digitalWrite(9,LOW);          // turn off the first LED
    digitalWrite(10,LOW);         // turn off the second LED
    digitalWrite(11,LOW);         // turn off the third LED
      pinMode(9, OUTPUT);             // declare the first LED pin as output
      pinMode(10, OUTPUT);            // declare the second LED pin as output
      pinMode(11, OUTPUT);            // declare the third LED pin as output
  }
  else if (readValue < 600) {     //if the value from the potentiometer is less than 600 then:
    analogWrite(9, writeValue);   // turn on the first LED
    digitalWrite(10,LOW);         // turn off the second LED
    digitalWrite(11,LOW);         // turn off the third LED
      pinMode(10, OUTPUT);            // declare the second LED pin as output
      pinMode(11, OUTPUT);            // declare the third LED pin as output
  }
  else if (readValue < 900) {     //if the value from the potentiometer is less than 900 then:
    analogWrite(9, writeValue);   // turn on the first LED
    analogWrite(10, writeValue);  // turn on the second LED
    digitalWrite(11,LOW);         // turn off the third LED
      pinMode(11, OUTPUT);            // declare the third LED pin as output
  }
  else {
    analogWrite(9, writeValue);   // turn on the first LED
    analogWrite(10, writeValue);  // turn on the second LED
    analogWrite(11, writeValue);  // turn on the third LED
  }
  Serial.print(readValue);
  Serial.print(" : ");
  Serial.println(writeValue);
  delay(100);
}

After testing the code above on my R4 Uno I can testify it now runs without issue after a generated PWM on a pin with analogWrite() , and a subsequent digitalWrite().

But why bother, your code with the analogWrites works and is simpler.

Thank you jim-p

This is a very good question, let me divide this question in to two answers,

  1. As you mentioned it doesn't make a difference between either using analogWrite(); (or in the case of R4 Uno), using digitalWrite(); with recalling the pinMode in the void loop:, with the second option of course being more complicated and unnecessary, but bear in mind. This was unknown to me when I did the the Arduino Course.

In the Arduino course, this is exactly what is described:

Note: Besides using digitalWrite(pin, LOW) to turn the LEDs off, you could also use the command analogWrite(pin, 0) . Either command will have the exact same effect on the LEDs.

https://studentkit.arduino.cc/studentkit/module/student-kit/lesson/dimmer-switch

It wasn't until it didn't work, I started to troubleshoot on WHY it did not work. I checked the wiring, the code, even my Arduino IDE FW and COM-port and reinstalled the IDE until I realised that this this HAS to be a difference on how the R3 UNO and R4 UNO interpret the digitalWrite(); command.

  1. Yes, why bother.. Would have been easier to just use the analogWrite(); and leave it as such. But then again, following the course, believing I did everything exactly as stated and NOT understanding WHY it still doesn't work did not sit right with me... I could just not let go why my code didn't work and continue on to the next course..

In the future I'll definitely use analogWrite(); in these cases but I'm glad to know find out WHY the code didn't work as described in the course pages. Almost went mad not figuring it out and I'm only getting started in the beginner courses. :grinning_face_with_smiling_eyes:

Big thanks to all of you for the help understanding this issue!

OK I see. You assumed that because it worked on an Uno R3 it should work on any Arduino. Well as you have found out that is not always true.

The course does specify an Uno R3