Arduino UNO Q - Servo Motor

This tutorial shows you how to control a servo motor using an Arduino UNO Q. Specifically, you will learn:

Arduino UNO Q Servo Motor

Hardware Preparation

1×Arduino UNO Q
1×USB Cable for Arduino Uno Q
1×Servo Motor
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno
1×Recommended: Sensors/Servo Expansion Shield for Arduino Uno
1×Recommended: Breadboard Shield for Arduino Uno
1×Recommended: Enclosure for Arduino Uno
1×Recommended: Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Buy Note: For controlling multiple servo motors, use the PCA9685 16 Channel PWM Servo Driver Module to save MCU pins and simplify wiring.

Overview of Servo Motor

A servo motor is a component that can turn its handle, usually from 0 degrees to 180 degrees. It is used to control the angle of an object.

Pinout

This example uses a servo motor with three pins:

  • VCC pin: Connect the red wire to VCC (5 volts).
  • GND pin: Connect the black or brown wire to GND (0 volts).
  • Signal pin: Connect the yellow or orange wire to receive the PWM control signal from an Arduino UNO Q pin.
Servo Motor Pinout

Wiring Diagram

Sometimes you may see online wiring diagrams showing the servo VCC connected directly to the 5V pin on the Arduino. It is best to avoid this method because it could harm the Arduino UNO Q board.

The wiring diagram between Arduino UNO Q Servo Motor

This image is created using Fritzing. Click to enlarge image

To protect your Arduino UNO Q board, it is best to use an external power supply for the servo motor. The wiring diagram below shows how to connect the servo motor to an external power source.

The wiring diagram between Arduino UNO Q servo motor external power supply

This image is created using Fritzing. Click to enlarge image

Make sure to connect the GND of the external power supply to the GND of the Arduino UNO Q board. This step is very important for it to work correctly.

How To Program For Servo Motor

  • Include the library:
#include <Servo.h>
  • Create a Servo object:
Servo servo;

If you manage multiple servo motors, simply declare additional Servo objects.

Servo servo1; Servo servo2;
  • Connect the control pin of the Arduino UNO Q to the signal pin of the servo motor, such as pin 9.
servo.attach(9);
  • Finally, turn the servo motor to the angle you need, like 90 degrees.
servo.write(90);

MCU Code

The Arduino UNO Q has two processors: the STM32 MCU (handles real-time hardware control) and the Qualcomm MPU (runs Debian Linux). In this section, only the STM32 MCU is programmed — the Linux side stays idle. A later section will show how both processors work together.

The code sweeps the servo motor smoothly from 0° to 180° and back, repeating continuously:

/* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-servo-motor */ #include <Servo.h> Servo servo; void setup() { servo.attach(9); // Connect the servo signal wire to pin 9 servo.write(0); // Move to 0 degrees at startup } void loop() { // Sweep from 0 to 180 degrees for (int pos = 0; pos <= 180; pos += 1) { servo.write(pos); delay(10); } // Sweep from 180 back to 0 degrees for (int pos = 180; pos >= 0; pos -= 1) { servo.write(pos); delay(10); } }

Detailed Instructions

  • First time with Arduino UNO Q? Follow the Getting Started with Arduino UNO Q tutorial to get your development environment ready before proceeding.
  • Wire the components: Connect the servo signal wire to pin 9, VCC to 5V external supply, and GND to both external supply and Arduino UNO Q GND.
  • Connect: Plug the Arduino UNO Q into your computer with a USB-C cable.
  • Open Arduino App Lab: Launch Arduino App Lab and wait until it detects your Arduino UNO Q.
  • Create a new App: Click the Create New App button.
Create New App in Arduino App Lab on Arduino UNO Q
  • Give the App a name, for example: DIYables_ServoMotor
  • Click Create to confirm.
  • You will see a set of folders and files generated inside your new App.
Arduino App Lab App folders and files on Arduino UNO Q
  • Find the sketch/sketch.ino file — this is where you will paste the MCU sketch.
  • Paste the sketch: Copy the MCU code above and paste it into the sketch file. Keep other files as default.
    • Install the library: Click the Add sketch library button (the open book icon with a + sign) in the left sidebar.
    Add sketch library in Arduino App Lab on Arduino UNO Q
    • Search for Servo created by Michael Margolis, Arduino and click the Install button.
    My Apps / DIYables Apps
    Run
    Bricks
    No bricks added...
    Sketch Libraries
    No sketch libra...
    Files
    python
    sketch
    .gitignore
    README.md
    app.yaml
    sketch.ino
    Add sketch library
    Servo Michael Margolis, Arduino

    This library can control a great number of servos.
    It makes careful use of timers: the library can control 12 servos using only 1 timer.
    On the Arduino Due you can control up to 60 servos.

    1.2.1
    Install
    More Info
    • Search for Arduino_RouterBridge created by Arduino and click the Install button.
    My Apps / DIYables Apps
    Run
    Bricks
    No bricks added...
    Sketch Libraries
    No sketch libra...
    Files
    python
    sketch
    .gitignore
    README.md
    app.yaml
    sketch.ino
    Add sketch library
    Arduino_RouterBridge Arduino

    This library provides a simple RPC bridge for Arduino UNO Q boards, allowing communication between the board and other devices using MsgPack serialization.

    0.4.1
    Install
    More Info
    • Upload: Click the Run button in Arduino App Lab to compile and upload to the STM32.
    Click Run button in Arduino App Lab on Arduino UNO Q
    • Watch: The servo motor turns slowly from 0° to 180°, then slowly turns back from 180° to 0°, repeating continuously.

    How to Control Speed of Servo Motor

    Using the map() and millis() functions allows you to smoothly adjust the speed of a servo motor while letting other code run without interruptions.

    #include <Servo.h> Servo servo; #define MOVING_TIME 3000 // Duration (ms) for the servo to move from startAngle to stopAngle unsigned long moveStartTime; int startAngle = 30; int stopAngle = 90; void setup() { servo.attach(9); moveStartTime = millis(); } void loop() { unsigned long progress = millis() - moveStartTime; if (progress <= MOVING_TIME) { long angle = map(progress, 0, MOVING_TIME, startAngle, stopAngle); servo.write(angle); } }

    Linux + MCU Bridge Programming

    The Arduino UNO Q has two processors that work together: the MPU (Qualcomm, runs Debian Linux) and the MCU (STM32, runs Zephyr OS with your Arduino sketch). They communicate using RPC via the Arduino_RouterBridge library — never via raw serial ports.

    • The servo is connected to the MCU (STM32) — the signal wire is wired to pin 9. The MCU drives the PWM servo signal.
    • The MPU cannot control the servo directly — it must call a function on the MCU via Bridge.call() to move the servo to a specific position.
    • The MPU has Wi-Fi — because the MPU runs full Debian Linux with Wi-Fi, it can receive Telegram commands and remotely position the servo.
    • Communication: Bridge.call() on the Linux side invokes Bridge.provide_safe() functions on the MCU side (since servo.write() is a hardware API)
    • ⚠️ Reserved: /dev/ttyHS1 (Linux) and Serial1 (MCU) are used by the Arduino Router — never open them directly

    In short: MPU sends angle command → MCU drives servo → servo moves to position.

    MCU sketch — servo motor control with Bridge and Monitor output:

    /* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-servo-motor */ #include "Arduino_RouterBridge.h" #include <Servo.h> Servo servo; void servo_left() { servo.write(0); Monitor.println("Servo: 0 degrees (left)"); } void servo_center() { servo.write(90); Monitor.println("Servo: 90 degrees (center)"); } void servo_right() { servo.write(180); Monitor.println("Servo: 180 degrees (right)"); } void setup() { Bridge.begin(); Monitor.begin(); servo.attach(9); servo.write(90); // Start at center position Bridge.provide_safe("left", servo_left); Bridge.provide_safe("center", servo_center); Bridge.provide_safe("right", servo_right); Monitor.println("Servo Motor Bridge ready"); } void loop() {}

    Python script (Arduino App Lab) — control servo from Linux:

    /* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-servo-motor */ from arduino.app_utils import * import time def loop(): print("Servo at 0 degrees (left)") Bridge.call("left") time.sleep(2) print("Servo at 90 degrees (center)") Bridge.call("center") time.sleep(2) print("Servo at 180 degrees (right)") Bridge.call("right") time.sleep(2) print("Servo at 90 degrees (center)") Bridge.call("center") time.sleep(2) App.run(user_loop=loop)
    • Note: Make sure Bridge.begin() is called in the MCU sketch and the sketch is uploaded before running the Python script on the Linux side.
    • ⚠️ Warning: Never directly open /dev/ttyHS1 (on Linux) or use Serial1 (on MCU) in your code — these are reserved by the Arduino Router and accessing them will break the Bridge.

    Detailed Instructions

    • Upload the MCU sketch: Open Arduino App Lab, create a new App, paste the Bridge MCU sketch into sketch/sketch.ino, install Servo and Arduino_RouterBridge libraries, and click Run.
    • Add the Python script: Paste the Python code above into the Python tab of the same App.
    • Run the App: Click Run — Python cycles the servo through left, center, right, center positions automatically.
    • Check the console: Open the Console tab → MCU Monitor subtab to see servo position messages.

    App Lab Console Output

    DIYables_Apps
    Stop
    sketch.ino
    1#include "Arduino_RouterBridge.h"
    Serial Monitor
    Python
    Message (Enter to send a message to "Newbiely" on usb(2820070321))
    New Line
    9600 baud
    Servo Motor Bridge ready Servo: 0 degrees (left) Servo: 90 degrees (center) Servo: 180 degrees (right) Servo: 90 degrees (center)

    Telegram Integration

    Control the servo motor remotely from anywhere via Telegram.

    If you do not have a Telegram bot yet, see How to Create a Telegram Bot to get your bot token before continuing.

    MCU sketch: Keep the same MCU sketch from the previous Bridge section — no changes needed. Make sure it is already uploaded and running on the STM32 before proceeding.

    Python script (Arduino App Lab) — Telegram bot for servo motor control:

    /* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-servo-motor */ from arduino.app_utils import * import requests import time BOT_TOKEN = "YOUR_BOT_TOKEN" API_URL = f"https://api.telegram.org/bot{BOT_TOKEN}" last_update_id = 0 def send_message(chat_id, text): requests.post(f"{API_URL}/sendMessage", json={"chat_id": chat_id, "text": text}) def get_updates(): global last_update_id resp = requests.get(f"{API_URL}/getUpdates", params={"offset": last_update_id + 1, "timeout": 5}) return resp.json().get("result", []) def loop(): global last_update_id updates = get_updates() for update in updates: last_update_id = update["update_id"] msg = update.get("message", {}) chat_id = msg.get("chat", {}).get("id") text = msg.get("text", "").strip() if text == "/left": Bridge.call("left") send_message(chat_id, "Servo moved to 0 degrees (left).") elif text == "/center": Bridge.call("center") send_message(chat_id, "Servo moved to 90 degrees (center).") elif text == "/right": Bridge.call("right") send_message(chat_id, "Servo moved to 180 degrees (right).") else: send_message(chat_id, "Commands:\n/left — move to 0°\n/center — move to 90°\n/right — move to 180°") time.sleep(1) App.run(user_loop=loop)
    • Note: Replace YOUR_BOT_TOKEN with the token obtained from @BotFather on Telegram.
    • Send /left to move the servo to 0°.
    • Send /center to move the servo to 90°.
    • Send /right to move the servo to 180°.

    Detailed Instructions

    • Upload the MCU sketch: Use the Bridge MCU sketch from the previous section (upload it first if not already done).
    • Paste the Telegram script: Copy the Python code above into the Python tab of your App in Arduino App Lab.
    • Set your token: Replace YOUR_BOT_TOKEN in the script with your actual bot token.
    • Run the App: Click Run — the bot starts listening for Telegram messages.
    • Test it: Send /left — the servo moves to 0°. Send /center — it goes to 90°. Send /right — it goes to 180°.

    App Lab Console Output

    DIYables_Apps
    Stop
    sketch.ino
    1#include "Arduino_RouterBridge.h"
    Serial Monitor
    Python
    [2026-04-29 12:00:01] Telegram: /left [2026-04-29 12:00:01] Servo moved to 0 degrees (left). [2026-04-29 12:02:10] Telegram: /center [2026-04-29 12:02:10] Servo moved to 90 degrees (center). [2026-04-29 12:04:05] Telegram: /right [2026-04-29 12:04:05] Servo moved to 180 degrees (right).
    Telegram
    Telegram 12:45
    Welcome to Telegram!
    ArduinoBot 10:19
    Chatting with Arduino...
    telegram-botfather
    BotFather Yesterday
    Your bot has been created.

    ArduinoBot

    bot
    Today
    /left
    10:15 AM ✓✓
    Servo moved to 0 degrees (left).
    10:16 AM
    /center
    10:17 AM ✓✓
    Servo moved to 90 degrees (center).
    10:18 AM
    /right
    10:19 AM ✓✓
    Servo moved to 180 degrees (right).
    10:20 AM

    OpenClaw Integration

    OpenClaw integration for Arduino UNO Q servo motor control is coming soon.

    • Coming Soon: OpenClaw support for controlling a servo motor on Arduino UNO Q will be covered in a future update.

    Application/Project Ideas

    • Remote camera pan: Mount a camera on a servo and aim it with Telegram commands
    • Automated valve: Use a servo to open or close a water valve triggered by a Telegram message
    • Door lock mechanism: Rotate a servo to latch or unlatch a door lock remotely
    • Solar tracker: Use a light sensor reading from the MPU to keep a solar panel aimed at the sun
    • Robot arm joint: Build a multi-servo robot arm and control each joint via Telegram

    Challenge Yourself

    • Easy: Add a /status Telegram command that reports the current servo angle (left, center, or right)
    • Medium: Implement smooth movement between positions by stepping degree-by-degree in the Bridge callback
    • Advanced: Add multiple preset positions (/pos1, /pos2, etc.) that can be configured via Telegram commands

    ※ OUR MESSAGES

    • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
    • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!