24V output with microcontroller using mosfets
A MOSFET needs a gate voltage well above the source to conduct properly, for your type ~ 10V. Obviously your circuit can't deliver this. That's why the common way to use such a transistor is with the source grounded and the load at the drain, and teh collector of the PC817 at a suitable voltage level (12V would be OK).