LED buzzing

Hi guys

For some reason my led's seem to be buzzing wen dimming them With PWM on my mega.

Is there any way of working around that?

1: Post your sketch (Use the #CODE TAGS!)

2: Post a schematic (a hand-drawn one is fine) (MAXIMUM 1000 pixels on the longest dimension!)

3: Post a picture of it (MAXIMUM 1000 pixels on the longest dimension!)

// Per.

Change the PWM frequency. Default is ~490 for some pins, and ~980 Hz for others.

Try driving the LED from tone library.

You do have a current limit resistor in use, yes?

In essence LEDs do not make a noise.
If your LED appears to be making a noise you are making some sort of mistake. What LEDs are you using (post a link to the data sheet ) and how are you powering them?

Hi guys

It seem the 980mhz works without buzzing. how do i change all the ports to 980mhz im currently using pin 12 and 13 on my mega 13 is at 980 and works well.

MouseMan: It seem the 980mhz works without buzzing. how do I change all the ports to 980mhz

It certainly would not buzz at 980 *milli*Hertz. It might "tick" rather like a clock. :astonished:

It seem the 980mhz works without buzzing.

And if you mean 980MHz then how on earth are you getting that fro a 16MHz processor?

Excuse me I ment 980hz. I need to change all the pwm pins to 980hz. Pin 13 and 4 currently 980hz I'd like all at that frequantcy

http://playground.arduino.cc/Code/PwmFrequency

Hi please have a look at may code below. does it look correct

I need to get pin 11 and 12 (490mh) to the same as 4 and 13 (980mh)

Code...

//*********** void setup **************************************************************

void setup() { Serial.begin(9600); setSyncProvider(RTC.get); // the function to get the time from the RTC // setTime(16,29,0,10,9,14); // set time to Saturday 8:29:00am Jan 1 2011

Wire.begin(); //initialize the I2C bus // lcd.init(); //initialize LCD lcd.begin(16, 2); // start the library lcd.setCursor(0,0); // set the LCD cursor position

pinMode(fan, OUTPUT); // digital pin for fan as output

TCCR0B = TCCR1B & 0b11111000 | 0x03; //Set PWM to 980 on pin 11 & 12 TCCR1B = TCCR0B & 0b11111000 | 0x03; //Set PWM to 980 on pin 11 & 12

}

I need to get pin 11 and 12 (490mh) to the same as 4 and 13 (980mh)

I don't think you can PWM pin 12. What Arduino are you using? If you start messing with timer 0 things like delay and the millis timer no longer work correctly you know.

I'm using an arduino mega it has 1 to 13pwm ports

MouseMan:
I’m using an arduino mega it has 1 to 13pwm ports

No it does not, it has

PWM: 2 to 13 and 44 to 46. Provide 8-bit PWM output with the analogWrite() function.

See this thread:-
http://forum.arduino.cc/index.php/topic,72092.0.html

I feel like in idiot. How do I access the registry to make the changes

MouseMan: I feel like in idiot. How do I access the registry to make the changes

What do you mean registry? You put those instructions in your code.

Please help me. im loosing my mind with this thing…
i need all the pwm to be at 980hz

full code below…

#include <OneWire.h>
#include <LCDI2C.h>
#include <WProgram.h>
#include <Wire.h>
#include <DS1307RTC.h>
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeAlarms.h>
#include

//*********** Select the pins used on the LCD panel ******************************************************************************************
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//LCDI2C lcd = LCDI2C(4,20,0x4C,1); //[# of lines on LCD],[address of serial LCD controller on I2C]
int lcd_key = 0;
int adc_key_in = 0;

#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5

int read_LCD_buttons(){ // read the buttons
adc_key_in = analogRead(0); // read the value from the sensor

// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
// We make this the 1st option for speed reasons since it will be the most likely result

if (adc_key_in > 1000) return btnNONE;

// For V1.1 us this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;

// For V1.0 comment the other threshold and use the one below:
/*
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
*/

return btnNONE; // when all others fail, return this.
}
OneWire ds(2); // Temp Sensor on pin 2
int fan = 20;
int fan_on_temp = 2600; //Turn on fan at this temp 78.800f = 26.00c
int fan_off_temp = 2550; //turn fan off once below this temp

#define SensorPin A1 //pH meter Analog output to Arduino Analog Input 0
unsigned long int avgValue; //Store the average value of the sensor feedback
float b;
int buf[10],temp;

//*********** void setup ******************************************************************************************

void setup()
{
Serial.begin(9600);
setSyncProvider(RTC.get); // the function to get the time from the RTC
// setTime(16,29,0,10,9,14); // set time to Saturday 8:29:00am Jan 1 2011

Wire.begin(); //initialize the I2C bus
// lcd.init(); //initialize LCD
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0); // set the LCD cursor position

pinMode(fan, OUTPUT); // digital pin for fan as output

TCCR0B = TCCR0B & 0b11111000 | 0x03; //Set PWM to 980 on pin 11 & 12
TCCR1B = TCCR1B & 0b11111000 | 0x03; //Set PWM to 980 on pin 11 & 12
TCCR3B = TCCR3B & 0b11111000 | 0x03;
TCCR4B = TCCR4B & 0b11111000 | 0x03;

//*********** Alarm Time ******************************************************************************************
// create the alarms
/*

Alarm.alarmRepeat(17,32,0, Morning1Alarm); // 8,00am every day 50
Alarm.alarmRepeat(17,34,0, Morning2Alarm); // 9:00am every day 100
Alarm.alarmRepeat(17,36,0, Morning3Alarm); // 10:00am every day 153
Alarm.alarmRepeat(17,38,0, Morning4Alarm); // 11:00am every day 204
Alarm.alarmRepeat(17,40,0, Morning5Alarm); // 12:00pm every day 255
Alarm.alarmRepeat(17,42,0, Morning6Alarm); // 2:00pm every day 204

Alarm.timerRepeat(15, Repeats); // timer for every 15 seconds
Alarm.timerOnce(10, OnceOnly); // called once after 10 seconds
*/
}

//*********** void loop ******************************************************************************************

void loop(){
//*********** PH ***********************************************************************************************
for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
{
buf*=analogRead(SensorPin);*

  • delay(10);*
  • }*
  • for(int i=0;i<9;i++) //sort the analog from small to large*
  • {*
  • for(int j=i+1;j<10;j++)*
  • {*
    _ if(buf*>buf[j])_
    _
    {_
    _ temp=buf;
    buf=buf[j];
    buf[j]=temp;
    }
    }
    }
    avgValue=0;
    for(int i=2;i<8;i++) //take the average value of 6 center sample*

    avgValue+=buf*;
    float phValue=(float)avgValue5.0/1024/6; //convert the analog into millivolt_

    _ phValue=3.5phValue; //convert the millivolt into pH value_
    _ Serial.print(“pH:”);
    Serial.print(phValue-0.48,2); // -0.40 is to ballance the PH to Setup Liquid*

    * Serial.println(" ");
    delay(800);*_

//*********** PH ***********************************************************************************************
* lcd.setCursor(9,0);*
* lcd.print(“pH:”); *
* lcd.print(phValue-.09,2); // -1.36 is to ballance the PH to Setup Liquid*
//*********** ***********************************************************************************************
* digitalClockDisplay();*

// Alarm.delay(1000); // wait one second between clock display
* byte i;*
* byte present = 0;*
* byte data[12];*
* byte addr[8];*
* long ph_val;
int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract, minute, hour, second, date, month, year, mil_time, ph_read;
_ char buf[12]; //used to convert int to string for displaying on LCD*_

mil_time = (hour * 100) + minute; //create military time output [0000,2400)

//*********** Get temp data from DS18B20 ******************************************************************************************
* if ( !ds.search(addr)) {*
* ds.reset_search();
_ return;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end*

* present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad*

* for ( i = 0; i < 9; i++) { // we need 9 bytes*
data = ds.read();
* }
LowByte = data[0];
HighByte = data[1];
TReading = (HighByte << 8) + LowByte;
SignBit = TReading & 0x8000; // test most sig bit*

* if (SignBit) // negative*
* {
TReading = (TReading ^ 0xffff) + 1; // 2’s comp*

* }*_

Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
// Tc_100 = (Tc_100 * 9/5) + 3200; //Convert to fahrenheit, comment this out to display in celcius

//*********** Display current temperature ******************************************************************************************
* lcd.setCursor(0,0);*

* Whole = (Tc_100 / 100); // separate off the whole and fractional portions*
* Fract = (Tc_100 % 100);
_ lcd.print(Whole, DEC);
lcd.print(".");
if (Fract < 10)
{
lcd.print(“0”);
}
lcd.print(Fract, DEC);
lcd.write(0xDF);
lcd.print("C ");*_

//*********** Serial Print current temperature ******************************************************************************************
* lcd.setCursor(0,0);*

* Whole = (Tc_100 / 100); // separate off the whole and fractional portions*
* Fract = (Tc_100 % 100);
_ Serial.print(Whole, DEC);
Serial.print(".");
if (Fract < 10)
{
Serial.print(“0”);
}
Serial.print(Fract, DEC);
Serial.write(0xDF);
Serial.print("C ");
Serial.println();
delay(1000);*_

//*********** Fan ******************************************************************************************
* if(Tc_100 > fan_on_temp){ // turn fan on if temp is above fan_on_temp*
* digitalWrite(fan, HIGH);*
* }*
* if(Tc_100 < fan_off_temp){ //turn fan off if temp is below fan_off_temp*
* digitalWrite(fan, LOW);*
* }*
}
//*********** Alarm Time ******************************************************************************************
/*
// functions to be called when an alarm triggers:
void Morning1Alarm(){
* analogWrite(11 , 51); *
}
void Morning2Alarm(){
* analogWrite(11 , 102); *
}
void Morning3Alarm(){
* analogWrite(11 , 153); *
}
void Morning4Alarm(){
* analogWrite(11 , 204); *
}
void Morning5Alarm(){
* analogWrite(11 , 255); *
}
void Morning6Alarm(){
* analogWrite(11 , 204); *
}
*/
//void ExplicitAlarm(){
*// Serial.println(“Alarm: - this triggers only at the given date and time”); *
//}
// void Repeats(){
*// Serial.println(“15 second timer”); *
//}
//void OnceOnly(){
*// Serial.println(“This timer only triggers once”); *
//}
void digitalClockDisplay()
{
* // digital clock display of the time*
* Serial.print(hour());*
* printDigits(minute());*
* printDigits(second());*
Serial.println();
}
void printDigits(int digits)
{
* Serial.print(":");*
* if(digits < 10)*
* Serial.print(‘0’);*
* Serial.print(digits);*

//*********** Display Time ******************************************************************************************
* lcd.setCursor(0,10);*
* lcd.setCursor(0,10);*
* lcd.print(hour());*
* lcd.print(":");*
* lcd.print(minute());*
* lcd.print(":");*
* lcd.print(second());*

_//*********** Moon Lights ****************************************************************************************** _

/*
* if(hour() < 7) analogWrite(3, 0);*
* if(hour() == 7) analogWrite(3, 127);*
* if(hour() == 8) analogWrite(3, 0);*
* if(hour() == 17) analogWrite(3, 127);*
* if(hour() == 19) analogWrite(3, 65);*
* if(hour() >= 20) analogWrite(3, 0);*
*/
* if(hour() < 7) analogWrite(13, 0);*
* if(hour() == 7) analogWrite(13, 66);*
* if(hour() == 8) analogWrite(13, 66);*
* if(hour() == 9) analogWrite(13, 127);*
* if(hour() == 10) analogWrite(13, 255);*
* if(hour() == 11) analogWrite(13, 255);*
* if(hour() == 12) analogWrite(13, 255);*
* if(hour() == 13) analogWrite(13, 255);*
* if(hour() == 14) analogWrite(13, 255);*
* if(hour() == 15) analogWrite(13, 255);*
* if(hour() == 16) analogWrite(13, 255);*
* if(hour() == 17) analogWrite(13, 255);*
* if(hour() == 18) analogWrite(13, 255);*
* if(hour() == 19) analogWrite(13, 127);*
* if(hour() == 20) analogWrite(13, 66);*
* if(hour() == 21) analogWrite(13, 66);*
* if(hour() >= 22) analogWrite(13, 0);*
//*********** Day Lights ******************************************************************************************
* if(hour() < 8) analogWrite(12, 0);*
* if(hour() == 8) analogWrite(12, 84);*
* if(hour() == 9) analogWrite(12, 168);*
* if(hour() == 10) analogWrite(12, 255);*
* if(hour() == 11) analogWrite(12, 255);*
* if(hour() == 12) analogWrite(12, 255);*
* if(hour() == 13) analogWrite(12, 255);*
* if(hour() == 14) analogWrite(12, 255);*
* if(hour() == 15) analogWrite(12, 191);*
* if(hour() == 16) analogWrite(12, 127);*
* if(hour() == 17) analogWrite(12, 63);*
* if(hour() >= 18) analogWrite(12, 0);*

}

You still haven’t read the “sticky” about how to post code? >:(