Pin Output Question

Sorry if this a stupid question but im new to all this.

I want to use a pin to output a voltage range from 0 - 3.3v or 5v

can i code it like this:

analogWrite(13, 200);

a snippet of code will be amazing…

Thanks,

Not really. The analogWrite does PWM (pulses). That works OK with LEDs because if you drive it for 50% of the time it looks 50% as bright (roughly). But that doesn't mean it is driving the LED at 2.5V. It is driving it at 5V for 50% of the time.

So how can i accomplish to have as an output a range from 0 - 5volts ?

What do i need to buy?

Thanks

Please guys... someone??

An RC filter, consisting of just a resistor and a capacitor, will convert that PWM output to a steady voltage, which can then range from 0 to just under 5 volts (there is some loss due to the filter).

By the way, bumping after 12 minutes is generally (well, always) frowned on.

You mean a continuous range? Or exactly 0, 3.3 or 5V? And what current will be drawn?

The answer would influence the solution.

The general approach would be some sort of DAC (digital to analog converter). Just as an example: MCP4921. That is driven by SPI and has 12 bits resolution. However its maximum output current is 25 mA.

Perhaps if you described what you are trying to achieve with this?

PaulS:
An RC filter, consisting of just a resistor and a capacitor, will convert that PWM output to a steady voltage, which can then range from 0 to just under 5 volts (there is some loss due to the filter).

That sounds simpler.

arkas99:
Please guys... someone??

Give me a bit of a break. I was researching DAC chips, and typing in a reply. Oh yeah, and climbing the stairs.

I want to control a Galvo from a hard drive, it gets as input a minimum amout of voltage.
i tryed it out with a 1.5 AA battery with a potencimeter and i could control it precisly.

I want contiunous range from 0 - 3.3 or 5v or even less.

Thanks

i tryed it out with a 1.5 AA battery with a potencimeter and i could control it precisly.

Then get a digital potentiometer.

For the fun of it I tried out Paul's idea of the resistor and capacitor:

This sketch:

void setup () { }

void loop () {
  analogWrite (3, analogRead (A0) / 4);
}

Can't get much simpler than that, huh?

That worked pretty well. I measured from 0V to 5V (DC) on the meter as I turned the knob.

With a scope it looked like it had around 70mV to 100mV ripple at 485 Hz, the frequency being what the analogWrite page predicted.

Substituting a 1 uF capacitor gave around 560 mV of ripple.

For the fun of it I tried out Paul's idea of the resistor and capacitor:

Cool... now put a load on it. :wink:

I did it and i got stable readings from my multimeter but somehow something the way the volts come up is not linear.. sundely from 0 goes .5 then .6 then .7 there are not many steps...

any suggestions?

Yes it seems pretty sensitive to loads. :slight_smile:

You were expecting that, huh?

I suppose the voltage drop over the 4.7K resistor will go up with current.

arkas99:
I did it and i got stable readings from my multimeter but somehow something the way the volts come up is not linear.. sundely from 0 goes .5 then .6 then .7 there are not many steps...

Oh? What did you do exactly?

Yes it seems pretty sensitive to loads.

You were expecting that, huh?

Yes I would, it would be usable feeding directly only into high impedance loads like a analog input pin. However it is easily buffered with a op-amp buffer stage.

Lefty

Like this you mean?

Yes, that seems to work pretty well. The maximum voltage at output now seems to be 3.4V which seems to be in spec for that chip. I imagine you could compensate for that by supplying the op-amp with a higher supply voltage (eg. 7V).

Just as a follow-up. When I wired pin 8 of the op-amp (Vcc) to a higher voltage (eg. Vin on the Arduino board when it is powered by a plug-pack) then the range is now 0V to 5.0V rather than 0V to 3.4V.

So, improved circuit: