I was trying to make my servo motor move 90 degree each time I put my hand near the ultrasonic sensor, and wait 5 sec, turn back to 0 degree. And I've tested that my sensor works. The only thing that's weird is that, my servo motor keep turning even when the ultrasonic reading is large, and stop when the value is small. I don't know what happen.
I have seen this with the SG90. Specifically, the one that came with the Elegoo Arduino Starter Kit. I was abusing an Uno R3 by powering the servo with the Uno +5V pin, and it rotated through 360 degrees multiple times until I cut the power.
I then ran the same code on the same servo on the same Uno, powering the servo at 5V with my bench power supply, and it worked normally again.
Can't speak to why this was the case, but for me, this was the case.
Maybe there are no hard end stops in those cheap servos.
This problem has come up a lot in the forum, lately, and it is often the case that people are following the terrible advice to power the servo from the 5V pin.
Maybe, I couldn't say. I just tried rotating the shaft on mine, two full rotations counter clockwise before it stopped; six full rotations clockwise and it still freely turned. Then went to go back again and heard a little plastic snap like it was forced past a stop. I wasn't applying much torque at all.
Maybe just really cjeap or undersized, worn down, I don't know.
A PC PSU that was converted into a convenient DC supply for electronics prototyping and testing. It's an extremely popular way to get a 3.3V, 5V and 12V power supply capable of allowing higher current devices to be powered off it.
If you don't have one and are getting more into electronics, it's essential. It's also very easy to make one. Tons of tutorials online.
Yes, you can purchase commercially available bench power supplies; however, it seems to me that making your own is almost a rite of passage for the novice electronics hobbyist.
Mega and Uno R3 have the same 5V regulator for all intent and purpose, ie, not suitable for powering servos. My SG90 is a generic type of yours, not specifically Tower Pro brand.
Not all (cheap) servos can do the full 180 degrees,
so if you don't need to swing 180 degrees, don't try to get to 0 or 180.
It seems you only need a swing of 90 degrees.
In setup() you should have written to 45 degrees, and in loop() 135 degrees. That would also have given you a swing of 90, without getting close to the potentially damaging end stops.
Leo..
So basically, my micro servo SG90 is only suppose to turn 180 degrees, but it turn full 360. Also, when I use write command in the setup, it's suppose to run it only 1 time. But it keep running for a long time after I upload the code to the board.
This topic sounds familiar. (the verdict was inconclusive)
This sketch (in a 180 servo simulation) might work on your servo... if it is a 360 servo, if will slowly increase in speed in a clockwise direction, then decrease speed in a clockwise direction, then repeat the same in the counter-clockwise direction.