PWM noise with LED

255 views
Skip to first unread message

Ed Devane

unread,
Sep 5, 2018, 12:44:02 PM9/5/18
to Mozzi-users
Hi,

I'm making an instrument based around Mozzi that has an LED and a speaker. I've got the Arduino Nano 328 /5v running in HiFi mode which works nicely with the test sine wave code. However when I introduce the LED PWM using the Sinewave_PWM_ledsHiFi sketch the LFO that modulates the led brightness is audible via the speaker. It's as loud as the (desired) audio in the low mid audio range. 

Is there anything I can do to cure this?

Thanks
DSC_0714.JPG

Tim Barrass

unread,
Sep 5, 2018, 9:23:33 PM9/5/18
to 'Ian C.' via Mozzi-users
Hi Ed,
looks like an interesting instrument!

I've just tested the Sinewave_PWM_ledsHiFi sketch here and wasn't able to get the noise you mentioned.  I tested using the onboard led at pin 13, which required changing some of the port code in updateRGB(), to this (just following the example http://playground.arduino.cc/Main/PWMallPins):

  (red_count++ >= r) ? PORTB &= ~(1 << RED_PIN-8) : PORTB |= (1 << RED_PIN-8);
  (green_count++ >= g) ? PORTB &= ~(1 << GREEN_PIN-8) : PORTB |= (1 << GREEN_PIN-8);
  (blue_count++ >= b) ? PORTB &= ~(1 << BLUE_PIN-8) : PORTB |= (1 << BLUE_PIN-8);

I also added this line to updateControl():
  aSin.setFreq(40+red_brightness);

to hear the sound at a range of frequencies.  It all seemed to be OK...

Just to be sure, have you changed mozzi_config.h to use HIFI instead of STANDARD_PLUS mode?  (Or you can keep STANDARD_PLUS and remove <<6 in updateAudio();)

Or maybe there's a long lead to the LED?  Could try a capacitor near the Arduino to reduce noise, if there was any?

Mmm not sure what else to suggest..

Tim

--
You received this message because you are subscribed to the Google Groups "Mozzi-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mozzi-users...@googlegroups.com.
To post to this group, send email to mozzi...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/mozzi-users/afc8cfaa-13cf-4563-b6eb-af95691ad6b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<DSC_0714.JPG>

Ed Devane

unread,
Sep 6, 2018, 2:47:01 AM9/6/18
to Mozzi-users
Thanks I'll give that a go. Good to know that it works for you, and that it could be an issue with my wiring...I used pin 5, could that be the problem? Easy to swap around anyway.

If you want one of these to play around with I'll gladly send you one. Some assembly required but that's the point 😀

Ed Devane

unread,
Sep 6, 2018, 2:58:14 AM9/6/18
to Mozzi-users
I'm using a tip102 transistor to power a 3w led in this version. Next iteration will be on a custom pcb(rather than several separate strip board circuits) with a mosfet and 1w led. Just wondering if the amount of current being chopped via the transistor is the cause of the noise...

Tim Barrass

unread,
Sep 6, 2018, 7:02:40 AM9/6/18
to 'Ian C.' via Mozzi-users
Well, it could be the current - I'd try putting a big capacitor (100uf or more) on the power supply rails close to the arduino. Do you get the same noise if the amp runs without audio input from the arduino connected? Sometimes a decoupling capacitor (about .22uf) between the arduino audio out and your amp input can also help.

>I used pin 5, could that be the problem?
I wouldn't expect so, if the led looks like it's working OK.

>If you want one of these to play around with
Thanks, I'd like to see and hear a video of it being played... don't find much time for more things to make and do around here!

Cheers
TIm

> On 6 Sep 2018, at 4:58 pm, Ed Devane <edjd...@gmail.com> wrote:
>
> I'm using a tip102 transistor to power a 3w led in this version. Next iteration will be on a custom pcb(rather than several separate strip board circuits) with a mosfet and 1w led. Just wondering if the amount of current being chopped via the transistor is the cause of the noise...
>
> --
> You received this message because you are subscribed to the Google Groups "Mozzi-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to mozzi-users...@googlegroups.com.
> To post to this group, send an email to mozzi...@googlegroups.com.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/mozzi-users/4b24bccc-48f2-45e1-bdcc-bb9a2be79c84%40googlegroups.com.

Ed Devane

unread,
Sep 6, 2018, 7:39:03 AM9/6/18
to Mozzi-users
I've tried changing to port B as you suggested but the noise persists. I don't think it's the transistor as I've tested this on an Uno with just a 5mm led powered off a pin with the same noisy result. I definitely have HIFI mode selected in the mozzi_config file. 

I've tried the capacitors as you suggested but no luck. Is it possible to increase the PWM frequency out of audio range?

Tim Barrass

unread,
Sep 6, 2018, 8:54:10 AM9/6/18
to 'Ian C.' via Mozzi-users
Do you want to post the code?

Ed Devane

unread,
Sep 6, 2018, 9:00:10 AM9/6/18
to Mozzi-users
#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/sin2048_int8.h> // sine table for oscillator
#define CONTROL_RATE 128
const byte LED_PIN = 5;
const char TRIGGER_PIN = 0; // set the input for the knob to analog pin 0
const char POT_PIN = 1; // set the input for the knob to analog pin 0
byte led_brightness;
byte volume;

// control oscillators using sinewaves to modulate LED brightness
Oscil <SIN2048_NUM_CELLS, CONTROL_RATE> kLed(SIN2048_DATA);

// audio oscillator
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);


void updateLED(byte b) {
  // stagger pwm counter starts to try to reduce combined flicker
  static byte LED_count = 0;
  (LED_count++ >= b) ? PORTD &= ~(1 << LED_PIN) : PORTD |= (1 << LED_PIN);
}


void setup() {
  pinMode(LED_PIN, OUTPUT);
  // set led brightness modulation frequencies


  // set audio oscil frequency
  //Serial.begin(115200);

  startMozzi(); // uses the default control rate of 64, defined in mozzi_config.h
}


void updateControl() {
  led_brightness = 128 + kLed.next();
  int potRead = mozziAnalogRead(TRIGGER_PIN); // value is 0-1023
  int potRead2 =  mozziAnalogRead(POT_PIN);
  int mod = map(potRead2, 10, 1023, 0.5, 20);
  aSin.setFreq(potRead2);
  kLed.setFreq(mod);
  //  Serial.println(potRead);
  // map it to an 8 bit range for efficient calculations in updateAudio
  volume = map(potRead, 1023, 952, 0, 255);
}


int updateAudio() {
 updateLED(led_brightness);
  // this would make more sense with a higher resolution signal
  // but still benefits from using HIFI to avoid the 16kHz pwm noise
  return ((int)aSin.next() * volume) >> 2;
  return ((int)kLed.next() * volume) >> 2;

}


void loop() {
  audioHook(); // required here

Tim Barrass

unread,
Sep 6, 2018, 9:07:06 AM9/6/18
to 'Ian C.' via Mozzi-users
>the LFO that modulates the led brightness is audible
what does it sound like?  (a description would help but a recording is even better)

Ed Devane

unread,
Sep 6, 2018, 9:14:32 AM9/6/18
to Mozzi-users

Ed Devane

unread,
Sep 6, 2018, 9:16:39 AM9/6/18
to Mozzi-users
you can hear the PWM noise from the LED almost as loudly as the desired sine wave. The sine wave in turn gets modulated by the PWM. It actually sounds quite nice in its own way but it would be nicer if it was optional...

Ed Devane

unread,
Sep 6, 2018, 9:23:11 AM9/6/18
to Mozzi-users
If I use digitalWrite to turn on the LED there's no noise. I might be able to do something with chopping the on/off time instead of using PWM to get some use out of it

Tim Barrass

unread,
Sep 6, 2018, 9:23:38 AM9/6/18
to 'Ian C.' via Mozzi-users
does it happen with a different amp or other listening method?

Here I tried the PWM led example with a mozzibyte board today, and previously just with a direct out to the computer with no other components... it sounded clean to me - but I might be not hearing it the same way as you, maybe?

Tim Barrass

unread,
Sep 6, 2018, 9:32:12 AM9/6/18
to 'Ian C.' via Mozzi-users
ah OK thanks, I just got the video.  Mmm not at all how the basic example usually sounds!  Tomorrow I'll have a go with your sketch, though I don't see anything I'd expect to cause that sound...

Ed Devane

unread,
Sep 6, 2018, 9:34:12 AM9/6/18
to Mozzi-users
I've got the output of the HIFI circuit feeding a PAM8304 amplifier. I just tried plugging it into a small guitar amp and same noise.

Ed Devane

unread,
Sep 6, 2018, 10:41:39 AM9/6/18
to Mozzi-users
thanks for your help!

Tim Barrass

unread,
Sep 7, 2018, 8:45:47 AM9/7/18
to 'Ian C.' via Mozzi-users
I tried your sketch here - it plays OK, though for my set up I had to change this line:

volume = map(potRead, 1023, 952, 0, 255);
to:
  volume = map(potRead, 1023, 0, 0, 255);

It's a bit rough due to the frequency and volume changing at control rate, but doesn't sound like the vid you linked to!

To test further, I tried using Mozzi's IntMap as a faster replacement for map(), and Smooth for the frequency and volume transitions.

The smoothed sketch is attached, with before and after recordings.

pwmled.zip

Ed Devane

unread,
Sep 10, 2018, 8:22:23 AM9/10/18
to Mozzi-users
Thanks Tim, 

I'll try that. 

I actually think the problem is something to do with the 9v battery not being able to provide enough current. I put in a fresh battery and while the unwanted noise was definitely still there, it was far quieter.

All the best
Reply all
Reply to author
Forward
0 new messages