Simple sketch locks board

Greetings. My son made this sketch to control some LED's. When uploaded, the program becomes stuck and the board becomes unrecognized ("port" greyed out in the IDE). It requires a double press of the reset button.

This appears to be related somehow to the analog outputs on pins A3, A6, and A7. If these are commented out (the analogWrite lines), the code runs fine. If any one of them is uncommented it runs fine, but if more than one (any two) are uncommented, then it returns to the locked state. The three LED's with random inputs will just remain at a fixed value, and the first of the analogWrite statements on pins A3, A6, and A7 will run, the other two pins will be off.

We are new to working with Arduino, maybe our lack of basic knowledge is the issue, but I don't see any problem with using these outputs (?)

int ledPin1 = A0; // pin 4
int ledPin2 = A1; // pin 5
int ledPin3 = A2; // pin 6
int ledRed = A3;  // pin 7
int ledGreen = A6; // pin 10
int ledBlue = A7; // pin 11

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledBlue, OUTPUT);

  delay(1000);
}

void loop() {
  analogWrite(ledPin1, random(120)+135);
  analogWrite(ledPin2, random(120)+135);
  analogWrite(ledPin3, random(120)+135);
  analogWrite(ledRed, 80);
  analogWrite(ledGreen, 1);
  analogWrite(ledBlue, 80);
  delay(random(200));
}

There are no analog outputs. The outputs are digital (on and off). The analogWrite Function creates a PWM signal using a timer. See Arduino Reference

https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

The same timer is used for delay. That might be a bit too much for the library to handle. Try increasing the values.

Thanks Klaus.

That information is helpful. I decided to simplify further and just try to blink an external led with one of the digital pins. I think either (a) we have a bad board, or (b) somehow the board was damaged.

I cannot read any voltage on any digital pins when the pin is set to HIGH. For example, the following code, using any of the digital pin numbers results in nothing happening. We have resistors on the LED's (220) is it possible we have a bad board? Maybe I misunderstand how to call the pin I want to turn on?

void setup() {
  pinMode(20, OUTPUT);
  delay(1000);
}

void loop() {
  digitalWrite(20, HIGH);
}

Edit: following up from my previous post... I had a Teensy sitting around, so tried the original sketch on it and it worked as expected. I used digital pins capable of PWM to accomplish the varying LED intensity. This leaves the question - is my Sense BLE bad (or got fried somehow)? The IMU seems to be working fine, and the IO on the analog pins as well. Are there any other possibilities that would leave ALL of the digital pins not responding?

The pin numbering documentation for the Arduino is not as clear as the pin diagram printed on the paper card you get with the Teensy.

To find the pin numbers and pins, you can have a look at the following files

  • pins_arduino.h
  • variant.cpp

both are in the following folder

C:\Users\YourUserName\AppData\Local\Arduino15\packages\arduino\hardware\mbed\1.1.3\variants\ARDUINO_NANO33BLE\

and the pinout diagram

Here is a simple program you can use to test your pins. It will enable each pin and then send pulses out that you can observe with an oscilloscope. If you do not have one, change the timing of the pulses and connect your led. The number of pulses is the pin number +1. I have a longer pulse distance every 5 pins (makes counting easier).

The pattern for pin 14 for instance is 1,5,5,4

void setup()
{
  // put your setup code here, to run once:

}

void loop()
{
  // put your main code here, to run repeatedly:
  static int pin = 0;

  pinMode( pin, OUTPUT );

  for( int i = 0; i < pin + 1; i++ )
  {
    digitalWrite( pin, HIGH );
    delay( 10 );
    digitalWrite( pin, LOW );
    if( ( i % 5 ) == 0 )
    {
      delay( 50 );
    }
    else
    {
      delay( 20 );
    }
  }
  pinMode( pin, INPUT );

  pin++;
  if( pin > 25 )
  {
    pin = 0;
  }
}

Biomechanist:
Maybe I misunderstand how to call the pin I want to turn on?

Just use the pin numbers marked on the bottom of the board. You can use the An pin names for digitalWrite() as well, so don't bother with trying to convert them to integers.

Klaus_K:
The pin numbering documentation for the Arduino is not as clear as the pin diagram printed on the paper card you get with the Teensy.

I'm interested to know what about it you find unclear?

pert:
I’m interested to know what about it you find unclear?

The Teensy boards come with a card (15x11cm / 6x4 inch) that looks like the images on the following web page

https://www.pjrc.com/teensy/pinout.html

As you can see the digital pins numbers are given for all pins including the analog ones.

Why do you need digital pin numbers? You can just use the An pin names.

While we're on the subject, I'll give my own criticisms of Arduino's pinout diagram:

My biggest complaint is that they don't provide the pin numbers of the RX and TX pins. Unlike the classic Nano, those pins are not the Serial interface, so there is no problem with the user using them as they like, so knowing the pin numbers is very useful. Worse, at least on my board, those pins are incorrectly marked "TX1" and "RX0", when actually TX is pin 0 and RX is pin 1 (breaking compatibility with the original Nano pinout).

My other complaint is the use of the Dn pin notation. Unlike the An pin names, you can't use the Dn pin names in your code so this is misleading.

For me it’s just about helping others with their code. If they use the number, I would like to simply look up which pin on the board it is.

I agree with your other observations.

It would also be nice if they included the other on board pins for LEDs, WiFi/BLE module reset, … in the pin diagram.

Thanks to you both.

I don't think there was any confusion about pin numbers, I just reference an image of the bottom of the board. However, I do agree on both points that the Teensy has a nicer pinout diagram with the physical pin numbers accompanying it, and that it would be nice to have constants for the Dn pins. Now that I have looked at pins_arduino.h, I see that as a place one could add those constants if they wanted to, but I assume any official update to that file would wipe user customization.

I did screen capture this from somewhere on the Arduino site and use it as a reference:
Imgur

Anyway, as you can see from my last post, I was keeping it very simple and just calling the pin number directly in my digitalWrite statement. I have a bad feeling that the board is toast. I can't get voltage on any digital pins on the BLE Sense (I'm testing using an LED and a voltmeter, I don't own a scope).

Its possible to damage the I/O pins. Your resistor for the LED is on the small side as well. If you read the NINA-B3 datasheet you can see the I/O have two different drive strength and they are not as strong as some of the older Arduino pins. I would use larger resisitors and some small low power LEDs for these boards.

Did you try to run my test program? You should at least see the 5 on board LEDs blink. Than you can still use the board for Bluetooth experiments and use a cheaper more robust Arduino for LED blink experiments. :slight_smile:

Klaus_K:
Did you try to run my test program? You should at least see the 5 on board LEDs blink.

I tried it and the 5 on-board LEDs do blink. I also picked up a brand new Sense BLE and tried your program there and the result with the new board identical to the old board that I thought may be bad (which now I believe is probably ok). The on-board LEDs blink, but I cannot get the digital pins to output voltage! I'm measuring right at the pin with a multimeter.

I'm powering via USB cable. I've got the Arduino mounted on a breadboard. Measuring at the pins (nothing external connected). On pin 2 I have ~ 3.30v. On pin 15 I have ~ 4.70v.

The following simple program seemingly has no effect. I must be missing something obvious...

void setup() {
  pinMode(20, OUTPUT);
  pinMode(21, OUTPUT);
  delay(1000);
}

void loop() {
  digitalWrite(20, HIGH);
  digitalWrite(21, HIGH);
}

I did notice my library just states that it's for the "Nano 33 BLE" (not specifically the Sense), but this appears to be the library that Arduino is providing us for this board?

Biomechanist:
On pin 15 I have ~ 4.70v.

What do you mean by "pin 15"? What is the the label marked on the silkscreen on the bottom of the board next to the pin?

Biomechanist:
The following simple program seemingly has no effect.

What is the the label marked on the silkscreen on the bottom of the board next to the pins you are probing while running that sketch?

Please, let's use the standard Arduino pin names for the rest of this discussion to avoid confusion.

Biomechanist:
I did notice my library just states that it's for the "Nano 33 BLE" (not specifically the Sense), but this appears to be the library that Arduino is providing us for this board?

Correct. The only difference between the Nano 33 BLE and the Nano 33 BLE Sense is the Sense has a bunch of sensors added. These make no difference as far as the board selection goes, so you can use the Tools > Board > Arduino Nano 33 BLE selection for either board.

Biomechanist:
The following simple program seemingly has no effect.

What exactly do you mean by "has no effect"?

If I connect my multimeter between the GND and A6 pins by running that sketch, I get 2 V. If I connect my oscilloscope, I see a square wave. I would expect a solid 3.3 V, which is what happens if I run your sketch on my Mega 2560. It seems there is something strange about digitalWrite() on the Nano 33 BLE that causes the pin to go LOW, then HIGH every time it's called.

What is the the label marked on the silkscreen on the bottom of the board next to the pin?

Pin 15 = "VIN"
Pin 2 = "3V"

What exactly do you mean by "has no effect"?

As I have stated a few times now, I am unable to read any voltage on the digital pins (Dn silkscreen printed on the board), measuring at the pins with a multimeter, with nothing else connected. Either of the previous two sketches I posted show exactly what I am doing with the code.

I don't have a scope.

When I run this on a Teensy board, it works as I would expect.

I am new to working with Arduino. I am not sure what official terminology is or not. I found this pinout on the Arduino site and have been referencing it for pin numbers since, as was already discussed there are no constant values defined for the digital pins. (Imgur: The magic of the Internet)

It seems there is something strange about digitalWrite() on the Nano 33 BLE that causes the pin to go LOW, then HIGH every time it's called.

How is it that I am the first person to report this? I don't believe analogWrite is behaving correctly on these pins either. It seems like a major design flaw to have something as simple as setting a pin high or low not behave as expected (!) This is also very off-putting for someone such as myself who is new to working with Arduino hardware. Is this typical of their products?

I don't necessarily need the digital outputs for the project (IMU) I purchased this board for, however, that's entirely beside the point. To have a dozen useless pins on a board that's approaching ~ $40 seems totally unreasonable.

Well, thank you for the help thus far and Happy New Year to all, and I really hope this is resolved by the time early 2020 rolls around!

Biomechanist:
Pin 15 = "VIN"
Pin 2 = "3V"

That's much more clear now. When we talk about pin numbers here, we mean the Arduino pin numbers that you use in your sketch. Arduino pins 15 and pin 2 are very different things from the VIN and 3V pins.

Biomechanist:
Either of the previous two sketches I posted show exactly what I am doing with the code.

I agree. You've done a good job at providing minimal demonstration sketches. That is very helpful. What was not clear to me is what your exact measurement procedure is. That's why I asked for clarification.

Biomechanist:
I don't have a scope.

Understandable. I only recently got one and it's the cheapest of the cheap. There are other tools that are a higher priority to acquire than an oscilloscope and I know that many of us only have a limited budget to spend on our hobby. In a case like this where you have a pin outputting a frequency, it's difficult to understand what is happening without an oscilloscope. Your multimeter can't measure these fast voltage changes. You should see some voltage on the pin (e.g., the 2 V I measured), but that doesn't tell you what is really happening. In this case, a logic analyzer would also be usable. You can get the cheap Chinese clones of the Saleae 8 for around $7 from the usual places and I think that's a pretty useful tool to have, even after you get an oscilloscope.

Biomechanist:
I am new to working with Arduino. I am not sure what official terminology is or not.

I understand. It's a common problem we deal with here. Now you know, so you can communicate more effectively here in the future.

Biomechanist:
I found this pinout on the Arduino site and have been referencing it for pin numbers

Yeah, I really don't like that table. I would have left off the first column, as it doesn't give any useful information and only leads to confusion. Nobody here uses the physical pin numbers.

Biomechanist:
as was already discussed there are no constant values defined for the digital pins.

Yeah, but that's not a problem. If you want to reference digital pin 1, you use the number 1, and so on. When it comes to the analog pins, use the A0, etc, pin names, even when you are using them as digital pins.

Biomechanist:
How is it that I am the first person to report this?

Well, for one thing, the Nano 33 BLE boards are Arduino's newest product so, compared to the other boards, not as many people are using them and they haven't been using them very long. But the more significant reason is that it's unusual to write code that writes the pin HIGH over and over again. Once the pin is HIGH, there is no need to waste clock cycles setting it HIGH again. More typically, we are setting the pin HIGH, then LOW, then HIGH, etc. In that case you wouldn't notice a problem because you are trying for a square wave anyway.

Biomechanist:
I don't believe analogWrite is behaving correctly on these pins either.

Are you referring to the problem you reported in your first post? I can reproduce that issue. I don't know the cause though. I can add that the debug output the Nano 33 BLE prints on the TX pin at 115200 after you upload the sketch is:

++ MbedOS Error Info ++
Error Status: 0x80FF0144 Code: 324 Module: 255
Error Message: Assertion failed: result == NRFX_SUCCESS
Location: 0x1593D
File: ./mbed-os/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/pwmout_api.c+109
Error Value: 0x0
Current Thread: main  Id: 0x20001834 Entry: 0x1687D StackSize: 0x8000 StackMem: 0x200025E0 SP: 0x2000A524 
For more info, visit: https://mbed.com/s/error?error=0x80FF0144&tgt=ARDUINO_NANO33BLE
-- MbedOS Error Info --

Although Arduino has been able to hide it from the user to some extent, the Nano 33 BLE boards use an advanced RTOS (Mbed OS) that does impose some additional constraints on what you can do. There is some conflict between that analogWrite() code you wrote and Mbed OS.

Biomechanist:
It seems like a major design flaw to have something as simple as setting a pin high or low not behave as expected (!)

I agree it's unfortunate. The Nano 33 BLE boards use a very different microcontroller and software framework from any of the other official Arduino boards. This means a lot of work to get everything working just right. I'm sure we will see steady improvement as time goes on.

Biomechanist:
This is also very off-putting for someone such as myself who is new to working with Arduino hardware. Is this typical of their products?

It's typical that the support software for the newer hardware will be more likely to have bugs. Over time, the bugs are reported and then fixed. You are much less likely to encounter bugs when using the classic AVR boards (e.g., Nano, Uno, Mega, Leonardo). You will also find there is better community support for these boards (e.g., libraries, example sketches, tutorials) simply because they have had years longer to accumulate these resources. For this reason, I would recommend that beginners start with the AVR boards. If you really do need the greater resources and features of the new boards, then that's just how it is, but for the simple learning projects, and even some quite demanding advanced projects, the AVR boards are plenty capable. I think of the newer boards as more appropriate for people who are adventurous and interested in the opportunity to participate in the testing and development process.

Biomechanist:
To have a dozen useless pins on a board that's approaching ~ $40 seems totally unreasonable.

I'm not yet convinced that the pins are useless. I'll wait to hear back from you regarding the pin numbers before I can say more. So far, from my own tests, all I've seen is:

  • The issue with your analogWrite sketch crashing, which I suspect is more a matter of needing to learn how to work with Mbed OS.
  • The issue of a square wave being generated when you set the pin HIGH over and over again, which can easily be avoided by simply writing your code so that doesn't happen.

Biomechanist:
Well, thank you for the help thus far and Happy New Year to all, and I really hope this is resolved by the time early 2020 rolls around!

Happy New Year to you as well! We're here to help you out with this and I think we can make some progress if we stay positive and keep working at it.

pert:
If I connect my multimeter between the GND and A6 pins by running that sketch, I get 2 V. If I connect my oscilloscope, I see a square wave. I would expect a solid 3.3 V, which is what happens if I run your sketch on my Mega 2560. It seems there is something strange about digitalWrite() on the Nano 33 BLE that causes the pin to go LOW, then HIGH every time it's called.

I see the same issue. It looks like the pin is initialized with LOW and then HIGH is written to it every time you set the pin to high. There is about 10us low before the level gets back to HIGH. I suspect there is a compatibility issue with mbed and the Arduino abstraction layer. The issue would likely go away when you use mbed functions for the I/O.

Thanks again to you both. I have actually gained quite a bit more understanding out of this conversation. I've realized that mistakes from lack of familiarity are/were a significant part of the problem, but this thread has also raised some new questions for me :slight_smile:

If you want to reference digital pin 1, you use the number 1, and so on. When it comes to the analog pins, use the A0, etc, pin names, even when you are using them as digital pins.

THIS was a major source of confusion for me.

Adding to my confusion from above, on the Teensy board (the only other board I own) I just reference the physical pin numbers in my code to turn them on/off or set some value via PWM. So, this is all clear now. I really wish Arduino would have picked a more intuitive standard here... why they decided to use An for analog pins and then use only n for digital pins is an odd choice.

So, using digital pins, the following codes runs as expected without causing any lockup of the clock...

int pinR = 2;
int pinG = 3;
int pinB = 4;

void setup() {
  pinMode(pinR, OUTPUT);
  pinMode(pinG, OUTPUT);
  pinMode(pinB, OUTPUT);
  delay(1000);
}

void loop() {
  analogWrite(pinR, random(240)+15);
  analogWrite(pinG, random(240)+15);
  analogWrite(pinB, random(240)+15);
  delay(500);
}

I wanted to test reading and writing the digital pin, because this seemed to be problematic. This code illustrates...

int pinR = 2;
bool didWrite = false;
bool didRead = false;

void setup() {
  pinMode(pinR, OUTPUT);
  delay(1000);
}

void loop() {
  if (!didWrite){
    digitalWrite(pinR, HIGH); 
    didWrite = true;
  }

  delay(3000);

  if (!didRead) {
    float foo = digitalRead(pinR);
    didRead = true;
  }
}

So, the pin does successfully go to a HIGH state, but calling digitalRead sets it to some intermediate state (~1.88v) where it remains thereafter (edit: apparently this is still read as HIGH. Minimum high voltage? ~.6*3.3v). I used the boolean flags to ensure that the digitalRead and digitalWrite procedures only got called once. Is this expected? I would not think so? I don't have a scope, as I mentioned, so beyond this I don't know what's actually happening, or why.

Biomechanist:
on the Teensy board (the only other board I own) I just reference the physical pin numbers in my code to turn them on/off or set some value via PWM.

Which Teensy? None of the Teensys I own have Arduino pin numbers that match the physical pin numbers.

Biomechanist:
So, the pin does successfully go to a HIGH state, but calling digitalRead sets it to some intermediate state (~1.88v) where it remains thereafter (edit: apparently this is still read as HIGH. Minimum high voltage? ~.6*3.3v). I used the boolean flags to ensure that the digitalRead and digitalWrite procedures only got called once. Is this expected? I would not think so? I don't have a scope, as I mentioned, so beyond this I don't know what's actually happening, or why.

There is no reasonable application where you would want to measure the voltage output from an input pin. As long as it's working correctly as an input, why worry about it? I think you're getting sidetracked by looking for problems. Now that you know how to reference the pins on your Nano 33 BLE board, why not start having some fun with it?