Arduino Project

Servo Control (IOT)

Course Introduction

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

Note

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

1.1 Install Arduino IDE(Important)
1.2 Introduction of Arduino IDE

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

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

4

Wires

Several

5

Digital Servo Motor

4

Wiring

servo_control_iot.webp__PID:5ff62b26-6dbb-4f72-a41a-fbb79aef6243

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.

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

1.png__PID:834cc8ff-a781-453b-8b58-cd94ad653988

Edit Value

2.webp__PID:39996f89-80be-4d65-874b-2ba04412e238

Follow the steps below to configure the dashboard.
1.Create New Dashboard

6.webp__PID:da293eac-eee8-44a0-a339-f878cbcacd32

2.Add Widgets

4.webp__PID:3129d7cd-f368-45ad-a9cc-15fdb362d41c

3.Link Variable

5.webp__PID:fddeba57-46dd-4f73-b1a3-2b4fa448d4686.png__PID:225f39b5-9c7b-4571-b91d-3799e4014711

4.Remember to click Done

10.webp__PID:43c5bc62-75b0-4a78-a001-93b05e14c186

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

9.webp__PID:af49573d-0890-45d0-853e-1c68405c2bc2

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.

11.webp__PID:2011ff51-bb05-4b0f-b667-93a3f1a14263

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


#include 
#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)); }