Combination of digitalWrite(LEDR, HIGH); and analogWrite(LEDR, 0);

Hi all,

I am very new to microcontrollers and Arduino. So I started experimenting with the LED of WiFiNINA,h.
I expected my code

#include <WiFiNINA.h>

void setup() {

  Serial.begin(9600);

  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);

  // LEDs aus
  digitalWrite(LEDR, LOW);
  digitalWrite(LEDG, LOW);
  digitalWrite(LEDB, LOW);
}

void loop() {

    digitalWrite(LEDR, HIGH);
    delay(500);
    //digitalWrite(LEDR, LOW);

    analogWrite(LEDR, 0);
    delay(500);

  exit(0);
}

to switch LEDR on for 500ms.
But the result I see is, that LEDR is switched on but not switched off by

analogWrite(LEDR, 0);

Can u please explain why LEDR is not switched off, and how to switch off by

analogWrite();

Thanks!

Some additional infos:
I installed latest WiFiNINA lib (1.8.14)

Vice versa using of the functions

    analogWrite(LEDR, 1);
    delay(500);

    digitalWrite(LEDR, LOW);
    delay(500);

does not work as expected also.

I have no explanation of why it does not work but I do wonder why you want to do it. Why not be consistent and use analogWrite() or digitalWrite() for both ?

As I've written above: I am just experimenting ...

Fair enough

So you have your answer to the experiment then.

Indeed, but I am looking for answers for my questions in my initial post.

So if anyone other than me is interested in the answers:

Me:
Hi Bard, i am running this code on Arduino Nano RP2040 Connect:

#include <WiFiNINA.h>

void setup() {
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);

  digitalWrite(LEDR, LOW);
  digitalWrite(LEDG, LOW);
  digitalWrite(LEDB, LOW);
}

void loop() {

  analogWrite(LEDR, 1);
  delay(500);

  digitalWrite(LEDR, LOW);
  delay(500);
}

Why does the function call digitalWrite(LEDR, LOW); not switch off LEDR?

Google Bard:
There are two reasons why the function call digitalWrite(LEDR, LOW); doesn't switch off LEDR in the provided code:

  1. PWM (Pulse Width Modulation): The function analogWrite(LEDR, 1); sets the LEDR pin to PWM mode. In PWM mode, the pin is not fully turned on or off, but rather modulated to produce a varying voltage level. This allows for more precise control over the brightness of an LED or the speed of a motor.
  2. Prioritization: When multiple functions try to control the same pin, the function with the highest priority takes precedence. In this case, the analogWrite(LEDR, 1); call has a higher priority than the digitalWrite(LEDR, LOW); call. This means that the PWM signal from analogWrite() will override the setting from digitalWrite(), keeping the LED partially on even when digitalWrite() tries to turn it off.

To effectively switch off the LED, you would need to either:

  1. Turn off PWM Mode: Use digitalWrite(LEDR, LOW); before calling analogWrite(LEDR, 1); to ensure that the pin is not in PWM mode. This will prevent the PWM signal from overriding the digitalWrite() command.
  2. Use analogWrite(LEDR, 0);: This will set the PWM signal to 0, effectively turning off the LED. However, this will not allow you to control the brightness of the LED.

I hope this helps!

What does Bard say if you tell it that the code is running on an Arduino Uno ?

Same Question with Arduino Uno:

Google Bard:
The reason why the function call digitalWrite(LEDR, LOW); does not switch off LEDR is because analogWrite() function overrides the pinMode().

The analogWrite() function is used to control the brightness of an LED by setting the duty cycle of the signal. The duty cycle is the percentage of time that the pin is high. For example, a duty cycle of 100% means that the pin is high all the time, while a duty cycle of 0% means that the pin is low all the time.

When you call analogWrite(LEDR, 1), you are setting the duty cycle of LEDR to 100%. This means that the pin is essentially always high, and the LED will be on.

The digitalWrite() function, on the other hand, is used to set the state of a pin to either HIGH or LOW. When you call digitalWrite(LEDR, LOW), you are setting the state of LEDR to LOW. However, since the pin is already set to HIGH by analogWrite(), the digitalWrite() function has no effect.

To turn off LEDR, you need to remove the analogWrite() function call. This will allow the digitalWrite() function to set the state of the pin to LOW and effectively turn off the LED.

Here is the corrected code:

#include <WiFiNINA.h>

void setup() {
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
}

void loop() {
  digitalWrite(LEDR, HIGH); // turn on LEDR
  delay(500);

  digitalWrite(LEDR, LOW); // turn off LEDR
  delay(500);
}

No mention of the fact that the WiFiNINA library is #included and that the code will therefore not compile

Really ?

No mention either that the analogWrite() function of the Uno actually calls digitalWrite() if values of 0 or 255 are used or that it sets the pinMode() of the pin to OUTPUT

1 Like

Bard is very sorry. It want you to know:
"
My apologies for overlooking the inclusion of the WiFiNINA library in the provided code. The inclusion of the WiFiNINA library is indeed problematic, as the Arduino Uno does not support WiFi connectivity natively. Attempting to use the WiFiNINA library with an Arduino Uno will result in compiler errors due to missing hardware support.

To address this issue, you can either remove the inclusion of the WiFiNINA library or select a different board that supports WiFi connectivity, such as the Arduino Uno WiFi Rev2. If you choose to remove the WiFiNINA library, the provided code will function as intended, controlling the LEDs as described.
"

The point that I was making is that Bard can get things wrong. Once you realise this then it becomes more difficult to believe everything it says

3 Likes

It is clear that AI is not to way to look for a why, type question.

1 Like