PWM pin 3 not pulsing

Hi guys/gals, I’m not getting a cyclic waveform from pin 3 of my Uno…

Here’s the relevant code sections…

#define PUMP_PWM 3 // Water Pump PWM Output
#define PUMP_SPEED 150 // Water Pump speed setting 0-255

… and in void(setup)…

pinMode(PUMP_PWM, OUTPUT);

analogWrite(PUMP_PWM, PUMP_SPEED); //

of course I should be getting just above 50% duty cycle with that speed setting, but I’ve got a constant 0v output on pin 3. This in turn, is driving my MOSFET on, giving me full speed.

The only other thing in the sketch that might affect PWM generation is a section of code that sets up TIMER2 for 8KHz interrupts.

// set up Timer 2 for 8KHz (125uS) interrupt
// code example from Amanda Ghassaei
// http://www.instructables.com/id/Arduino-Timer-Interrupts/?ALLSTEPS
// ------------------------------------------------------------------
cli(); // stop interrupts
TCCR2A = 0; // set entire TCCR2A register to 0
TCCR2B = 0; // same for TCCR2B
TCNT2 = 0; // initialize counter value to 0
OCR2A = 249; // Set compare match register for 16KHz. Increments calculated as follows:
// Arduino clk frequency 16MHz / (required frequency * Prescaler of 8) - 1, Result must be < 256.
// So for this instance we calculate 16,000,000 / (8,000 * 8) - 1 = 249
TCCR2A |= (1 << WGM21); // turn on CTC mode
TCCR2B |= (1 << CS21); // Set CS21 bit for 8 pre-scalar
TIMSK2 |= (1 << OCIE2A); // enable timer compare interrupt
sei(); // allow interrupts
// end code example from Amanda Ghassaei
// -------------------------------------------------------------

Anyone got any ideas why pin 3 is not PWM’ing ??

Timer2 drives the PWM on pin 3 and 11

if you need to use Timer2 then you should do your PWM on other pins

...R

Robin2:
Timer2 drives the PWM on pin 3 and 11

if you need to use Timer2 then you should do your PWM on other pins

…R

AAAARGHH !!

PCB re-design !!

Thanks for the info…

Hi, You have designed a board (PCB) before getting the prototype working? ? ?

Tom... :o :o :o

TomGeorge:
Hi,
You have designed a board (PCB) before getting the prototype working? ? ?

Tom… :o :o :o

Not quite - I am using a PCB design from a working project which had two digital outputs D2 and D3 driving air solenoids.

Now I want to pump water at variable rates with one of those outputs, so chose the D3 output because it can be PWM controlled.

It’s either a hack on the PCB, or I’m looking at SoftPWM methods. I’m finding that some are using TIMER2 as well.

Having said that, I already have that 8KHz interrupt, so may be able to PWM my D3 output within that interrupt routine.

Since it is a motor, and it’s only pumping water for a “water feature”, I probably don’t need super timing accuracy, in fact variation of the pump speed wouldn’t have any detrimental effect on the visual.

I got it working…

I added the following to the ISR…

ISR_COUNT++; // count the interrupt
digitalWrite(PUMP_PWM, (ISR_COUNT < PUMP_SPEED));

ISR_COUNT is declared as a byte, so is constrained to 0-255, which mirrors the PWM function.

There might be a better way to do the digitalWrite, so it only needs to write as the count passes thresholds, but that would mean extra code and slow down the ISR, but what I’ve got works a treat, so leave well alone !

EDIT : discovered it was inverted, so just changed the digitalWrite …

digitalWrite(PUMP_PWM, (ISR_COUNT > PUMP_SPEED));

Also remembered the PCB has flywheel diodes on the output (they were driving solenoids), removed that and the motor speed control now works great.

This is at 180/255 = 70%