What can cause PWM pins 9,10 not to work on an Arduino nano?

I'm using cheap nanos, but they seem consistently not to do PWM on pins 9 and 10. Pins 3 and 6 work fine.
The spec says:
PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.
I've tried this with several nanos, with the same result, one today being brand new.

Could there be something about the multiple-possible-uses of these pins that is blocking them?
analogWrite(p,0) sends them high, and analogWrite(p,254) sends the working pins low for 4us but sends the 9, 10 pins solid low. I'm trying to dim LEDs, and this puts them on full bright.

Any suggestions?

Are you using any libraries that take over timer 1? That's responsible for PWM on those pins, but some libraries use the timer for something else, which means it can't do PWM.

Otherwise, post the code.

Post your full code. Something might use that timer, e.g the servo library.
Leo..

The code was too long to paste in. Attaching it.

Password_and_unlock_Color_LEDs_PWMed.ino (8.94 KB)

#include <Servo.h>

Servo library

"On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins."
Leo..

Hi,
When you developed this code did you do it in stages?

Getting each feature coded and working.

So you had a collection of codes.

Then combine then one at a time, each time debug and get the combination working before adding the next?

I it sounds like you didn't because as you say;

I'm using cheap nanos, but they seem consistently not to do PWM on pins 9 and 10. Pins 3 and 6 work fine.

It sounds like you have not even tried to run a simple PWM sketch to see if it was the clones.

Developing in stages would have pointed the problem out long before you got to this stage of a large code that may have to have complex edits made.

Tom.... :slight_smile:

I did develop in stages, but after I had everything working I started making improvements on both the hardware and the software side.
For example, the RGB light for keyboard status was running using millis() just fine, but the light was too flickery due to the interrupt activity or something, so I remembered the PWM feature. I tried that on 3 pins that worked, but then I realized I could reallocate pins to make way for some future expansion. I wanted to keep pin 3 free so pins 2 and 3 could be used for a second serial port in software, and because the interrupt features might be useful. I switched the keyboard to use A0-A5 (something warned me that you can't use A6 or A7 as digital pins) and D4 to free up the PWM pins and use 4 D pins for servo driving. I suppose it would have been safer to redo the software from the beginning each time I changed the pin assignments, but I didn't expect this kind of problem, which anyway wouldn't have shown up until I added the servo library.

Yes, I agree that reading the documentation is the right thing to do, but there is so incredibly much documentation that it's hard to know what to read if you don't know what you don't know. So I turned to humans, who are good at that. I'd already spent two days trying to figure out what the cause of the problem was, because of many distractions like a short on one board that wiped its regulator out, then moved everything to a second nano that turned out to have a nonfunctional USB connection which I hadn't thought to test first, then moved everything to a third nano using millis() for the dimming but with a pinout that should have allowed PWM, then converted the code to actually use the PWM, then discovered that the pins I now use don't work. The presently uploaded version just loops forever so I could check the PWM pins with a scope to see if there were any clues. (Nope.)

I do appreciate the help from my fellow humans. Thank you!

One thing I still don't know, but should be able to look up--is it possible to switch the servo code to use a different timer to solve the pin 9 10 problem, and if so which timer should I use so as not to screw up the millis() or EPROM? If someone just happens to know.... I can figure it out eventually, but my grandson is getting impatient.

-is it possible to switch the servo code to use a different timer to solve the pin 9 10 problem,

Yes but you will loose some other PWM pins in the process.
Your solution might be to get an external chip to generate the servo signals. Adafruit has one.