The first law is that a robot shall not harm a human, or by inaction allow a human to come to harm. The second law is that a robot shall obey any instruction given to it by a human, and the third law is that a robot shall avoid actions or situations that could cause it to come to harm itself. How did Asimov come up with these?
The Three Laws developed gradually. It started with Robbie, aka Strange Playfellow, Asimov's first robot story, which he wrote in 1939. Asimov thought of robots as “industrial products […] built with safety features” (The Complete Robot, 1982), and so he had a character say: “He just can’t help being faithful and loving and kind. He’s a machine—made so.” Asimov later called this “the first germ […] of the First Law of Robotics.” (“Robots and Androids,” The Visual Encyclopedia of Science Fiction, 1977).
In his next robot story, Reason (written in 1940), a character says, “Those robots are guaranteed to be subordinate.” According to Asimov, “That was a hint of the Second Law” (“The Word I Invented,” The Magazine of F&SF, October 1980).
The third robot story, Liar! (written in 1941), mentions the First Law and hints (again) at the Second Law:
Lanning’s voice was hostile. “What is all this, Dr. Calvin?”
She faced them and spoke wearily. “You know the fundamental law impressed upon the positronic brain of all robots, of course.”
The other two nodded together. “Certainly,” said Bogert. “On no condition is a human being to be injured in any way, even when such injury is directly ordered by another human.”
As Asimov puts it, “What had been a simple ‘made so’ in ‘Strange Playfellow’ had now become a ‘fundamental law’ of robotics.” (Opus 100, 1969).
Asimov’s fourth robot story, Robot AL-76 Goes Astray (written in 1941), again mentions the first and the second law:
It also struck him that he had heard somewheres that robots were mentally incapable of harming human beings.
What he didn’t know was that a robot always obeyed a human order except where carrying it out involved danger to another human.
But it’s the fifth robot story, Runaround (written in 1941), in which the Three Laws were finally given in full (although Asimov slightly changed the wording later):
“Now, look, let’s start with the three fundamental Rules of Robotics—the three rules that are built most deeply into a robot’s positronic brain.” In the darkness, his gloved fingers ticked off each point.
“We have: One, a robot may not injure a human being under any conditions—and, as a corollary, must not permit a human being to be injured because of inaction on his part.”
“Two,” continued Powell, “a robot must follow all orders given by qualified human beings as long as they do not conflict with Rule 1.”
“Three: a robot must protect his own existence, as long as that does not conflict with Rules 1 and 2.”
Regarding who came up with the Three Laws, Asimov or John Campbell, it seems that they credited each other:
It was he [John Campbell] who considered my third or fourth robot story, shook his head and said, “No, Isaac, you're neglecting the Three Laws of Robotics which are—” and that was the first I heard of them. […]
He admitted he fed me ideas but he said he kept on doing so only because I brought them back changed and improved. He denied he had made up the Three Laws of Robotics and insisted he found them in my stories and merely put them into words.
“The Father of Science Fiction,” Luna Monthly #27S, August 1971
[O]n 16 December 1940, when I was preparing to write “Liar!” (which eventually appeared in the May 1941 issue of Astounding), I brought the plot to John Campbell, editor of the magazine. As I tried to explain what made my robot work the way he did, John lost patience with my attempts and said:
“Look, Isaac, it boils down to this. There have to be the three laws of robot behavior: First …”
And he went on to recite the three laws nearly as I finally worked them out. He always claimed thereafter that he had merely distilled the three laws out of what I was trying to tell him, but all I know is that I heard them from him first and I didn't have them clearly in my mind, in words, before then.
“Robots and Androids,” The Visual Encyclopedia of Science Fiction, 1977
In this issue of Compute! magazine Asimov discusses the Three Laws as something that is "obvious from the start, and that everyone knows subliminally"; he credits himself not so much with inventing them but in writing them down in easily memorable fashion, and in particular draws a direct parallel between the Laws of Robotics and the (implicit) laws governing the design of tools:
- a tool should be safe to use
- a tool should carry out its task accurately and efficiently, to the extent that this is possible without compromising safety
- a tool should be durable and re-usable, unless the nature of its task, or the safety of the user, requires consuming it
Wikipedia attributes the specific formulation of the Laws to collaboration between John W. Campbell and Asimov, saying that "Asimov had the Three Laws already in his mind and that they simply needed to be stated explicitly. Several years later Asimov's friend Randall Garrett attributed the Laws to a symbiotic partnership between the two men"; the cite for this is Asimov's autobiography, In Memory Yet Green, and excerpts from it support this, although I don't have a copy of my own to check the full text.