Arduino Nano Pin 11 works only when pluged in USB, not VIN

I have a simple project where I have 6 leds dimming in and out at different intervals.
I wired everything to PWM pins (3,5,6,9,10,11). All works fine as long as it is pluged to the PC, but when I use VIN to power it up, pin 11 stops working.
VIN is receiving 5V.
Here is the wiring :

The code is simple too:

int pins[6] = {3,5,6,9,10,11};
volatile int pLvl[6];
volatile bool pGoUp[6];
void setup() {
  for (byte p = 0; p < 5; p = p + 1) {
    pinMode(pins[p], OUTPUT);
    pLvl[p] = random(255);
    pGoUp[p] = true;
  }
}

void loop()
{

  for (byte p = 0; p < 5; p = p + 1) {
    // max/min and direction
    if ( pLvl[p] >= 255) {
      pLvl[p]=255;
      pGoUp[p] = false;
    }
    if ( pLvl[p] <= 0) {
      pLvl[p]=0;
      pGoUp[p] = true;
    }
    analogWrite(pins[p], pLvl[p]);  // analogWrite values from 0 to 255

    // moves dim up or down
    if (pGoUp[p])     {
      pLvl[p] = pLvl[p] + 2;
    } else {
      pLvl[p] = pLvl[p] - 2;
    }

  }
  
  delay(30);
  
}

Is there anything specific on this pin that would cause this ?

VIN needs more than 5v to regulate too 5v..
Looks like it's 7-12vdc..
If you have a regulated 5vdc supply, use the 5v pin instead if VIN..

good luck.. ~q

I had tried 5V pin, but same thing.
I've tried VIN with 9V and 12V as suggested and still nothing.

should be < 6 else you miss the end pin 11..
did this twice..

~q

dummy me...
thanks for spoting that...
Weird though that pin 11 actual works when pluged in the pc... anyways, it works now.
Thanks for saving me the trouble. I was sure the problem was hardware I wasn't even looking at the code.

1 Like

There is a useful bit of code you can use to automatically determine the number of elements in the array in order to make sure you never encounter this type of bug again.

Instead of hardcoding the array size, use sizeof(pins) / sizeof(pins[0]):

  for (byte p = 0; p < (sizeof(pins) / sizeof(pins[0])); p = p + 1) {

sizeof(pins) returns the number of bytes in the array. In a case like this where the array type is larger than 1 byte, that is not the same as the number of elements in the array. For this reason, we must divide the number of bytes in the array by the number of bytes in each of the array elements. We get the number of bytes in the element using sizeof(pins[0]). You could pass any of the array elements to sizeof, to get this number, but we choose to use the first element since an array will generally always have at least one element.

Reference:

I saw on these fora this pattern

const int numberOf = sizeof pins / sizeof *pins;

which works well and can serve to motivate learning or reinforce knowing about arrays and their intimate relationship to pointers in C/C++.

I would give credit, and I looked just now, but cannot single out anyone out, it is one of three heavies, though. Probably. I liked it right away and use it.

Any step away from hand counting and just writing

const int numberOf = 42:

is well taken. I did for too long, and probably wasted time here or there because.

a7

I prefer

#define NUMELEMENTS (x) (sizeof(x) / sizeof(x[0]))

...
...

for (uint8_t cnt = 0; cnt < NUMELEMENTS(inputPins); cnt++)
{
...
...
}

//And in the same program

for (uint8_t cnt = 0; cnt < NUMELEMENTS(ledPins); cnt++)
{
...
...
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.