Opto coupler to limit switch with Nano

I want to connect a NS04-N limit switch to a Nano. The limit switch requires a voltage of 10-30 volts to work correctly. The output from the limit is too high for the Nano, so I thought of connecting it through an optocoupler like a PC218.
If I connect the NS04 to a 12 volt supply and take the signal out which is 12 volts to a PC218 opto with the appropriate dropping resistor, and then connect the collector of the opto to the Nano 5v pin and the emitter of the opto to the limit pin on the Nano, would that work OK?

I am not sure about this type of connection or the values of the resistors needed if someone can help me please.

You could use a voltage divider like:
sw-12V.png

sw-12V.png

JCA79B:
You could use a voltage divider like:
sw-12V.png

Wow, that is too easy, I like it, thanks

Note that if you increase the resistor values a bit (1.5-2x the current value) this circuit is also safe with 30V input, as R1 limits the current through the pin sufficiently.

Noticed that you might have a proximity switch with open collector NPN transistor switch, in that case this will be a better circuit:
Image-0001.png
EDIT: Be SURE to get the diode polarity right, cathode (end with stripe or band) toward limit switch.

Image-0001.png