Arduino_Genuino
Published

Control Servo Motor from the IoT Cloud

This servo motor is a staple in home automation and robotics.

BeginnerFull instructions provided1 hour5,815
Control Servo Motor from the IoT Cloud

Things used in this project

Hardware components

Arduino MKR WiFi 1010
Arduino MKR WiFi 1010
Or any other IoT compatible board.
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Story

Read more

Code

Control Servo Motor from the IoT Cloud

Arduino
/*
Sketch generated by the Arduino IoT Cloud Thing "Servo"
https://create.arduino.cc/cloud/things/4a5ae1e7-a3d7-485e-b1a0-e7f522963eb3
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
int position;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
//First we need to include the Servo library so that its functions may be used to control the Servo motor.
#include <Servo.h>
Servo myservo;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
myservo.attach(9);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
}
/*
Since Position is READ_WRITE variable, onPositionChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPositionChange()  {
// Add your code here to act upon Position change
myservo.write(position);
}

Credits

Arduino_Genuino

Arduino_Genuino

91 projects • 11337 followers

Comments