0
\$\begingroup\$

I'm working on an effects pedal design that uses a microcontroller to control on/off state as well as 2 LEDs. The microcontroller code uses a PWM square wave to control the lights, and the PWM is about 4kHz. Unfortunately, that frequency is coming thru in my signal (COM_1 in the schematic). I have tried disconnecting the close_relay_signal and open_relay_signal wires, and I'm still measuring the 4kHz frequency, which leads me to believe I'm picking up the squeal thru ground -- am I understanding the situation correctly? Is there a way to provide isolation there without introducing different noise?

Original schematic: Schematic

Updated Schematic: Updated Schematic

relevant code:

void controlLights() {
  int pot_value, red_pwm_value, blue_pwm_value;
  
  pot_value = analogRead(pot_pin);
  // map 0 - 1023 to 10 - 255
  red_pwm_value = max(16, pot_value / 4) ;

  blue_pwm_value = max(16, (255 - (pot_value / 4)));

  analogWrite(red_led_pin, red_pwm_value);
  analogWrite(blue_led_pin, blue_pwm_value);
}
\$\endgroup\$
11
  • 4
    \$\begingroup\$ Your design does not have even a single bypass capacitor anywhere. Why? \$\endgroup\$
    – Justme
    Commented Oct 14, 2022 at 18:18
  • \$\begingroup\$ You should show the layout of the circuit board to get advice on how noise can be transferred from one wire to another \$\endgroup\$ Commented Oct 14, 2022 at 18:42
  • 3
    \$\begingroup\$ Any reason you have to use 4 kHz for the PWM? That sounds like the worst possible choice and if you're really just driving two LEDs, you shouldn't be too constrained in choosing the most suitable frequency. Given you're working with audio and want to keep it clean, it would sound logical to pick a PWM frequency safely out of the audio range. Either go to tens of kHz, or if these LEDs are just indicators and don't need any sort of low latency, use like 10 Hz with RC smoothing. \$\endgroup\$
    – TooTea
    Commented Oct 14, 2022 at 18:53
  • 3
    \$\begingroup\$ Take the GND return from both LED's and disconnect it from whereever you have it connected. Instead, connect it directly to the GND pin of your voltage regulator. You probably have return currents passing through your sensitive analog section. And add appropriate capacitors as mentioned previous. \$\endgroup\$
    – Kyle B
    Commented Oct 14, 2022 at 19:03
  • 1
    \$\begingroup\$ At the VCC pin of the ATTINY. The L4931 also specifies a minimum 2uF of output bypass capacitance. \$\endgroup\$
    – vir
    Commented Oct 14, 2022 at 19:27

1 Answer 1

4
\$\begingroup\$

The 4 kHz waveform, a square wave rich with harmonics, is a bad choice to be deliberately generating in an audio environment just to drive indicator LEDs.

You can generate a much higher PWM frequency, such as 25 kHz, in an effort to stay above the audible frequency range.

A much better solution is to not generate PWM at all and to make a simple DAC for each LED. You have 6 GPIO pins free so, along with the 2 GPIOs already driving LEDs, you could make a 3-bit or 4-bit DAC for each LED. These give 8 or 16 brightness levels respectively, which may be fine for your application.

It'll be a little less efficient than your PWM method. But if you select low current LEDs, you can drive them with, say, 3 mA max. rather than the 13-odd mA max. your current circuit uses.

(Obviously, the PWM drive means that your current circuit's average current is typically lower, unless they're always driven at 100% which would defeat the point of PWM.)

The below circuit just contains example resistor values for a 3-bit DAC, delivering something like 3.6 mA into an LED with 2.1 V drop and GPIO Vo of VDD-0.2 V. More relevant values depend on the parts used and you can select some for the higher current LEDs if you prefer to continue with them.

In this design of DAC, each GPIO must be driven HIGH for a bit at '1' or tri-state for a bit at '0'. Don't drive a GPIO LOW as that will drain current from the LED drive and waste it.

schematic

simulate this circuit – Schematic created using CircuitLab

As others have commented, good decoupling around the board is essential to eliminate the kinds of problems you're detecting. Use a mix of bulk value and small value capacitors to ensure that low frequencies and high frequencies are filtered off the supply. Example values might be 3-off 10 uF and 3-off 100 nF, distributed around the circuit close to the regulator and to the main loads.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.