I need to make a really simple circuit. I am going to light a LED up but LED works between 1,2 - 1,5 volts, so I need to get an output from Arduino and set the output up as only 1,2 volts.
I do not want to use any resistors or something else, I aim to get it via coding.
Will anyone help me by showing an example code or something like that?
You need to understand that the PWM is 5V pulse and you need the resistor to limit the current when 5V is output.
The fading takes place because of the ON/OFF of PWM, the longer it is ON compared to OFF the brighter the LED will be, and vice versa.
It is standard practice to include a current limit resistor, rather than try and make the output voltage exactly what the LED is rated at.
Not all LEDs are the same, even if they are from the same batch.
Also if you have excessive current flowing through the LED from the Arduino, not only will you destroy the LED but the output of the Arduino, as it has a maximum current rating of 40mA.
I do not want to use any resistors or something else, I aim to get it via coding.
You can't do everything in software... Some things take special hardware.
An analogy - You can't dim a light in your house without a dimmer. A regular light switch only goes on & off and you can't control it with your computer. Or, you can't get sound of a computer without a soundcard.
I will do it, connect a 150ohm resistor but it won't prevent burn the led. I still need to reduce any PWM pins' voltage.
The voltage gets divided among the series components and the voltages will sum-up to 5V.* The "magic" of LEDs is that they are non-linear (their resistance changes with voltage) and with a resistor the voltage "falls into place".
The resistor limits/controls the current. Resistance is the "resistance to current flow". More resistance = less current, less resistance = more current (Ohm's Law).
The output voltage may not be exactly 5V, but the series voltages must sum to the total voltage.
fkydln:
I am going to light a LED up but LED works between 1,2 - 1,5 volts, so I need to get an output from Arduino and set the output up as only 1,2 volts.
You drive a LED with CURRENT, not with voltage.
If you drive a fixed current through a LED, then as a result you have a volt drop across the LED.
A resistor in series with the LED is an easy way to set LED current.
If you power the LED/resistor from a 5volt Arduino pin (PWM or not), then you can calculate LED current if you know Vf (working voltage) of the LED. In your case about 1.25volt (infrared LED or opto coupler?).
So 5-1.25= 3.75volt is going to drop across the resistor.
If you choose a 220ohm resistor, then you will have 3.75/220= ~17mA LED current.
Leo..
fkydln:
I need to make a really simple circuit. I am going to light a LED up but LED works between 1,2 - 1,5 volts, so I need to get an output from Arduino and set the output up as only 1,2 volts.
As long as the voltage is >Vf, your LED will act like a short.
As long as the voltage is <Vf, your LED will block.
This of course is a simplification of the reality which is less black and white (or block and short in this case) but that's roughly how an LED works and why your idea won't work to begin with.
You can use pulse width modulation and varying frequencies to generate a given voltage via a digital pin using a simple low pass filter. However, this requirement rules that solution out:
I do not want to use any resistors or something else, I aim to get it via coding.
6v6gt:
You can use pulse width modulation and varying frequencies to generate a given voltage via a digital pin using a simple low pass filter.
Even so you won't be able to drive an LED with such a filtered output. That would need a buffer, such as an OpAmp in unity gain, and that still doesn't solve the current issue of the LED.
fkydln:
. . .
I will do it, connect a 150ohm resistor but it won't prevent burn the led. I still need to reduce any PWM pins' voltage.
. . .
You can vary the brightness of the LED (fading etc.) with PWM (but not the voltage, hence you need the resistor).
Are you trying to achieve a concrete goal or is this a school exercise ?
The fading takes place because of the ON/OFF of PWM, the longer it is ON compared to OFF the brighter the LED will be, and vice versa.
Worth mentioning - just to make life more difficult, the dimming will be non-linear...!
The simplest way to get a smooth fade from 0-255 is to use a byte lookup table that corrects the LED brightness to match the flat-slope of the 0-255 span.
I do not think it feasible to do without resistors and stuff. At the very least you'll need a capacitor. Try looking up 'audio capacitor, 10 Volts, 100 Microfarad'. You blip that with 5V not ten, often enough and briefly enough to stay near enough to your desired LED voltage. To protect your Arduino from damage dumping into an empty capacitor, you should have a resistor. I'd use 3.3kOhm, expecting 1 mA steady and less than 2mA during the initial turn on surge. If 1mA is not enough for the brightness of LED which you want then use either an NPN or NFET below the LED and 10 kOhm from the arduino to its base or gate. The capacitor still goes parallel with your LED. Further improvements from here are possible.
PWM never does 1.5V at the arduino output pin; It does 0 and 5 V and spends about a third of its time at 5 V.
ad2049q:
It does 0 and 5 V and spends about a third of its time at 5 V.
Rubbish. The amount of time it spends at 5V is determined by the value you use when you call the analogWrite function.
@OP - Like the others have said a pure software is not possible.
I am going to light a LED up but LED works between 1,2 - 1,5 volts, so I need to get an output from Arduino and set the output up as only 1,2 volts.
Basically you are misunderstanding how an LED works, you don't put a voltage on it to drive it. When it is driven by the recommended current then a voltage of 1.2 to 1.5 volts will appear across it, not the other way round.