This project is based on a project by Project Hub user Alex Glow
Make a wall-mounted dial to indicate today's weather conditions, your current location (à la Harry Potter), or power usage in your home. Or build brainwave-controlled wings. Or a robot arm to serve you Bellinis. The power is yours, with SERVOS!
Here's a quick guide to setting them up with the Arduino IoT Cloud.
For this project you will need to use an IoT Cloud enabled Arduino board, we are using an Arduino MKR WIFI 1010.
Step 1: WiringStart off by connecting the components to the breadboard. Follow the step by step guide included below.
- Place the Arduino board on the breadboard.
- Place the servo motor on the breadboard.
- Connect GND on the servo motor to GND on the Arduino board
- Connect the Signal pin on the servo motor to pin 9 on the Arduino board.
- Connect the Power pin on the servo motor to 5V on the Arduino board.
To get started with this step, you will need some very basic knowledge of the Arduino IoT Cloud service. If you have built any previous project using the service, then don’t worry, you know all that you need to know.
If you are new to the Arduino IoT Cloud, then take some time to read through the Getting started page and you will be good to go. There are also a bunch of tutorials available if needed.
In the cloud, you need to create a new Thing, and configure your device and network.
You should then add a Variable. Name it Position. It should be of the variable type Integer Number. It needs to have Read & Write permissions because we want to send values to control the servo motor.
Now, you will need to create a Dashboard to be able to send information to the Arduino board. Go to the Dashboards section, and build a new dashboard.
Inside, create a new widget: To make the creation of the control widget super easy, we will create the widget from the variable. To do this, follow these steps:
- Press “Add” to open the drop-down menu, then click into the “Things” tab
- Select the thing where you have created the Integer Number variable, then just confirm and create the widget.
- This will give you a text field which displays the current angle of the servo motor and also lets you enter new values.
- Alternatively you can choose the type of input you want to use by selecting a fitting widget from the drop-down menu: in our case we chose the slider widget.
- Make sure to select our previously created variable “position” and link it to the widget.
- Done!
This widget will, for now, not function, since we have not yet uploaded the sketch to our board, which we will do in the next step.
Step 3: CodeFor this project we need to send the value entered in our widget to the servo motor. Servo motors usually have a range of 0-180 representing the angels but in this example our servo motor reaches its max at 165º. Depending on which servo motor you are using you might have to see exactly how far it can turn.
If you hear a little unhappy groany noise at either end of the servo’s arc it means that you have overstepped the limit and should lower it until you do not hear the sound anymore.
First, inside the Code tab, we need to include the Servo library so that its functions may be used to control the Servo motor.
#include <Servo.h>
Next we define a servo from the library which is a type of object called servo followed by a name of your choosing. In our case it will be called myservo.
Servo myservo;
Inside setup() we need to attach the servo on pin 9 to the servo object
void setup() {
myservo.attach(9);
}
The loop function will actually remain empty, we don’t want to run code all the time without reason. Instead, there is a function at the bottom of the sketch that will be run every time the values in the Position variable that we added before creating our dashboard changes. The remainder of our code will be written inside this function.
Inside onPositionChange() we simply need to pass on the values from our widget, which are stored inside position variable, to the servo motor.
void onPositionChange() {
myservo.write(position);
}
ConclusionOnce you upload this sketch, you are done and able to control the servo motor using the slider in the Arduino IoT Cloud.
Enjoy!
Comments
Please log in or sign up to comment.