Servo Control (IOT)

Note

🌟 Welcome to the SunFounder Facebook Community! Whether you’re into Raspberry Pi, Arduino, or ESP32, you’ll find inspiration, help ideas here.

  • ✅ Be the first to get free learning resources.

  • ✅ Stay updated on new products & exclusive giveaways.

  • ✅ Share your creations and get real feedback.

  • 👉 Need faster updates or support? Click [here] join our Facebook community

  • 👉 Or join our WhatsApp group: Click [here]

Kit purchase

Looking for parts? Check out our all-in-one kits below — packed with components, beginner-friendly guides, and tons of fun.

../_images/ultimate_sensor_kit.png

Name

Includes Arduino board

PURCHASE LINK

Elite Explorer Kit

Arduino Uno R4 WiFi

BUY

3 in 1 Ultimate Starter Kit

Arduino Uno R4 Minima

BUY

Course Introduction

This Arduino project uses Arduino IoT Remote to control a servo motor.

Required Components

In this project, we need the following components:

SN

COMPONENT INTRODUCTION

QUANTITY

PURCHASE LINK

1

Arduino UNO R4 WIFI

1

BUY

2

USB Type-C cable

1

3

Breadboard

1

BUY

4

Wires

Several

BUY

5

Digital Servo Motor

4

BUY

Wiring

../_images/servo_control_iot.png

Common Connections:

  • Digital Servo Motor A

    • Connect to breadboard’s positive power bus.

    • Connect to breadboard’s negative power bus.

    • Connect to 3 on the Arduino.

  • Digital Servo Motor B

    • Connect to breadboard’s positive power bus.

    • Connect to breadboard’s negative power bus.

    • Connect to 5 on the Arduino.

  • Digital Servo Motor C

    • Connect to breadboard’s positive power bus.

    • Connect to breadboard’s negative power bus.

    • Connect to 6 on the Arduino.

  • Digital Servo Motor D

    • Connect to breadboard’s positive power bus.

    • Connect to breadboard’s negative power bus.

    • Connect to 9 on the Arduino.

Note

If this is your first time working with an Arduino IOT project, we recommend downloading and reviewing the basic materials first.

Please follow the steps in the tutorial below to complete the binding and setup of Arduino Cloud and the Arduino WiFi board.

Create a New IoT Project

After configuring the Arduino Cloud and the Arduino WiFi board, follow the steps below to complete the Arduino Cloud project setup

../_images/servo_step1.png

Edit Value

Follow the steps below to configure the dashboard.

  1. Create New Dashboard

../_images/dashboard_step.png
  1. Add Widgets

../_images/servo_dashboard1.png
  1. Link Variable

../_images/servo_variable1.png ../_images/servo_variable2.png
  1. Remember to click Done

../_images/done.png

After completing the above configuration, return to the Things page and open the sketch.

../_images/done2.png

When you have completed the configuration of the Things and Dashboard, as well as the connection and network setup of the Arduino WiFi board, the thingProperties.h and Sketch Secrets files will be generated automatically. If Sketch Secrets is not generated, please manually enter the connected SSID and OPTIONAL_PASS

Copy this code into Arduino Cloud.

../_images/servo_code.png

Don’t forget to select the board(Arduino UNO R4 WIFI) and the correct port before clicking the Upload button.

#include <Servo.h>
#include "thingProperties.h"

// ========== Servo pins (modifiable if needed) ==========
const uint8_t SERVO_PINS[4] = {3, 5, 6, 9}; // D3, D5, D6, D9
// Servo pulse width (microseconds), adjustable according to your servo, e.g., 500~2400us
const int SERVOMIN_US = 500;
const int SERVOMAX_US = 2400;
// ======================================================

Servo servos[4];

void setup() {
  Serial.begin(115200);
  delay(500);

  // Initialize cloud properties and connect to IoT Cloud
  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

  // Attach 4 servos and set default angle
  for (int i = 0; i < 4; i++) {
    servos[i].attach(SERVO_PINS[i], SERVOMIN_US, SERVOMAX_US);
    servos[i].write(90);
  }

  // (Optional) Sync current device angle to the cloud
  angle0 = 90; angle1 = 90; angle2 = 90; angle3 = 90;
}

void loop() {
  ArduinoCloud.update();   // Must be placed inside loop
}

// ====== Cloud property change callbacks (triggered when slider changes) ======
void onAngle0Change() { servos[0].write(constrain(angle0, 0, 180)); }
void onAngle1Change() { servos[1].write(constrain(angle1, 0, 180)); }
void onAngle2Change() { servos[2].write(constrain(angle2, 0, 180)); }
void onAngle3Change() { servos[3].write(constrain(angle3, 0, 180)); }