There is not a clear cut answer to this, it depends on whose opinion you accept. Grammarians regularly dispute questions such as this, some insisting that it is a preposition (because of the type of word it is), and others that it is an adverb (because of the function it carries out).
In your sentence:
When Sam came in, I was asleep.
'in' functions in the role of an adverb, modifying the verb 'came'. It is telling us where Sam came.
In the following sentence:
When Sam came in my room, I was asleep.
'in' is clearly a preposition showing the relationship between the subject ('Sam') and the object ('room') of the sentence.
In essence, when a preposition follows a verb and the sentence does not have an object, then the prepositions functions as an adverb. In my opinion, it is correct to call it an adverb. However, there are some who would still call it a preposition.