Laboratory 8 Pulse width modulation (PWM), D/A conversion, analogue outputs, analogue filters 
[28/4] 
1. Specifications
Our first goal is to generate analogue voltages using (1) PWM techniques and (2) specific D/A chips. For instance, DC, triangular, ramp and sinusoidal waveforms at different frequencies.
We also aim conditioning analogue outputs (1) filtering high frequency noise associated to the D/A conversion process and (2) generating current outputs to drive devices such analogue panel needle meters or 420 mA current loops.
Fig. 1. Block diagram of several analogue output circuits from Arduino. 
Single power supply 5V
Active 0dB firstorder filter (20 dB/dec), 5V railtorail operation, cutoff frequency fC= 100 Hz.
Active 0dB secondorder Butterworth response filter (40 dB/dec), 5V railtorail operation, cutoff frequency fC= 100 Hz
Modify and enhance the previous application LAB7 so that in a new state (waveforms) the following voltages and currents are calculated:
 Output VO6 generates the same analogue voltages acquired at VS1
 Output VO5 drives the needle panel indicator coil from 0 to 100%
Drive a 1 mA = IFS needle panel indicator from VO9.
Analogue values will update every TD = 2 s (distribution CLK period).
Pulsewidth modulation Wikipedia page.
Digitaltoanalog converter Wikipedia page.
Wikipedia LPF reference.
Example of passive and active filter circuits.
Other references: Thomas, R. E., Rosa, A. T., Toussant, G. J, "The Analysis and Design of Linear Circuits", 7th ed, John Wiley & Sons, Inc., 2012
TI, TLV237x: RailtoRail input and output operational amplifiers with shutdown
TI, SBOA231: Singlesupply, 2ndorder, multiple feedback lowpass filter circuit
TI, SNOA224A: AN779 National: A basic introduction to filters  active, passive, and switched capacitor
TI, SLOA049D: Active LowPass Filter Design.
Proteus: an introduction to active filters (ref.)
Let us drive a moving coil panel meter type SD38/01MA. as shown in Fig. 2. The meter scale can be customised to any quantity and span as shown in this reference page.
Fig. 2. Panel analogue needle meter. 
Let us drive the coil with a voltage controller current source. This are useful references from Texas Instruments on the design of Howland current sources (1), (2).
2. Planning
The idea is to continue expanding our previous project LAB7 adding jumpers to three specific pins so that they can be available as analogue outputs disconnecting the LED.
For example, we can connect a D/A converter type MCP4921 by means of the SPI bus.
Fig. 3. MCP4921 breakout (12bit D/A) for Arduino (ref. including the circuit). 
Or instead, in a similar way, an external D/A type MCP4725 can be connected by means of the I2C bus.
Fig. 4. MCP4725 breakout board (12bit D/A) for Arduino or Raspberry Pi circuits. 
Arduino circuits will include the D/A and the analogue filters.
Fig. 5. Arduino connections to add analogue filters to the three PWM outputs 5, 6, and 9, and also an MCP4921 breakout board. 
To design the 1storder LPF5 and LPF6, as shown in Fig. 6, we can choose between two models based on OpAmps. We need to analyse its transfer function H(s) in the Laplace domain in order to locate the pole in the required position on the real axis of the left half of the splane diagram.
ç 
Fig. 6. Firstorder active LPF. 
To design the 2ndorder LPF9 we can choose a multiplefeedback configuration as represented in Fig. 7. We need to calculate its transfer function H(s) and be able to locate the conjugate poles so that the frequency response is maximally flat in the bandpass (Butterworth response).
Fig. 7. This is a proposed second order lowpass filter (40 dB/dec). 
To design a voltage to current converter (or transconductance amplifier) we can use the Howland current source shown in Fig. 8 and adjunct it to our requirements to drive the needle panel indicator proposed in Fig. 2.
Fig. 8. An example idea on the design of a voltage controlled current source. 
Function analog_write() function to generate analogue signals.
Interfacing SPI MCP4921 chip, example page.
Interfacing I2C MCP4725 chip, example page.
To start experimenting with analogue output generation we will add another state "waveforms" to our FSM.
Project location:
C:\EMC\LAB8\Temp_LCD_DA\(files)
3. Development and 4. testing
Fig. 9 shows the virtual simulation firstorder filters: filters_freq_response_1st_order.pdsprj.
This is the virtual simulation secondorder filters: filters_freq_response_2nd_order.pdsprj.
Fig. 9. Second order filter captured as a subcircuit in Proteus. It can be tested using instruments and advanced graphical analysis. To obtain the filter frequency response we can simplify the OpAmp using it ideal model. 
This is the virtual simulation of the voltage to current converter: Howland_current_source.pdsprj.
Fig. 10 shows the new board enhanced from LAB7 including the digital to analogue new blocks. It includes the software program: Temp_meter_LCD_DA.zip.
Fig. 10. Application captured in Proteus. Ready for simulation and a first version of the PCB design. 
We have to add a new state to generate correctly the D/A signals at the given distribution CLK.
Fig. 9. Modified state diagram. 
5. Prototyping
6. Reporting
Modify and enhance the application so that in the new state (waveforms) another voltage is calculated:
 Output VO9 generates an analogue voltage corresponding to the mean value of the three analogue inputs.
Finally, while the FSM is running, generate an audible 1 kHz sinusoidal waveform output VO from the SPI D/A converter.
Annex: Fourier series of a PWM waveform
We can represent a rectangular PWM waveform using a Fourier series, an infinite number of harmonic.

Fig. 1. Fourier representation of a PWM waveform (ref.) 
In this way, it is easy to visualise how choosing correctly the cutoff frequency of a LPF we can generate the waveform mean value.
Fig. 2. Mean value of the PWM waveform filtering all the harmonics. 