Arduino 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.

Download: file
/************************ 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.

Download: file
// 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.

Download: file
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.

Download: file
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().

Download: file
// 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.

Download: file
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.

Download: file
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.

This guide was first published on Feb 16, 2017. It was last updated on Feb 16, 2017. This page (Arduino Code) was last updated on Dec 05, 2019.