No output on PWM pins - UNO or Mini Pro (Atmega328)

Hey guys, so I’ve been working on a small project that is basically finished with the exception of me being unable to get PWM on several of the pins of an arduino mini pro/arduino uno (atmega328 chip). The idea behind the project is to read RPM from the can bus and apply a specific voltage to the pedal wires. It works great until I started adding stuff and running out of pins to use.

My understanding is that if you use millis() or related functions, or timer0 at all, you will not be able to get PWM on pins 5 and 6. Originally I had been using these pins for PEDAL_MAIN and PEDAL_ALT and neither worked, so I changed the pins to 3 and 9. Unfortunately, while pin 9 is now working fine, pin 3 still won’t work and I’m out of pins to use. I’m unaware of any reasons why PWM on pin 3 would not be working, the pin isn’t being used by anything else as far as I am aware.

We’ve tried changing all kinds of stuff and haven’t made any progress in the last few days, so hopefully someone here knows what’s going on.

TLDR: I have attached the code in case anyone can figure out what is going wrong here, currently I cannot use pins 3, 5, or 6 for PWM and I need one of them to work. If anyone knows what has gone wrong any information would be appreciated:

RPM (1).txt (7.7 KB)

From the reference page for analogWrite(), here - https://www.arduino.cc/en/Reference/AnalogWrite:

After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin).

(emphasis added)

From your function checkButtons():

void checkButtons()
{
  for (int i = 0; i <= 8; i++)
  {
    int currentstate = digitalRead(i);
    . . .

From your loop():

void loop() //test loop
{
  checkButtons(); //always executes
  . . .

So, it looks like loop() calls checkButtons() on every iteration, and checkButtons() calls digitalRead() multiple times, terminating any analog output that might have been initiated by analogWrite() on pins 0 through 8. That would account for the symptoms you report - no analog output on pins 3, 5 and 6.

I haven’t pored over the sketch to see why you’re reading from pins that you intend to use for analog output. If you need to do both with the same pins, you’ll want to craft your sketch with care, to make sure that all the pins are in the right states at the right times.

Edit - add this:

JPlanck:
My understanding is that if you use millis() or related functions, or timer0 at all, you will not be able to get PWM on pins 5 and 6.

I’m not aware of any such restriction. You can use analogWrite() on those pins, and it will work just fine. If you change the output frequency, it will affect timing functions. But, using timing functions won’t affect analog output on those pins.

JPlanck: My understanding is that if you use millis() or related functions, or timer0 at all, you will not be able to get PWM on pins 5 and 6.

No, where did you read that? Those pins are used for Timer 0, which runs all the time, but that isn't stopping it being connected for PWM output.

I didn't realize reading the pin would stop PWM, thanks that is very helpful.

The reads are not needed at all for those pins, it was just lazy/bad programming. I'll modify the code and it will work fine I think, thanks.

As far as timer 0 goes I had read somewhere that using the timers can interfere with pwm, but don't remember where I read that.