Home > Arduino Sketch, Exploring Ideas, Hardware testing, Internet of Things > NodeMCU Breathing LED with Arduino IDE

NodeMCU Breathing LED with Arduino IDE

Breathing_LED

The first sketch used to test an Arduino compatible board is Blink.
After run Blink in my new NodeMCU Development Kit,
Started experiments with the WiFi connection:
-Mini servers to control leds, servos, etc..
-Send light intensity and temperature to ThingSpeak, Xively, Plotly etc..
-Serial data bridges
-etc.

The blue led in the board was used to signal the execution of a particular procedure.
Because the GPIO16 (DO) limitation to use the analogWrite() and looking to see my LED doing something else…

Inspired by the Apple’s “breathing” pattern used for the sleep indicator.
Here is the code to generate a “breathing LED”.

NOTE:
The Arduino code runs inside a multitasking application, the ESP8266 is running at the same time TCP and WiFi stacks.
The sketch can breake the multitasking execution if your code runs in a blocking section.
The delayMicroseconds() is one of those blocking functions, delay(0)  was used to prevent watchdog reset inside the PWM loops.

/*LED_Breathing.ino Arduining.com  20 AUG 2015
Using NodeMCU Development Kit V1.0
Going beyond Blink sketch to see the blue LED breathing.
A PWM modulation is made in software because GPIO16 can't
be used with analogWrite().
*/

#define LED     D0        // Led in NodeMCU at pin GPIO16 (D0).
 
#define BRIGHT    350     //max led intensity (1-500)
#define INHALE    1250    //Inhalation time in milliseconds.
#define PULSE     INHALE*1000/BRIGHT
#define REST      1000    //Rest Between Inhalations.

//----- Setup function. ------------------------
void setup() {                
  pinMode(LED, OUTPUT);   // LED pin as output.    
}

//----- Loop routine. --------------------------
void loop() {
  //ramp increasing intensity, Inhalation: 
  for (int i=1;i<BRIGHT;i++){
    digitalWrite(LED, LOW);          // turn the LED on.
    delayMicroseconds(i*10);         // wait
    digitalWrite(LED, HIGH);         // turn the LED off.
    delayMicroseconds(PULSE-i*10);   // wait
    delay(0);                        //to prevent watchdog firing.
  }
  //ramp decreasing intensity, Exhalation (half time):
  for (int i=BRIGHT-1;i>0;i--){
    digitalWrite(LED, LOW);          // turn the LED on.
    delayMicroseconds(i*10);          // wait
    digitalWrite(LED, HIGH);         // turn the LED off.
    delayMicroseconds(PULSE-i*10);  // wait
    i--;
    delay(0);                        //to prevent watchdog firing.
  }
  delay(REST);                       //take a rest...
}

Advertisements
  1. March 5, 2016 at 5:11 PM

    I’m confused as this code does not compile in the Arduino ide. ‘D0’ was not declared in this scope. Am i doing something wrong?

    • March 9, 2016 at 10:53 AM

      Check if NodeMCU board is selected in Boards Manager…
      Or change D0 to 16

      • Chris Sparks
        January 30, 2017 at 6:43 PM

        I tried the 16 thing and while it compiled, nothing is flashing.

  2. March 20, 2016 at 3:45 PM

    Can we use AnalogWrite() to generate PWM on NodeMCU pins????

  3. July 16, 2016 at 9:32 AM

    Thank you!

  4. November 20, 2016 at 12:19 PM

    Fantastic!!

  5. Chris Sparks
    January 30, 2017 at 6:38 PM

    Didn’t work for me:
    Arduino: 1.8.1 (Windows 7), Board: “Generic ESP8266 Module, 80 MHz, 40MHz, DIO, 115200, 4M (3M SPIFFS), nodemcu, Disabled, None”

    …/LED_Breathing.ino.ino: In function ‘void setup()’:

    LED_Breathing.ino:8: error: ‘D0’ was not declared in this scope

    #define LED D0 // Led in NodeMCU at pin GPIO16 (D0).

    ^

    …\LED_Breathing.ino.ino:17:11: note: in expansion of macro ‘LED’

    pinMode(LED, OUTPUT); // LED pin as output.

    ^

    …\LED_Breathing.ino.ino: In function ‘void loop()’:

    LED_Breathing.ino:8: error: ‘D0’ was not declared in this scope

    #define LED D0 // Led in NodeMCU at pin GPIO16 (D0).

    ^

    …\LED_Breathing.ino.ino:24:18: note: in expansion of macro ‘LED’

    digitalWrite(LED, LOW); // turn the LED on.

    ^

    …\LED_Breathing.ino:8: error: ‘D0’ was not declared in this scope

    #define LED D0 // Led in NodeMCU at pin GPIO16 (D0).

    ^

    …\LED_Breathing.ino\LED_Breathing.ino.ino:32:18: note: in expansion of macro ‘LED’

    digitalWrite(LED, LOW); // turn the LED on.

    ^

    exit status 1
    ‘D0’ was not declared in this scope

    ————–
    I am so close to trashing this unit as it is nearly impossible to work with.

    Chris

    • Chris Sparks
      January 31, 2017 at 12:34 AM

      I got this to work now. I found a site that had the pin constants defined.

      • Jim E.
        February 10, 2017 at 9:11 PM

        Are you going to tell us what the correct pin mapping is?

      • Chris Sparks
        February 12, 2017 at 12:41 PM

        The specific lines are below ardunaut’s reply.

    • January 31, 2017 at 10:33 AM

      Looks like the first line is missed:

      #define LED D0

      • Chris Sparks
        January 31, 2017 at 10:42 PM

        That wasn’t it. that define didn’t work for me. This is what I used:
        static const uint8_t D0 = 16;
        static const uint8_t D1 = 5; //ok
        static const uint8_t D2 = 4; //ok
        static const uint8_t D3 = 0;
        static const uint8_t D4 = 2;
        static const uint8_t D5 = 14; //ok
        static const uint8_t D6 = 12; //ok
        static const uint8_t D7 = 13;
        static const uint8_t D8 = 15;
        static const uint8_t D9 = 3;
        static const uint8_t D10 = 1;

        The “ok” are the connections I used to my 4 channel relay.

    • Chris Sparks
  6. Chris Sparks
    February 12, 2017 at 11:11 PM

    Does anyone know if this NODEMCU/Arudino IDE combo is based on the NoOS SDK or the RTS SDK? I couldn’t get anything to work with a network using the Arduino setup and I developed at the SDK level (RTOS). I got things working but I am having issues figuring out how to do GPIO on the RTOS SDK so I am thinking of porting my Ethernet working code over to Arduino.

  1. April 2, 2017 at 12:14 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: