ESP32: Timer PWM

update: 06.07.2019

Pulse Width Modulation (PWM)

หมายเหตุ: คำสั่ง PWM ใน ESP32 ไม่เหมือนกับ Arduino UNO!!!

PWM Analog Concept

01_TimerPWM_00

PWM Analog Concept: at 25% duty cycle.

01_TimerPWM_01

PWM Analog Concept: at 70% duty cycle.

01_TimerPWM_02

PWM Digital Concept

01_TimerPWM_03.jpg

ESP32: digital PWM

01_TimerPWM_04


PWM: Project 1. Simple

Circuit

01_TimerPWM_05

Program

/* ESP32_Timer_PWM_01Simple.ino
* Study PWM Simple one!
*/

byte LED0 = 12;
int freq = 1000;
int PWMChannel_0 = 0; //0…15, 16 channels
int resolution = 16; //1…16, n =16, 2^n = 65,536
int dutyCycle0;

void setup() {
ledcSetup(PWMChannel_0, freq, resolution);
ledcAttachPin(LED0, PWMChannel_0);
}

void loop() {
dutyCycle0 = 49152;
//100% = 65,536, 32,768(50%), 16,384(25%), 8,192(12.5%)
//49,152(75%)
ledcWrite(PWMChannel_0, dutyCycle0);
delay(1000);
}

Result

Press Reset and see LED light and Digital oscilloscope screen by testing to change dutyCycle0 variable.

dutyCycle0 = 49152;

tek00000

dutyCycle0 = 16384;

tek00001

dutyCycle0 = 8192;

tek00002

dutyCycle0 = 49152;

tek00003

Description

01_TimerPWM_06


Exercise 1. จงต่อวงจรให้ LED1 ต่อกับขา GPIO33 และสร้างสัญญาณ PWM 80% ที่มีความถี่ 500 Hz โดยมี resolution n = 12 bits ซึ่งแสดงสัญญาณดัง Oscilloscope ด้านล่าง

tek00004

Answer. ESP32_Timer_PWM_03Exercise1.ino

Exercise 2. จงต่อวงจรดังรูปด้านล่าง และให้ GPIO00 สร้างสัญญาณ PWM, f = 1kHz, d = 20% และ GPIO02 สร้างสัญญาณ PWM, f = 1kHz, d = 80% ซึ่งแสดงสัญญาณดัง Oscilloscope ด้านล่าง CH1 = GPIO00, CH1 = GPIO02.

01_TimerPWM_07

tek00005

Answer. ESP32_Timer_PWM_03Exercise2.ino


PWM: Project 2. ADC adjust PWM

Circuit

01_TimerPWM_08

01_TimerPWM_09

Program

/* ESP32_Timer_PWM_04_Project2.ino
* ADC adjust PWM
*/

byte LED0 = 15;
byte ADCchannel_10 = A10;
int commandPWM;
int freq = 1000;
int PWMChannel_0 = 0; //0…15, 16 channels
int resolution = 16; //1…16, n =16, 2^n = 65,536
int dutyCycle0;

void setup() {
Serial.begin(115200);
Serial.println(“…START…”);
Serial.println(“”);
ledcSetup(PWMChannel_0, freq, resolution);
ledcAttachPin(LED0, PWMChannel_0);
}

void loop() {
commandPWM = analogRead(ADCchannel_10);
dutyCycle0 = map(commandPWM,0,4095,0,65535);
//map(value, fromLow, fromHigh, toLow, toHigh)
Serial.print(“Vq(12 bits) = “);
Serial.print(commandPWM);
Serial.print(“: dutyCycle(16 bits) = “);
Serial.println(dutyCycle0);
Serial.println(“”);
ledcWrite(PWMChannel_0, dutyCycle0);
delay(2000);
}

Result

Press Reset and see LED light and Digital oscilloscope screen by testing to adjust resistor VA.

Phatiphat

 

Oscilloscope: CH1 = VA, CH2 = GPIO15

VA = 0.580 V

tek00006

VA = 2.943 V

tek00007

Description

ทำความเข้าใจกับโปรแกรม….