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);
    voltage = (analogRead(A1) - analogRead(A0)) * resolution;
    Serial.println(voltage);
    delay(10);
  }
}

obvod.jpg

obvod.jpg

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

Hi @Bergerino

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?

http://www.skillbank.co.uk/arduino/char.htm

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);
    voltage = (analogRead(A0) - analogRead(A1)) * resolution;
    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?

circuit2.png

circuit2.png

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;

..

    voltage = (analogRead(A0) - analogRead(A1)) * resolution;
    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

your code

  TCCR2A = B10100011;
  TCCR2B = B00000001;

changes ALL the settings for timer 2.

As what you are trying to do is EXACTLY what my page shows why not just build it - op amps are fun.

Whole circuit - £2 5 op amps for £1

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.