ArduinoStudio

Servo motor

Turn an SG90 servo to an exact angle, hold it there, and then move on to the next block.

A hobby servo turns its horn to an angle between 0° and 180° and holds it there. Use one for a gate, a pointer, a flag that pops up, or a little robot arm.

In ArduinoStudio each Servo block sends one Angle, then waits for its Duration before the next block runs. It works in the simulator, over USB direct in Chrome, and through the Bridge, so you don’t need the desktop app to drive a servo on a real board.

Wiring

An SG90 servo with its signal wire on Arduino pin 9, powered from 5V and GND.Arduino pin 9 to SG90 servo orange signal; Arduino 5V to SG90 servo red +5 V; Arduino GND to SG90 servo brown GND.Arduino Uno95VGNDSG90 servo3-wire cableorange signalred +5 Vbrown GND
An SG90 servo with its signal wire on Arduino pin 9, powered from 5V and GND. Wire colours: blue for digital signals, orange for power, grey dashed for ground.
FromThroughTo
Arduino pin 9—SG90 servo orange signal
Arduino 5V—SG90 servo red +5 V
Arduino GND—SG90 servo brown GND

Pin 9 is the block’s default, but any digital pin works: Firmata makes the servo pulse itself, so you don’t need a PWM (~) pin. Wire colours vary by brand. Signal is orange or yellow, power is red, ground is brown or black.

The Arduino’s 5V pin is fine for one SG90 with a light load. Bigger servos, or more than one, need their own 5 V supply. Join that supply’s GND to the Arduino GND.

Block settings

SettingNew block starts atIn the settings panel
Digital pin9Editable
Angle90Editable
Duration1000 msEditable
ModetoComing soon

Step by step

  1. Wire the servo

    Connect orange or yellow to pin 9, red to 5V and brown or black to GND. Press a horn onto the shaft so you can see it move.

  2. Add the first Servo block

    Click + on the sequence, open the Components tab, open Motion and choose Servo. Keep Digital pin 9. Set Angle to 0 and Duration to 600 ms. Mode shows “Coming soon” and stays at “to”.

  3. Add a swing to 180°

    Add a second Servo block with Angle 180 and Duration 1000 ms. Duration is how long the sequence waits before the next block. An SG90 needs about half a second for a full swing, so leave some margin.

  4. Come back to the middle

    Add a third Servo block with Angle 90 and Duration 600 ms.

  5. Repeat it (optional)

    Right-click the sequence name (long-press on a touchscreen) and tick Infinity Loop. The servo now repeats 0, 180, 90 until you press Stop.

  6. Try it in the simulator

    Press Play with no board connected. In the Virtual Board panel, pin 9 is listed as SERVO and its value steps through 0, 180 and 90 while each block counts down its Duration. The Servo block itself shows a red OFF while it runs. That is a display quirk; the Virtual Board shows what is really happening.

  7. Run it on the board

    Plug in the Uno, click USB direct in the toolbar, press Connect on the USB direct row and pick the board. Press Play. The horn swings to each angle and pauses for that block’s Duration. See Getting started if the board doesn’t show up.

Mini-project: push-button gate

Press a button and a gate opens, stays open for two seconds, then closes. Keep the servo wired as above and add a push button between pin 2 and GND, across its diagonal legs. No resistor is needed. The button guide explains why, and the idea comes from the servo gate project.

A push-button servo gate: SG90 servo on pin 9 and a button between pin 2 and GND.Arduino pin 9 to SG90 servo orange signal; Arduino 5V to SG90 servo red +5 V; Arduino GND to SG90 servo brown GND; Arduino GND to Push button leg B; Arduino pin 2 to Push button leg A.Arduino Uno95VGND2SG90 servo3-wire cableorange signalred +5 Vbrown GNDPush buttondiagonal legs, no resistorleg Bleg A
A push-button servo gate: SG90 servo on pin 9 and a button between pin 2 and GND. Wire colours: blue for digital signals, orange for power, grey dashed for ground.
FromThroughTo
Arduino pin 9—SG90 servo orange signal
Arduino 5V—SG90 servo red +5 V
Arduino GND—SG90 servo brown GND
Arduino GND—Push button leg B
Arduino pin 2—Push button leg A
  1. Start with the button

    Build one sequence and turn on Infinity Loop. Add a Button block from Inputs, with Digital pin 2.

  2. Add a Check

    Add a Check right after it and keep its defaults (If =, To On/Off, Choose on). For a button, that means “pressed”.

  3. Open, wait, close

    Add a Servo block with Angle 90 and Duration 500, a Wait of 2000 ms, then a second Servo block with Angle 0 and Duration 500.

  4. Test it

    In the simulator, click pin 2’s toggle on the Virtual Board to 0 to press the button. On the board, use USB direct and hold the button down until the gate moves. The Check reads the button once per pass, so a quick tap can fall between readings.

Use USB direct for this project rather than the Bridge. The Bridge doesn’t turn on the button’s internal pull-up yet, so the gate can open by itself.

Questions

Why does my Arduino disconnect or reset when the servo moves?

A servo draws a burst of current when it starts to move. USB power can dip far enough to reset the board, which drops the connection. Power the servo from its own 5 V supply, such as 4×AA batteries or a 5 V adapter, and connect that supply’s GND to the Arduino GND. Only the signal wire stays on the pin.

What does the Duration setting do?

It is how long the sequence waits after sending the angle, before it runs the next block. A new Servo block starts at 1000 ms. ArduinoStudio can’t read where the horn actually is, so pick a Duration long enough for the move to finish. In the simulator and over USB direct, 50 ms or less means no wait at all.

Can the servo sweep or spin continuously?

Not from the settings panel yet. Mode is marked Coming soon and stays at “to”, so the servo always moves to its Angle. A project built by the AI assistant may show sweep, cw or ccw, but only the ArduinoStudio Bridge desktop app acts on those. To swing back and forth on any connection, chain Servo blocks with different angles.

Does a servo need a PWM pin?

No. Firmata makes the servo pulse on any digital pin; 9 is just the default. On an Uno, though, once a servo is attached, pins 9 and 10 can no longer dim an LED or mix RGB colours through the Bridge. Put those parts on pins 3, 5, 6 or 11.

My servo buzzes or jitters at 0° or 180°. Is it broken?

Usually not. Small servos like the SG90 often can’t quite reach the ends of their range and strain against their stops. Try 5° and 175° instead. Jitter under load is usually a power problem; see the first question.

See the editor in action with a guided demo, no account or install needed: open the demo