getting low output voltage after adding a resistor

my question is that, can I get the five volt output by only modifying the code?

Not with the filter and the load... The 1K resistor in the filter and the 1K load form a 50/50 [u]voltage divider[/u]. With two equal-value resistors you should get 2.5V out with 5V in.

Note that an output-high (or PWM=255) may not give you exactly 5V out, especially with a load. And you can't get exactly 5V is your 5V power is a little low.

The input of an op-amp is very-high impedance so there will be essentially no voltage divider effect. However, your op-amp may need a power supply higher than 5V if you want 5V out, and you may need dual power supplies (positive and negative) if you need to get-down to zero-volts out. Or there are "rail-to-rail op-amps that should get close to the full 0 - 5V range.

I am trying to build a voltage tracker with a feedback loop

The op-amp buffer will do that with no software. :wink:

There are current limits for op-amps too. A 1K load will be OK with most op-amps, but regular op-amps are not "power supplies" or "power amplifiers".