How to detect in the code (Not have to design any extra hardware) if a (peripheral) PCB has been inserted/ plugged/ interfaced to the main PCB?
A quick method which is followed to solve this is by making a pin as pull-up and reading if it is high or low. You can make a logic that if the pin is LOW, then the peripheral is connected.
On the other side, the MCU on peripheral side just drives the pin LOW. 

More advanced ways, use PWM signals or a signal pattern with consensus for better communication and authentication across controller.

Little Sophisticated method : One-Wire protocol.