# PWM to analog voltage conversion for communication between two microcontrollers

I want to use this schematic for sending PWM signals from an STM32F0 Arm microcontroller to an ATtiny85. I want to convert the PWM signal to a proper analog voltage, so that the ATtiny analogRead() function can detect a proper ADC value from 0-1023 and assert the voltage and perform a particular action based on the analog voltage detected. So, IO1 in the schematic is directly connected to one of the STM32F0 PWM pins, and the CONTROL pin in the schematic is directly connected to an analog pin of the ATtiny85.

NOTE: Both the STM32F0 and ATtiny85 are running on 3.3V supply, thus operating at 3.3V logic level.

Questions:

1. Is this a proper method to do PWM to analog voltage conversion?
2. Is there any way to improve the output accuracy of the STM32 and the detection accuracy of the ATtiny85 in this circuit design?
3. Are the resistor and capacitor values are good enough for this use case? And what specific SMD parts could be used for this application? Like the type of capacitor, the voltage and capacitance rating, resistor type and sizes etc.

There's a little bit of caveat that I forgot to mention. The STM32F0 controller in question has pre-flashed firmware which only allows me to pull-up or pull-down or generate a PWM signal through the IO pins available. If I hack into the firmware, apparently it voids the FCC and CE certifications and warranty of the product. So, the API is pretty limited.

• If you get rid of the filter and run the pulses into a digital channel instead of an analog channel, you could throw an interrupt every time a pulse edge is detected and count clock cycles between pulses edges. This would let you read the analog value at the expense of an interrupt, but at the savings of external components and/or an analog pin. – schadjo Oct 11 '19 at 14:46
• Unless you define error tolerances for slew rate of PWM vs ADC out, it is not possible to ensure success. The filter shown has Tau=RC=3.3ms delay. So PWM f and rate of change with max error must be defined – Tony Stewart Sunnyskyguy EE75 Oct 11 '19 at 14:58
• On the face of it, it sounds silly to convert digital data to an analog voltage just so another MCU can measure it in order to get the original data. There are so many ways to transmit the data in digital form directly between the two MCUs -- have you considered any of them? – Dave Tweed Oct 11 '19 at 15:14
• @DaveTweed I do not know about those type of communication protocols. I only know about I2C and SPI which are more than 1 wire communication. I need something which will be able to communicate data through 1 wire and a common ground. Any suggestion on how to do this? – Farukh Oct 11 '19 at 15:17
• "...ATtiny analogRead()" is this an Arduino function? If so then perhaps you could use pulseIn() to read the PWM pulse width arduino.cc/reference/en/language/functions/advanced-io/pulsein – Bruce Abbott Oct 11 '19 at 18:46

Is this a proper method to do PWM to analog voltage conversion?

Yes.

Is there any way to improve the output accuracy of the STM32 and the detection accuracy of the ATtiny85 in this circuit design?

Use the highest available PWM frequency. Make the r/c time constant large enough that voltage ripple is less than the accuracy you need, but small enough that you don't need to wait too long for the voltage to settle.

To reduce measurement noise, take multiple readings and average them. With a 10 bit ADC this can be done by simply accumulating (adding) 32 consecutive readings in a 16 bit Int, then dividing the result by 32.

If the PWM frequency is low and you need high accuracy the total acquisition time could be very long. The ADC will also introduce more errors due to noise and non-linearity. Don't expect to extract 10 bits from a 10 bit PWM!

Are the resistor and capacitor values are good enough for this use case?

Depends on the PWM frequency and required accuracy. The resistor value should be much higher than internal resistance variation of the PWM output port, but low enough to satisfy ADC input requirements. 1-10k is probably OK.

And what specific SMD parts could be used for this application? Like the type of capacitor, the voltage and capacitance rating, resistor type and sizes etc.

Sizes shouldn't matter in this case since the current and power dissipation is low. The capacitor should be rated for at least 4V, but if you use a multilayer ceramic (MLCC) type a much higher voltage rating may be needed to maintain capacitance over the working range.