SG90 Arduino Servo Sweep
Drive a 9 g micro servo from 0 to 180 degrees with a single PWM pin — the same hobby servo behind every RC car steering arm and entry-level robot joint.
Difficulty: Breadboard. Estimated build time: about 20 minutes. Estimated parts cost: about US$3.20. 6-line bill of materials. Compare supplier offers when available. A microcontroller-driven hobby servo that smoothly sweeps its horn from 0 to 180 degrees and back.
Wire power and the signal pin
Connect the SG90's red wire to a 5 V supply, brown wire to ground, and orange wire to any MCU GPIO that can output PWM at 50 Hz (any digital pin works with the Servo library — it bit-bangs the timing). Tie the supply ground to the MCU ground; both grounds must be common or the pulse won't be referenced correctly. Add a 100 uF electrolytic from the servo's 5 V to ground right at the servo lead — when the motor kicks on it pulls a sudden burst of current and that cap keeps the rail from dipping enough to reset the MCU.
Sweep with the Servo library
Open the Arduino IDE and load File > Examples > Servo > Sweep. The sketch is six lines inside loop(): for (pos = 0; pos <= 180; pos += 1) { myservo.write(pos); delay(15); } and then the same loop counting back down. Upload it and the horn should rotate from one end stop to the other and back in about 3 seconds. If the horn jitters at the ends, your supply is sagging — add a bigger bulk cap or use a separate 5 V supply for the servo and only share grounds with the MCU.
Drive it from sensor input
Replace the for-loop with an analogRead on a 10 k potentiometer wired between 5 V and ground with the wiper to A0. Map the 0-1023 reading to 0-180 with map(value, 0, 1023, 0, 180) and feed that to myservo.write(). Turn the pot and the horn follows in real time. Swap the pot for an HC-SR04 ultrasonic sensor and the horn now points at whatever is closest. This is the core control loop of every entry-level robot — a sensor reads, a servo responds, the world reacts.