Recently I would like to develop a PWM program to control a 5V DAC output with 0.1V resolution. Everything seems OK, except when I adjust the multi-tune potentiometer to change the PWM percentage to 255, the output PWM waveform is unstable. It keeps on changing from 0 to 1. Once pass through this value, I can easily to turn the PWM from 0 to 100% without any problem very stable.
I tried to use 10 bits and Timer 3 and 4, the results are same. Below is my program after changing from Timer 3 to 4. Hope you can indicate what is wrong of the setting. Thanks.
/* Using a potentiometer and PWM on an Arduino to fade an LED.
/* POT to LED test -> by Owen Mundy March 11, 2010
* from: http://itp.nyu.edu/physcomp/Labs/AnalogIn
—————————————————————*/
int potPin = A0; // Analog input pin that the potentiometer is attached to
int potValue = 0; // value read from the pot
unsigned dimValue = 0;
int led = 6; // PWM pin 6 for Timer4 of Mega OC4A.
void setup() {
// Setting the timer to generate 31KHz pulse with 9bit PWM control
TCCR4A = 0b10000010; // Timer/Counter Control Register set WGM11, WGM10 to 1,0
TCCR4B = 0b10001001; // mode 6, clock prescaler by 1 and WGM12=1 for fast PWM 9 bit
OCR4B =0 ;
TCNT4 =0x01FF ; // Timer/Counter Register
Serial.begin(115200);
}
void loop() {
potValue = analogRead(potPin); // read the pot value
Serial.print(potValue);
dimValue = potValue/2; // PWM the LED with the pot value (divided by 2 to fit in a byte)
analogWrite(led, dimValue);
Serial.print(". ");
Serial.println(dimValue); // print the pot value back to the debugger pane
}
Moderator edit: </mark> <mark>[code]</mark> <mark>
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
If the Mega has a 10 bit ADC you will get values of 0 to 1023 if the pot is wired as a potential divider between grond and Vcc.
AnalogWrite() takes values from 0 to 255
Since I used 9 bits timer so I did the potValue / 2 to get the maximum value of 511 which is equid to 100%. It works fine. My only problem is at the value of 255 which is unstable all the other values are pretty stable with a digital noise less than 3mV except for 255, the noise value over 20mV and the pulse is not stable.
First thing, fix the declaration/initialization of dimValue as noted in reply #2.
Second, you should take a look at the analogWrite() function which in Windows is found at
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\wiring_analog.c
There is an issue when using analogWrite() when you have set up the timers differently than the default setup of the ide.
In analogWrite() you will see the special cases
if (val == 0)
{
digitalWrite(pin, LOW);
}
else if (val == 255)
{
digitalWrite(pin, HIGH);
}
analogWrite() can accept a value larger than a byte for a 16 bit timer, and it will place that value in the appropriate compare match register for the output, but you will need to modify the library or else avoid the special case of 255 when you are taking values up to 511.
Thanks for Cattledog suggestion. In fact, it will be the last step to use your suggestion if I can't find anyway to improve the program. I had tried the long integer and it didn't make any different.
I assume I didn't activate any interrupt in the program, it should not trigger anything at the value of 255. I still don't understand when I set to 10bit, the same thing happen at the value of 255. It will not happen at 511 nor 767. Can someone tell me what I had do it wrong?