Adafruit IO Basics Servo
2017-03-14 | By Adafruit Industries
License: See Original Project Adafruit Feather
Courtesy of Adafruit
Guide by Todd Treece
This guide is part of a series of guides that cover the basics of using Adafruit IO. It will show you how to wirelessly control a servo from Adafruit IO.
If you haven't worked your way through the Adafruit IO feed and dashboard basics guides, you should do that before continuing with this guide so you have a basic understanding of Adafruit IO.
You should go through the setup guides associated with your selected set of hardware, and make sure you have internet connectivity with the device before continuing. The following links will take you to the guides for your selected platform.
If you have went through all of the prerequisites for your selected hardware, you are now ready to move on to the Adafruit IO setup steps that are common between all of the hardware choices for this project. Let's get started!
Adafruit IO Setup
The first thing you will need to do is to login to Adafruit IO and visit the Settings page.
Click the VIEW AIO KEY button to retrieve your key.
A window will pop up with your Adafruit IO. Keep a copy of this in a safe place. We'll need it later.
Creating the Servo Feed
Next, you will need to create a feed called Servo. If you need help getting started with creating feeds on Adafruit IO, check out the Adafruit IO Feed Basics guide.
Adding the Slider Block
Next, add a new Slider Block to a new or existing dashboard. Name the block whatever you would like, and set min value to 0 and max value to 180. Make sure you have selected the Servo feed as the data source for the slider.
If you need help getting started with Dashboards on Adafruit IO, check out the Adafruit IO Dashboard Basics guide.
When you are finished editing the form, click Create Block to add the new block to the dashboard.
Next, we will look at wiring the circuit.
Wiring
You will need the following parts for this tutorial:
- 1x Adafruit IO compatible Feather
- 3x jumper wires
- 1x micro servo
You will need to connect the following pins to the servo using the three jumper wires:
- Feather GND to the brown or black servo wire
- Feather USB to the red servo wire
- Feather Pin 2 to the yellow servo wire
Note: The servo requires 5V power, so you will need to power the Feather via USB.
Next, let's look at the example sketch we will be using.
Arduino Setup
You should go through the setup guides associated with your selected set of hardware, and make sure you have internet connectivity with the device before continuing. The following links will take you to the guides for your selected platform.
You will need to make sure you have at least version 2.4.3 of the Adafruit IO Arduino library installed before continuing.
For this example, you will need to open the adafruitio_16_servo example in the Adafruit IO Arduino library.
Next, we will look at the network configuration options in the sketch.
Network Config
To configure the network settings, click on the config.h tab in the sketch. You will need to set your Adafruit IO username in the IO_USERNAME define, and your Adafruit IO key in the IO_KEY define.
WiFi Config
WiFi is enabled by default in config.h so if you are using one of the supported WiFi boards, you will only need to modify the WIFI_SSID and WIFI_PASS options in the config.h tab.
FONA Config
If you wish to use the FONA 32u4 Feather to connect to Adafruit IO, you will need to first comment out the WiFi support in config.h
Next, remove the comments from both of the FONA config lines in the FONA section of config.h to enable FONA support.
Ethernet Config
If you wish to use the Ethernet Wing to connect to Adafruit IO, you will need to first comment out the WiFi support in config.h
Next, remove the comments from both of the Ethernet config lines in the Ethernet section of config.h to enable Ethernet Wing support.
Next, we will look at how the example sketch works.
Code
The adafruitio_16_servo example uses pin 2 by default, and that can be modified by changing the SERVO_PIN define at the top of the sketch.
// pin used to control the servo
#define SERVO_PIN 2
The next chunk of code sets up an instance of the Servo class, and also an instance of the Adafruit IO Feed class for a feed called servo.
// create an instance of the servo class
Servo servo;
// set up the 'servo' feed
AdafruitIO_Feed *servo_feed = io.feed("servo");
In the setup function, we attach a function called handleMessage to the servo_feed, which will be called whenever your device receives messages for that feed. We also tell the servo class which pin we are using with the servo.attach() method.
The code will wait until you have a valid connection to Adafruit IO before continuing with the sketch. If you have any issues connecting, check config.h for any typos in your username or key.
void setup() {
// start the serial connection
Serial.begin(115200);
// wait for serial monitor to open
while(! Serial);
// tell the servo class which pin we are using
servo.attach(SERVO_PIN);
// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();
// set up a message handler for the 'servo' feed.
// the handleMessage function (defined below)
// will be called whenever a message is
// received from adafruit io.
servo_feed->onMessage(handleMessage);
// wait for a connection
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}
// we are connected
Serial.println();
Serial.println(io.statusText());
}
Next, we have the main loop() function. The first line of the loop function calls io.run(); this line will need to be present at the top of your loop in every sketch. It helps keep your device connected to Adafruit IO, and processes any incoming data.
void loop() {
// io.run(); is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
// io.adafruit.com, and processes any incoming data.
io.run();
}
The final chunk of code is the handleMessage function. This is the function that is called whenever servo_feed gets a message.
We use the data->toInt() function to convert the incoming data to an int, and set the angle of the servo to that value using servo->write(). We also check to make sure that the incoming angle value is not less than 0, or greater than 180.
// this function is called whenever a 'servo' message
// is received from Adafruit IO. it was attached to
// the servo feed in the setup() function above.
void handleMessage(AdafruitIO_Data *data) {
// convert the data to integer
int angle = data->toInt();
// make sure we don't exceed the limit
// of the servo. the range is from 0
// to 180.
if(angle < 0)
angle = 0;
else if(angle > 180)
angle = 180;
servo.write(angle);
}
If you would like your servo to switch directions, you can subtract the angle from 180, and write the result to the servo.
servo.write(180 - angle);
Upload the sketch to your board, and open the Arduino Serial Monitor. Your board should now connect to Adafruit IO.
Connecting to Adafruit IO....
Adafruit IO connected.
Change the slider value on your Adafruit IO dashboard, and you should see the servo change position depending on the value you send.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum