Skip to main content

Control Led Brightness using Adafruit IO Platrorm

Tim is currently doing research in Internet of Things (IoT). His desire to spread the concept, ideas, and experience of IoT.

Adafruit IO platform provides MQTT services and a dashboard to create stunning Internet of Things (IoT) and M2M applications. This platform supports variety of hardware and is very flexible and easy to use solution for building connected applications.

Components Required

  • NodeMCU Development Board X 1
  • LED X 1
  • Resistor (220 Ohm) X 1
  • Male-To-Male Jumper Wires (Optional) X 2
  • Breadboard (Optional) X 1


Connect the LED to NodeMCU pin D6 as shown in following diagram. D6 is the PWM pin which is require to control the LED.


Environment Setup

Now open your Arduino IDE and Install libraries as per instructions given in following links:

Arduino Code

After all the required libraries are installed, type or copy paste following code and compile and upload it to NodeMCU.

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

/************************* WiFi Access Point *********************************/
#define WLAN_SSID       "YOUR_WIFI_SSID"

/************************* Setup *********************************/

#define AIO_SERVER      ""
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_KEY         "YOUR_KEY"
#define LEDPIN D6
/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.

/****************************** Feeds ***************************************/
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>

// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe brightness = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/brightness");

/*************************** Sketch Code ************************************/

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

void setup() {
  Serial.println(F("Adafruit MQTT demo"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");

  while (WiFi.status() != WL_CONNECTED) {

  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());

  // Setup MQTT subscription for onoff feed.

uint32_t x=0;

void loop() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.

  // this is our 'wait for incoming subscription packets' busy subloop
  // try to spend your time here

  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(5000))) {
    if (subscription == &brightness) {
      Serial.print(F("Got: "));
      String brightnessValue = (char *)brightness.lastread;
      analogWrite(LEDPIN, brightnessValue.toInt());


// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println("Retrying MQTT connection in 5 seconds...");
       delay(5000);  // wait 5 seconds
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
  Serial.println("MQTT Connected!");

In the above code, please update your WiFi SSID and password on lines 6 and 7. You will also need Adafruit IO username and API key in the above code lines 13 and 14. Please follow the instructions below to retrieve your Adafruit details.

Go to Adafruit IO Platform and create your account. After you have created you account, sign in to your account.


After login get the Username and API Key for the Arduino code above. To get this click on AOI Key link on top right as shown in following image and copy your details to your Arduino code.


After you have compiled and uploaded the Arduino code on NodeMCU, access the AIO account again and create a dashboard which will contain the slider control to control LED brightness. To create dashboard, click on Actions->Create New Dashboard and name your dashboard as show below.


Your screen will now look as following.

Scroll to Continue

Now click on your dashboard link 'AIO_Controller' on the AIO Platform. You will see screen as in following figure.

Click on '+' button to create a new block.


Clicking the '+' button, following screen will be shown.

Click on slider control.


The next dialog box 'Choose Feed' will be shown. Here you can choose previously created feeds for your control or you can create new feed. In this example we are creating new feed since we do not have any feed right now. I have created a feed named 'brightness' as shown in following figure.


Type the name of feed 'brightness' and click create button. You will see following screen. Please note that this feed should be same as I have described here because I am also using it in Arduino code line 30. This is the MQTT topic to which your IoT Node subscribe therefore it should be same. If you wish another name, then please change at both places.


Choose the feed 'brightness' and click next button.


Clicking the next button you will see 'Block settings' dialog box where you have to specify certain settings. Do it as shown in following figure.


And then click create block button.


Your interface is now created as shown in following figure.

You can use this slider control to control the LED attached to your NodeMCU board remotely using the MQTT protocol.

Related Articles