Creating sawtooth wave

I am trying to create a sawtooth waveform using a charging resistor (100k), discharging resistor(100k), 47uF capacitor and an Arduino Nano.

I am using the serial monitor to view the voltage across the capacitor.

void setup() {
pinMode(A1, OUTPUT);
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop() {
  digitalWrite(A1, HIGH);
  for(int i = 0; i < 471; i + 1)
  {
  Serial.println(analogRead(A0));
  delay(10);
  }
  digitalWrite(A1, LOW);
  for(int i = 0; i < 471; i + 1)
  {
  Serial.println(analogRead(A0));
  delay(10);
  }
}

The problem is that the capacitor charges perfectly on the serial plotter, but stays there, it does not
return to 0 volts.

Secondly, the maximum voltage reaches only 2.5 volts (512 on serial plotter). \

My circuit is 100% correct, and to confirm I used a switch to manually charge and discharge which worked.

Can I please get some help?
Thanks.

Do you know what a sawtooth wave should look like? A constant ramp that suddenly resets back to the starting level at regular intervals.

I don't think you'll get that by alternating a pin high then low for equal amounts of time, but I can't tell without seeing your circuit.

You seem to confuse a triangle wave with a sawtooth.

You only need a single resistor, which you alternate between HIGH and LOW to charge and discharge. I think
you must have somehow arranged that the capacitor is charged by a 100k:100k resistor divider to
half the supply voltage.

Here is the circuit. The square wave is applied across the capacitor.

Remove the resistor to +5V, you only need a single resistor connected to the output pin.

MarkT, I think you are right, I have confused a triangle and sawtooth. I removed the second resistor and now, I am reaching 5 volts, but it is not returning to zero.

This is the wave.

Then you probably removed the wrong resistor!

Discharging a capacitor through an output pin - I think you need to be careful here. It could be quite a lot of current (even though it's only for a tiny fraction of a second).

I don't know what the specs are for a transient current spike like that but the idea makes me feel bad.

Thank you guys, it was my silly mistake.
I figured that the reason it was not returning to 0 volts is because I had set the for loop counter increment as
i+1 and not i = i+1. That solved it!!!
Thanks again. This forum is very quick in responding and I was pleasantly surprised.
The code and output wave are down below.

:slight_smile: :slight_smile: :slight_smile:

GypsumFantastic:
Discharging a capacitor through an output pin - I think you need to be careful here. It could be quite a lot of current (even though it's only for a tiny fraction of a second).

I don't know what the specs are for a transient current spike like that but the idea makes me feel bad.

I am with you on this one, but it's a triangle, not a saw. to create a saw there should be a diode to drain the voltage back down, but that is a little risky a dare say.