I am struggling to create a sawtooth wave using arduino UNO R3.
I would like to end up having sawtooth waves with a certain delay from each others (have a look at the sketch). I have tried so many time to change the code, like using analogWrite instead of digitalWrite, using analogic ports, and many other attempts with no success.
//This section defines the pins on the Arduino. We specify the intigers (int) TTL1 through TTL4.
//If you expanding this to require 6 channels, uncomment the last two lines here and add additional code below where needed.
int TTL1 =5; // for optogenetic and openephys
//int TTL5 = 12;
//int TTL6 = 13; //Though, not recommend, as there are slight differences in this pin. If you must use pin 13 as a digital input, set its pinMode() to INPUT and use an external pull down resistor.
//This tells the Arduino to treat the pins as output. In this example, all channels are output. For an example with input (closed loop), see the Arduino-Simple_Closed_Loop.ino file section.
void setup() {
pinMode(TTL1, OUTPUT);
}
//Draw out the desired waves for each channel on paper, on the same timeline, such that channel 1 appears over channel 2, etc. The condition (or state) of the Arduino changes any time any of the channels change. See the write up for more information.
//Define the number of conditions. Define the length of time (in ms) for each condition.
int con1 = 2000;
int con2 = 10;
int con3 = 200;
int con4 = 7000;
//This is the code that the Arduino loops through. It will start on condition 1 and then move through the rest of the conditions and then auto restart back at condition 1.
//It writes each pin as High/ON or Low/Off for each condition, and the waits for the duration of condition (set above).
void loop() {
//Condition 1
int i;
for(int i = 1; i <256; i = i +1)
{
digitalWrite(TTL1,i);
delay(con1);
digitalWrite(TTL1,LOW);
delay(con4);
}
}

The best I can get out of this code is a square wave.
int PWM_PIN = 5; // Pin for generating the sawtooth wave
void setup() {
pinMode(PWM_PIN, OUTPUT);
}
void loop() {
for (int i = 0; i <= 255; i++) {
analogWrite(PWM_PIN, i); // Generate the sawtooth wave by varying the PWM duty cycle
delay(10); // Delay between each step of the wave
}
}
barshatriplee's sketch gives out a rectangular waveform, but if you feed it in to a low pass filter, then you can get a sawtooth waveform at the output.
In the following oscilloscope traces, I've used barshatriplee's sketch and a low pass filter with R = 10kΩ and C = 1µF.
The yellow trace is Vin and the blue trace is Vout.
to be fair to both parties here maybe you could say @Grumpy_Mike has partially corrected @jim-p . While you can generate complicated waveforms it's always going to be at a much lower frequency than a true DAC. So if the definition of complicated involves high frequency (with high resolution) then @jim-p I would say is still partially correct. @Grumpy_Mike is also quite right to note that complex waveforms that can be generated at low frequencies. My guess is that the OP is probably looking in the frequency range where PWM would be just fine but I can't quite work that out from the code posted. @zonedro could you say what is the ramp time of your sawtooths?
Hello @JohnLincoln .could you please revise my code and adding the low pass filter in order to have the final output as in the pics in the attachments?