An electronic device comprising: a first housing; a second housing coupled to the first housing, and moving in a first direction for moving from a first position to a second position or in a second direction which is opposite to the first direction between the first position (P 1) at least in part accommodated in the first housing and the second position protruding from the first housing; a driving unit for moving the second housing in the first direction or the second direction; a first sensor (223, 225) for detecting a position of the second housing; at least one processor operably coupled with the driving unit and the first sensor; a second sensor for detecting a temperature of the electronic device; and a memory operably coupled with the at least one processor, wherein the memory stores instructions, when executed, causing the at least one processor to, detect an input for moving the second housing in the first direction, control the driving unit to drive the driving unit for a first time to move the second housing in the first direction, identify whether the second housing is positioned at the second position, through the first sensor, in response to identifying that the second housing is not positioned at the second position, control the driving unit to drive the driving unit for a second time to move the second housing in the second direction, and control the driving unit to drive the driving unit for the second time, and then control the driving unit to drive the driving unit for a third time to move the second housing in the first direction, wherein the instructions cause the at least one processor to, obtain information of the temperature from the second sensor, and control the driving unit to drive the driving unit at a driving frequency corresponding to the temperature for the second time to move the second housing in the second direction. a first housing; a second housing coupled to the first housing, and moving in a first direction for moving from a first position to a second position or in a second direction which is opposite to the first direction between the first position (P 1) at least in part accommodated in the first housing and the second position protruding from the first housing; a driving unit for moving the second housing in the first direction or the second direction; a first sensor (223, 225) for detecting a position of the second housing; at least one processor operably coupled with the driving unit and the first sensor; a second sensor for detecting a temperature of the electronic device; and a memory operably coupled with the at least one processor, wherein the memory stores instructions, when executed, causing the at least one processor to, detect an input for moving the second housing in the first direction, control the driving unit to drive the driving unit for a first time to move the second housing in the first direction, identify whether the second housing is positioned at the second position, through the first sensor, in response to identifying that the second housing is not positioned at the second position, control the driving unit to drive the driving unit for a second time to move the second housing in the second direction, and control the driving unit to drive the driving unit for the second time, and then control the driving unit to drive the driving unit for a third time to move the second housing in the first direction, detect an input for moving the second housing in the first direction, control the driving unit to drive the driving unit for a first time to move the second housing in the first direction, identify whether the second housing is positioned at the second position, through the first sensor, in response to identifying that the second housing is not positioned at the second position, control the driving unit to drive the driving unit for a second time to move the second housing in the second direction, and control the driving unit to drive the driving unit for the second time, and then control the driving unit to drive the driving unit for a third time to move the second housing in the first direction, wherein the instructions cause the at least one processor to, obtain information of the temperature from the second sensor, and control the driving unit to drive the driving unit at a driving frequency corresponding to the temperature for the second time to move the second housing in the second direction. obtain information of the temperature from the second sensor, and control the driving unit to drive the driving unit at a driving frequency corresponding to the temperature for the second time to move the second housing in the second direction.