 getting a weird plot using capacitor and PWM

Hello, i am having a problem. Basically i made this circuit and connected it to arduino (C=100uF, R=1k). By using capacitor and PWM i expected to get a somewhat smooth curve of voltage. But when i am seeing in my plotter is nothing i expected. Does anyone have an idea on what am i doing wrong? This is my code:

float resolution = 4.9;
float voltage = 0;

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

for (int i = 0; i <= 255; i++) {
analogWrite(9, i);
Serial.println(voltage);
delay(10);
}
}  a [URL=http://[**[u]low-pass filter[/u]**](RC Low-pass Filter Design Tool) has a resistor in series with the capacitor.

(That will also create a voltage divider with the existing resistor and diode.)

What is the purpose of this line

analogWrite(9, i);

?

Erik_Baas:
What is the purpose of this line

analogWrite(9, i);

?

To increase the input voltage with every step.

How? I only see a capacitor, a diode and a resistor, connected to A1, A0 and GND.

Yeah, this is true. I didn't realise i forgot to draw this in the circuit, but i connected it this way - that upper node with resistor, capacitor is connecten to PWM pin 9 at the same time.
DVDdoug, im not sure if i understood, but are you implying that i should make an RC circuit? well the thing is i don't really want to make this more complicated

simple problem - you need to increase the pwm frequency

I have a page here showing what I did with PWM - with a sketch

dont worry about the circuit, all you need is the RC filter shown at the left of the diagram.

and the code from the sketch to pick a suitable (higher) frequency.

Also - the c & r form an RC filter - but you have them connected wrong.

Hello John, i think you forgot tu put in the url, could you do it again please?

Hey john, i read your link a bit, i rembember you shared this some time ago. Thanks again. I tried to do as you instructed, increased PWM frequency of pin9 to 31kHz and i tried to rebuild the circuit with the correct way of building RC filter, i think it got a bit better, but there is still something i am doing wrong. My guess is it has something to do the way i change PWM frequency now:

float resolution = 4.9;
float voltage = 0;

void setup() {
Serial.begin(9600);
TCCR1B = TCCR1B & B11111000 | B00000001;

for (int i = 0; i <= 255; i++) {
analogWrite(9, i);
Serial.println(voltage);
delay(10);
}
}

In your sketch, there is much more complicated way to do this. And i could just do ctrl+c and ctrl+v, but i don't think this would help me to understand how this works. Do you think the way i changed pwm frequency is the source of the problem and i should try and understand the way you did that?  I improved this a bit, to the point i think i can go with this. What i did was this:

TCCR2A = B10100011;
TCCR2B = B00000001;

(I used different pin)

1: 100 ohm is MUCH too small for a pin output.

2: You are measuring A0 & A1

A0-A1 gives the current through the LED.

float resolution = 4.9;

..

Serial.println(voltage);

If you are trying to measure the IV characteristic of the LED you will need a buffer as shown in my diagram (op amp 1) to drive more current through the LED without affecting the pwm voltage.

To change the PWM frequency for pin 9 (timer 1) see the atmel data sheet ref 13:11:2 TCCR1B
the code I showed you changes it like this:

mode = 0x01;
TCCR1B = TCCR1B & 0b11111000 | mode;

TCCR1B = TCCR1B & 0b11111000 clears the low order 3 bits ONLY.

| mode; sets the low order 3 bits to 0BMODE ie if mode =1 the result will be
TCCR1B = XXXX X001