Problem with analogWrite

Hi all

I've a very strange problem... arduino crash with this code

int redPin2 = A0;
int redPin3 = A1;
int redPin4 = A2;
int redPin5 = A3;
int redPin6 = A4;
int redPin7 = A5;

void setup() {
// put your setup code here, to run once:
pinMode(redPin2, OUTPUT);
pinMode(redPin3, OUTPUT);
pinMode(redPin4, OUTPUT);
pinMode(redPin5, OUTPUT);
pinMode(redPin6, OUTPUT);
pinMode(redPin7, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
delay(10000);

analogWrite(redPin2, 128);
analogWrite(redPin3, 128);
analogWrite(redPin4, 128);

delay(5000);

analogWrite(redPin5, 128);
analogWrite(redPin6, 128);
analogWrite(redPin7, 128);
}

why?

It's all OK if I used only 4 PIN... when add another PIN... CRASH

thanks thanks
:frowning:
Ciao
Paolo

They're not PWM pins.
You're effectively doing a digitalWrite.

What do you mean by "CRASH"?

Thanks for your reply!!

I used Analog port (A0 to A5) ... it's equal.. right?

I apologize but I forgot to write that usage form. Arduino Nano 33 BLE.

I have also tried with digital PINs defined as PWM
(Arduino Nano 33 BLE — Arduino Official Store)

but the error remains.

//int redPin2 = A0;
//int redPin3 = A1;
//int redPin4 = A2;
//int redPin5 = A3;
//int redPin6 = A4;
//int redPin7 = A5;

int redPin2 = 3;
int redPin3 = 5;
int redPin4 = 6;
int redPin5 = 9;
int redPin6 = 10;
int redPin7 = A5;

void setup() {
// put your setup code here, to run once:
pinMode(redPin2, OUTPUT);
pinMode(redPin3, OUTPUT);
pinMode(redPin4, OUTPUT);
pinMode(redPin5, OUTPUT);
pinMode(redPin6, OUTPUT);
pinMode(redPin7, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
delay(10000);

analogWrite(redPin2, 128);
analogWrite(redPin3, 128);
analogWrite(redPin4, 128);

delay(5000);

analogWrite(redPin5, 128);
analogWrite(redPin6, 128);
//analogWrite(redPin7, 128);
}

Equal to what?

What do you mean by "CRASH"?

Have you got current limiting resistors on the LEDs?

Sorry, I didn't explain to you what I mean by CRASH

For CRASH I mean that the yellow LED starts flashing intermittently (with a slow sequence, 3 slow and 3 fast blinks)
in that state also the connection with the serial to carry out a new upload is blocked.
In that state also the serial is unreachable and the sketch can no longer be loaded.
For that I put a 10 second delay. that way just reset and reload the new sketch.

No now I only have the Arduino attached to the USB.

The A/Analog ports on an Arduino are Analog inputs on most Arduino's. The name analogWrite() is still pretty confusing but it applies to the PWM capable pins. Really whished they called it pwmWrite().

PS Please use code tags in you next post!

PPS What happened to poor redPin1? :o

PPPS TIP: Have a look at arrays :wink:

When analogWite(DPin, 8bitDutyCycle) function is executed, it automatically generates fixed frequency PWM signals at DPins (Digital Pin) indicated in the following diagram. The duty cycle of the signal changes in proportional to the values of the 2nd argument (8bitDutyCycle).
pwm328.png

pwm328.png

GM - did you take note of the fact that the OP says that he is using an Arduino Nano 33 BLE which does not have a 328 processor ?

UKHeliBob:
GM - did you take note of the fact that the OP says that he is using an Arduino Nano 33 BLE which does not have a 328 processor ?

I believed that the Platform is the Arduino; so, if ESP can behave like an UNO in some applications why not NANO 33 BLE? Sorry, I don't have that Kit; but, it looks like an ordinary NANO.
NANO BLE 33.jpg

NANO BLE 33.jpg

GolamMostafa:
I believed that the Platform is the Arduino; so, if ESP can behave like an UNO why not NANO 33 BLE? Sorry, I don't have that Kit; but, it looks like an ordinary NANO.
NANO BLE 33.jpg

Too much hassle to check the product spec page?

Nooo, not the confusing Nano again. Who does shit like that, name a product the same with a little suffix but be COMPLETELY different :confused:

But yeah, PWM pins (aka the ones you can use with analogWrite()) are D3, D5, D6, D9, D10, D11 and D12.

This excerpt is from Arduino Document, which says that NANO 33 BLE does support PWM signals at the following DPins:

5, 6, 9, 10, 3

nano33BLEPic.png

Nano33BLEPWM.png

Nano33BLEPWM.png

nano33BLEPic.png

Hi everyone, thanks again for your help.

I would like to tell you one thing the example code I wrote works correctly on an arduino uno R3 or Mega 2560 while it doesn't work on the Nano 33 BLE

int redPin2 = 3;
int redPin3 = 5;
int redPin4 = 6;
int redPin5 = 9;
int redPin6 = 10;
int redPin7 = 11;


void setup() {
  // put your setup code here, to run once:
pinMode(redPin2, OUTPUT);
pinMode(redPin3, OUTPUT);
pinMode(redPin4, OUTPUT);
pinMode(redPin5, OUTPUT);
pinMode(redPin6, OUTPUT);
pinMode(redPin7, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
delay(10000);

analogWrite(redPin2, 128);
analogWrite(redPin3, 255);
analogWrite(redPin4, 128);

delay(5000);

analogWrite(redPin5, 255);
analogWrite(redPin6, 128);
analogWrite(redPin7, 255);
}

I'm using analogWrite as specified in the Arduino documentation

:open_mouth:

"Does not work" is THE most crappy description of all times.... What DOES actually happen now you use PWM capable pins? What did you expect?

And to leave nothing to doubt, how are the pins wired?

Hi septillion

The code is obviously an example code and what I expect is simple.
Using the analogWrite function on the outputs A0 ... A5 by setting 128 as a value, I expect the relative output to deliver (3.3 / 2) 1.6 volts "ON ALL 6 PINS".

This is what I expect.

What happen? I've already explained it before, I try to repeat myself.

If I only use 4 PINs, then in the code I only put 4 analogWrite, everything works, the code is loaded and from the related PINs I see the volts I expect.
If I use 5 PINs, so in the code put 5 analogWrite, code is in block, the serial communication LED starts to flash abnormally and, above all, it disconnects and it is no longer possible to load sketch.

here's all this behavior NOT and I repeat NOT happen on an arduino one.

I wouldn't want it to be a limitation of the Nano 33 BLA.

Ciao

pallo100:
Using the analogWrite function on the outputs A0 ... A5 by setting 128 as a value

Like we said, can't do! PWM capable (aka analogWrite()-capable) pins are D3, D5, D6, D9 and D10, D11 and D12.

pallo100:
What happen? I've already explained it before, I try to repeat myself.

That was before you made changes to the code. Especially after changing to pins that ARE capable of PWM I'm interested if you even tested that :slight_smile:

And to be clear, the problem must happen with the example code you post :slight_smile: If that's not the case I would like to point you to here.

GolamMostafa:
This excerpt is from Arduino Document, which says that NANO 33 BLE does support PWM signals at the following DPins:

5, 6, 9, 10, 3

nano33BLEPic.png

Nano33BLEPWM.png

Odd that the pinout shows five PWM pins, but the back of the box shows six.
Looking at the schematic, D12/MISO is shown as a PWM output, so the PWM pins would be 3, 5, 6, 9, 10, and 12.

david_2018:
Odd that the pinout shows five PWM pins, but the back of the box shows six.
Looking at the schematic, D12/MISO is shown as a PWM output, so the PWM pins would be 3, 5, 6, 9, 10, and 12.

I also expected that DPin-11 should be there to remain in line with UNO. However, the product spec shows only 5 DPins for PWMs.