How to write a program to create multilevel waveform using arduino
If you need a 24 volt signal you would need at least an extra transistor as shown.  Also note that the basic PWM output will only give a digital average of a "staircase" waveform.  Adding a resistor/capacitor to the final output could yield a better DC like staircase but may have a slow response between steps. In the circuit the selected values of R1 and C1 will determine the rate at which the staircase levels change.  Also, since an NPN transistor is used here the PWM value will create an inverted output (eg. 0-255 creates 24v-0v), so you may want to invert the way you adjust the PWM signal. 







simulate this circuit &ndash; Schematic created using CircuitLab

Another method could use 3 (or more) digital outputs and create a much squarer wave form using just transistors and resistors.  In this case the simple on/off digital code determines the final output voltage. (Mosfet transistors could also be used here as a BJT transistor can have a small Vce voltage that prevents a full 0v output).







simulate this circuit