We're not talking about personal safety here -- a much weaker thing, safety for your own device. Do you want to be replacing the switching transistor, its driver (microcontroller?), various burned out resistors and whatnot, every time it fails? Do you want it taking out the wall adaptor and/or battery at the same time? (The battery could be a very real fire hazard actually, if it ends up overcharged.)
I don't want to give you the impression that you're doing something stupid. Indeed, this is a good introductory sort of project, and I congratulate you for attempting it, rather than just throwing an all-in-one chip at it (which is still a learning opportunity, if not as in-depth), or god forbid, getting one of those cheap and horrible premade Chinese modules.
But I have to convey, somehow, that what I guess you've read about is dumb, in a sense.
No, it's not at all wrong that PWM makes variable output voltage -- that remains incontrovertibly true! But what does matter is how you get there.
To me, it comes down to three things:
1. While the general aspects of switching supplies are interesting (and challenging) all their own, this isn't about that: it's about control systems. Which, to be fair, is a field of study all its own, but we only need the tip of the iceberg, and that's fine.
2. In this type of switching supply, the one free variable in the system is inductor current. The sin of omission is committed here: an explanation in terms of PWM alone, will simplify, because they are only attempting to explain one property, and one alone: that the output voltage (at DC, under static, ideal, eternal, useless conditions!) is proportional. But there is so much more to the system than this, and we must introduce these ideas so you can work with them.
3. A personal connection. This may not make sense, but as a skilled expert in this art, it is my business to know: what feels good, what feels right, what feels elegant; to know the heart of a problem, and to work with that first and foremost, if it is at all possible.
As technical as the art of electronics may seem, it still remains open to a vast scope of personal, artistic, decisions. And this is one that I cannot compromise my art with, and strongly encourage others to follow! On the upside, anyone who knows the art deep enough to understand my reason, will also agree with my instruction (as you see, perhaps?), so an appeal to the majority or to the experts would have similar results!
So, what do you really need to know?
The system is dynamic. The whole point, of course, of having a switching waveform, is to use an inductor for energy storage. Energy is constantly moving around, and so, it is almost meaningless to speak of "DC", because nothing interesting happens at DC!
You can look at it in the time domain. That is, as time goes on, the switch turns on and off, and the inductor current goes up and down respectively. Indeed, the current follows the inductor equation:
V = L * dI/dt
Where V is the voltage applied to the inductor (which will be about (Vin - Vout) while the switch is on, or about (-Vout) while off), and 'd' means 'change', but if you don't know calculus, then it's sufficient to say deltas, i.e., after a time interval dt, the current rose/fell by dI. (This assumes V and L remain constant during that time interval, but fortunately we can reasonably make these assumptions -- and, we have convenient time events (switch-on and -off) to sample things at!)
This is a differential equation. You have to take the integral to get actual current (rather than just the relative change). But when you integrate, it's "plus a constant". The average value of current through an inductor, due to an applied voltage, is undefined. This is the hazard of which you must be aware. If the input and output voltages are precisely fixed, then the inductor current need not be at all related to the output / load current! If that current gets ratcheted up and up and up, pretty soon (maybe 0.0001 second) your switching transistor simply dies, and now you get full power flowing across the circuit, and things can only get worse from here.
Inductor current varies in the normal course of events (as PWM varies), so we must control this parameter first and foremost, in order to prevent the transistor from failing.
And yes, sure, if you ramp up and down very slowly, you can avoid your driver from delivering excessive current. By the (still overly) simplified statements above, this has no reason to work, but the reality is, the inductor current will eventually equal the load current, quite exactly.
But what's missing is, how do you reach this conclusion? You've jumped from t=10us when the switch turned on and off once, to t=20000us when the microcontroller finally polls the output V/I. What's happened inbetween? A few thousand cycles, but how do you know they were okay?
Dynamics are all about answering that. The full analysis is rather involved (not only calculus, but indeed, differential equations!), but the reduced form of it is this: if the source or load is unstable (prone to changing voltages or currents), you cannot tell that the inductor current will level out smoothly in those intervening 19990 microseconds. It could be up or down, by unlimited amounts in either direction*, at a rate (dI/dt) limited by the voltage difference and the inductance.
(*If you use an "ideal diode" in the converter, otherwise a regular diode of course won't allow < 0A.)
So again -- it is absolutely possible to do this with a microcontroller, but you will have to do quite a lot more than you were hoping for:
a. Current sensor, with a fairly rapid polling rate (preferably once every cycle)
b. Voltage sensor, with a modest polling rate (doesn't need to be > 1/3 the previous rate, actually, but it's usually easier to set up with equal rate anyway)
c. Two PID loops (it sounds like you might've been planning on zero, just an open-loop ramp command)
d. The switching converter itself
e. And some filtering and support circuitry to make everything work.
If you're not opposed to the idea of op-amps, I might suggest building it discrete instead -- this at least saves you the trouble of programming a PID loop (which is actually a DSP (digital signal processing) topic -- a part of control theory, but complicated by the digital part!), and is much easier to debug (you can watch the voltages and currents on an oscilloscope in real time, rather than having to sprintf or debug your way through code).
Tim