Shawn Johnson's alleged stalker Robert O'Ryan is insane ... this according to the guy himself.
O'Ryan -- who's facing charges of felony stalking, felony commercial burglary and two misdemeanor counts of carrying a loaded firearm in his car -- plead not guilty by reason of insanity in L.A. County Superior Court.
As we previously reported, O'Ryan was arrested last year when he attempted to enter the set of "Dancing with the Stars" in an alleged effort to have an encounter with the Olympian.
During the hearing, the judge told O'Ryan -- who faces 6 years behind bars if convicted on all counts -- that if he was found to be insane, he could spend at least that amount of time in a state mental hospital.
O'Ryan's trial is scheduled to begin tomorrow -- and, as we first reported, Shawn Johnson is set to testify against him.