Maker.io main logo

Adafruit IO Basics Analog Output

2017-03-15 | By Adafruit Industries

License: See Original Project

Courtesy of Adafruit

Guide by Todd Treece

Overview

This guide is part of a series of guides that cover the basics of using Adafruit IO. It will show you how to dim a LED from Adafruit IO using any modern web browser.

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.

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.

Keep a copy of your Adafruit IO. It is needed later

Creating the Analog Feed

Next, you will need to create a feed called Analog. If you need help getting started with creating feeds on Adafruit IO, check out the Adafruit IO Feed Basics guide.

Create a new Feed

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 1024. Make sure you have selected the Analog 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.

Block Settings

When you are finished editing the form, click Create Block to add the new block to the dashboard.

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
      • 2x jumper wires
      • 1x 560 ohm resistor
      • 1x 10mm LED

You will need to connect the following pins to the LED and 10k resistor:

      • Feather GND to LED cathode (short leg)
      • Feather Pin 5 to one leg of the 560 ohm resistor
      • LED anode (long leg) to the second leg of the 560 ohm resistor

Note: Resistors are not polarized, so the 560 ohm resistor can be connected to the circuit in either direction.

Wiring Diagram

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.3.1 of the Adafruit IO Arduino library installed before continuing.

version 2.3.1 of the Adafruit IO installed

For this example you will need to open the adafruitio_09_analog_out example in the Adafruit IO Arduino library.

open the adafruitio_09_analog_out example

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.

Set Your Username and Key

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.

Set WiFi SSID and Password

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

Comment out Default WiFi Config Lines

Next, remove the comments from both of the FONA config lines in the FONA section of config.h to enable FONA support.

Uncomment Both Fona Config Lines

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

Comment Out Default WiFi Config Lines

Next, remove the comments from both of the Ethernet config lines in the Ethernet section of config.h to enable Ethernet Wing support.

Uncomment Both Ethernet Config Lines

Next, we will look at how the example sketch works.

Code

The adafruitio_09_analog_out example uses pin 5 by default, and that can be modified by changing the LED_PIN define at the top of the sketch. This pin should correspond to a pin on your feather with PWM capability.

Copy Code
/************************ Example Starts Here *******************************/

// this should correspond to a pin with PWM capability
#define LED_PIN 5

The next chunk of code sets up an Adafruit IO Feed instance for a feed called analog.

Copy Code
// set up the 'analog' feed
AdafruitIO_Feed *analog = io.feed("analog");

In the setup function, we attach a function called handleMessage to the analog feed that will be called whenever your device receives messages for that feed.

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.

Copy Code
void setup() {

// start the serial connection
Serial.begin(115200);

// wait for serial monitor to open
while(! Serial);

// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();

// set up a message handler for the 'analog' feed.
// the handleMessage function (defined below)
// will be called whenever a message is
// received from adafruit io.
analog->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.

Copy Code
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 the analog feed gets a message.

We use the data->toInt() function to convert the incoming data to an int, and set the state of the LED_PIN to that value using analogWrite().

Copy Code
// this function is called whenever an 'analog' message
// is received from Adafruit IO. it was attached to
// the analog feed in the setup() function above.
void handleMessage(AdafruitIO_Data *data) {

// convert the data to integer
int reading = data->toInt();

Serial.print("received <- ");
Serial.println(reading);
analogWrite(LED_PIN, reading);

}

Upload the sketch to your board, and open the Arduino Serial Monitor. Your board should now connect to Adafruit IO.

Copy Code
Connecting to Adafruit IO....

Adafruit IO connected.

Change the value of the slider on your Adafruit IO dashboard, and you should see something resembling the following in the Arduino Serial Monitor.

Copy Code
received <- 940
received <- 290
received <- 230
received <- 110
received <- 0
received <- 90
received <- 320
received <- 630
received <- 840
received <- 1020

You should also see your LED change brightness depending on the value you send.

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.