13

Regarding my program, it is a program that does some calculations and then outputs a voltage based on the result using analogWrite function. However my problem is that I had done my programming based on a misconception that analogWrite function via PWM does output an analog voltage, where in fact it only "simulates" the analog voltage instead.

Here are the relevant parts of my code :

int pwmOutput = 11;
int pwm = 0;

void compareNewOldVoltageYes(void)
{
  if(pv_Vnew > pv_Vold && pwm != 255)
  {
    ++pwm;           //increasing value
  }
  else if(pwm != 0)
  {
    --pwm;           //decreasing
  }
}

void compareNewOldVoltageNo(void)
{
  if(pv_Vnew > pv_Vold && pwm != 0)
  {
    --pwm;
  }
  else if(pwm != 255)
  {
    ++pwm;
  }
}

void loop() 
{
   reading();
   PowerCalculation();

   if(pv_NewP > pv_OldP)
   {
      compareNewOldVoltageYes();
   }
   else
   {
      compareNewOldVoltageNo();
   }

   analogWrite(pwmOutput, pwm);           //analogWrite

   float displayPWMvolt = pwm * 0.0196;

   Serial.print("Output Voltage: ");
   Serial.print(displayPWMvolt);
   Serial.println(" V");

   pv_OldP = pv_NewP;

   Serial.print("Previous Power: ");
   Serial.print(pv_OldP);
   Serial.println(" W");

}

As for the circuitry, output pin 11 is being connected to a 1 ohm resistor and then to GND. (Where I measured the voltage) I am using Arduino Uno board.

UPDATE:

As said in the comments by @DatHa, pwm does not output voltage. Is there a way to output voltage as said, without changing the board?

5
  • 1
    P.S. Analog write uses PWM and it DOES NOT produce an analog voltage.
    – Dat Ha
    Nov 28, 2016 at 1:30
  • @DatHa does that mean it cannot be done this way? Is there another function to output analog voltage?
    – bytk
    Nov 28, 2016 at 1:32
  • See @Russell answer on arduino.stackexchange.com/questions/10041/…
    – duck
    Nov 28, 2016 at 1:34
  • @dpw I don't think that works as my program has to connect to an electronic load via its external programming port and control it using 0-5V.
    – bytk
    Nov 28, 2016 at 1:43
  • PWM outputs a specific voltage, or I'm a pirate in the year of our Lord 1734. You can't go above an 8-bit value, and there's always some inaccuracy in reading it.
    – user400344
    Nov 29, 2016 at 22:12

6 Answers 6

13

You basically have three options:

  1. Switch to an Arduino Due which has a built-in DAC which outputs a real voltage.
  2. Add an external DAC chip (such as the MCP4821/2) to create the voltage for you
  3. Use a low-pass filter (R-C network) on a PWM pin.

Of the three options I usually use an MCP4822 since it gives the best results and doesn't cost as much as using a Due.

5
  • Thank you. Option 2 sounds really good, but due to time constrains, I went ahead with Option 3 and got the results I wanted.
    – bytk
    Nov 28, 2016 at 13:48
  • 1
    or use an R-2R ladder as a "poor man's DAC" Nov 28, 2016 at 16:54
  • 1
    @AndreHolzner I count an R-2R ladder the same as a DAC chip but with too many wires to make it practical on your average Arduino.
    – Majenko
    Nov 30, 2016 at 0:35
  • @Majenko I just wanted to add that there exist "digital potentiometers" or "digipots" that are pretty much R-2R ladders inside a DIP-8 chip. They are cheaper then DACs. (For example X9C103) Mar 28, 2019 at 7:55
  • 1
    @FilipFranik Except they're not R-2R ladders. They're just a chain of resistors with a FET connected to each connection between resistors. There is only R. Many Rs all the same. Yes, if you write it up with the two ends of the potentiometer to VCC and GND you get kind of a DAC, but it suffers the same kind of impedance problems a real potentiometer does. It provides a pair of resistances, whereas a DAC provides a voltage.
    – Majenko
    Mar 28, 2019 at 12:27
7

At 5V, a 1 ohm resistor will try to sink 1A and far exceed the 40mA specs. Please use at least a 5/0.040=125 ohm resistor to protect your pin. And if you put the a capacitor between your resistor and ground, the RC circuit of the capacitor will smooth out the PWM into an analog voltage.

Please try the suggested @russell answer with a 47K resistor and 1uF capacitor, you will get an analog voltage at the junction to use with your electronic load.

0
2

As far as I know Arduinos have ADC (Analog to Digital Converters) but do not have any DAC (Digital to Analog Converters). So you can not output a set voltage from any pins based on a digital value.

1
  • 3
    The ARM based Arduinos usually have at least one DAC channel.
    – KIIV
    Nov 28, 2016 at 7:54
1

There is no direct way.

In addition to @Majenko

Alternative way: you can use H bridge like L293d to provide continuous level. Many diy inverter circuits are useing this technique.

http://www.instructables.com/id/How-to-Make-an-Inverter-Using-ARDUINO/?ALLSTEPS

0

NO.. There is no way to get an analog voltage from the Arduino. The best that you can do is to use Arduino as a Buck Voltage converter in linear or boost mode using a voltage regulator, inductor and the mosfet transistor. You will also might need to use a Mega or Dulorme which provide a PWM frequency other that the standard 50o Hz.

Best luck

0

I wondered if this would give a more stable output.

PWN presented to RC.  Cap charges, sample is made by another analog input.  When PWN pin goes low, slow discharge of cap.  Maybe a large resistor is needed in parallel with the cap for slow discharge?

1
  • You need to add an R in parallel with C1 to discharge C1.
    – MatsK
    Aug 23, 2020 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.