An electronic device is provided. The electronic device includes a housing including at least one receiving hole exposed to an outside, a magnet disposed inside the housing and adjacent to the at least one receiving hole, an input module configured to be insertable into the at least one receiving hole and including a ferrite core configured to block or redirect part of a magnetic field generated from the magnet, a sensor unit disposed inside the housing and configured to sense the magnetic field generated from the magnet, and a controller configured to sense whether the input module is inserted into the at least one receiving hole by a signal according to a strength of the magnetic field sensed by the sensor unit.