2018年5月4日 星期五

NodeMCU IO Basics: PWM

NodeMCU IO Basics: PWM

Contents [show]

PWM on ESP-12E NodeMCU with the Arduino IDE

General Purpose IO (GPIO) pins on the ESP-12E NodeMCU allow for Pulse Width Modulated (PWM) outputs.   Simply put, PWM outputs allow us to set the output pins to be on for a certain percentage of time in a given time period.   This tutorial explains pulse with modulation in more detail and puts into practice with a simple project that varies the intensity of an LED connected to a GPIO output.
This tutorial focuses on programming the NodeMCU using the Arduino IDE.  If you haven’t already prepared your Arduino and your NodeMCU, you can read how to do so HERE.

Getting a NodeMCU

You will need a NodeMCU Module. The module can be found from any of the following vendors:
Amazon    eBay    Bang Good    AliExpress

NodeMCU analogWrite

To invoke a PWM output you use the analogWrite command.  The syntax looks like this:
analogWrite( PinNumber, TimeOn);
Where:
  • PinNumber is one of the digital output pins on the NodeMCU.   The graphic serves to show you these output pins and provides the number you will use from the Arduino IDE.   Click on it to enlarge
  • TimeOn is the amount of time the output is high within a time period.   The value can range from 0 (off all the time) to 1023 (on all the time).   Thus the number 255 would mean that the output is high about 25 percent of the time.   The picture below illustrates how the value affects the output within a period of time.
If you’re familiar with an Arduino or are porting programs over from an Arduino,  it is important to remember that TimeOn values range from 0 to 255.  In other words,  the NodeMCU provides 4 times the resolution.

Arduino IDE NodeMCU PWM Tutorial

In this tutorial we will use pulse width modulation via the analog write command to vary the intensity of an LED connected to one of the outputs.

Connect the ESP-12E NodeMCU PWM Circuit

Connect the circuit as shown.   There are other power supply options.  You can read about them HERE.

Copy, Paste and Upload the Sketch

The sketch below will vary the intensity of the attached LED.
int LedPin = 16;


void setup() { 
  pinMode(LedPin, OUTPUT);
}

void loop() {

  // Turn LED OFF
  
  analogWrite(LedPin, 0);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second

  // Turn LED on: 25 Percent Intensity

  analogWrite(LedPin, 255);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second

  // Turn LED on: 25 Percent Intensity
  
  analogWrite(LedPin, 512);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second

  // Turn LED on: 75 Percent Intensity

  analogWrite(LedPin, 767);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second

   // Turn LED on: 100 Percent Intensity
  
  analogWrite(LedPin, 1023);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second  
}

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...