Fast PWM on Arduino Mega

Hi. I'm working on an Arduino Mega based simpit dashboard. I'm using actual vehicle gauges like this:


which on the inside, contains some kind of a motor with 3 terminals, +, - and S (I've been told that this is essentially a voltmeter with a different scale):

I've already managed to create a circuit with a transistor to control the values via PWM, however, the system buzzes at any values other than 0 or 255. I'm pretty sure this is because of the PWM pulse turning on and off rapidly, and I've already read some about fast PWM, but I do have some questions:

  • Megas have multiple PWM pins (2 to 13). Would it be an issue using fast PWM on multiple pins? this should control 6 to 8 gauges, so I would need to do it for that amount of pins. I'm not sure if making PWM fast produces some kind of burnout or stress on the mega that could be harmful.
  • I'm not sure if you can control the frequency you can set the fast PWM to, code is a bit confusing for me. If so, how do I know what frequency should I use?
  • If I decide not to switch to fast PWM and leave the buzzing as is (it's not too loud), is this harmful for the motor? I've read that it can be annoying but has no real impact on the motor.

Thank you

You can add a RC low pass filter or increase the PWM frequency (in low level code). That frequency is not related/restricted to FastPWM mode.

Perhaps the TimerOne library will be helpful.

Thank you. So my circuit looks like this:
image
Where S1 is pin 2 (PWM) in the Arduino Mega.

I'm assuming the RC filter would be adding a resistor and a capacitor, I'm guessing at the collector of the transistor, right? Would the below be the correct diagram? and how do I calculate the R value? and the capacitor?

1 Like

The time constant of a RC filter is R*C seconds. That's the time when a square wave input raises the output to about 2/3 of the input amplitude.

Placing the filter at the output end will result in low R and high C values. More practical is placing the filter before the transistor base, with handier component values but higher load on the transistor. In either case the transistor is working in the linear area and dissipating more heat than in switching (rectangular) mode.

Hi, @Assamita
Looking at your circuits, do you have a series resistor in the the base circuit of Q1 to limit base current?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

I do, a 1k resistor. I just forgot to put it in the diagram

M1 is a moving coil galvanommetr which responses to slowly varying DC excitation. Therefore, the PWM signal (with Mark/On and Spce/Off) must be somoothed using a LPF (low pass filter) before being asserted on the base of Q1 through a series resitor.

You may try using the following LPF with 1 kHz PWM signal.
lpf

Thank you. Though I'm not sure how can I implement that in my circuit. Can you help me out? where would the resistor and capacitor go in my diagram?

I'd split your 1k base resistor in two (2*470) and attach the capacitor between both to GND. You can start with a smaller cap and increase it until the effect is sufficient.

The splited resistor compensates the non-linear characteristic of the transistor BE diode.

I don't have that many components laying around. Do you think 2x510 resistors and a 10 microfarad cap will make it?
This would be the schematics, right?
image

Find out yourself, it doesn't bite :wink:

Ok, I did try it, and unfortunately, looks like the cap is cancelling the PWM effect, so my code, that takes the needle up to 50% for 2 seconds, then to 100% for two more seconds and then to 0% for two more seconds, is only doing the 0 and the 100%.

The meter may have a lower range than actually provided. Find out which duty cycle makes it show less than 100%. Then take half of that value for 50%.

You also can add a series resistor to the meter, of same or higher than the meter resistance.

No, that's not the case, because without the cap, the meter works as expected, but buzzes

Please, post the PWM codes.

It's just analogWrite(2, 0); analogWrite(2, 127); and analogWrite(2, 255);
with delays in between.

What does the meter show at 127?

1. Connect the wiper point of a 5k pot (Fig-1) with A0-pin of MEGA. The other 2 pins of the pot will be connected with 5V and GND.


Figure-1:

Now, upload the following sketch for 980 Hz PWM signal at DPin-4. Slowly turn the pot and see the response of the galvanometer.

void setup()
{
    Serial.begin(9600);
}

void loop()
{
      byte arg = map(analogRead(A0), 0, 1023, 0, 255);
      analogWrite(4, arg);
      delay(1000); //test interval
}

It's at half-full.

You mean pin4 goes to the base of the transistor, right?