start:esp32:pwm
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
start:esp32:pwm [2022/12/20 17:21] – [Schéma] gerardadmin | start:esp32:pwm [2023/01/27 16:08] (Version actuelle) – modification externe 127.0.0.1 | ||
---|---|---|---|
Ligne 90: | Ligne 90: | ||
Pour le reste des paramètres, | Pour le reste des paramètres, | ||
+ | |||
+ | <code c pwmesp32led.ino> | ||
+ | const int LEDPin = 16; /* GPIO16 */ | ||
+ | |||
+ | int dutyCycle; | ||
+ | /* Setting PWM Properties */ | ||
+ | const int PWMFreq = 5000; /* 5 KHz */ | ||
+ | const int PWMChannel = 0; | ||
+ | const int PWMResolution = 10; | ||
+ | const int MAX_DUTY_CYCLE = (int)(pow(2, | ||
+ | void setup() | ||
+ | { | ||
+ | ledcSetup(PWMChannel, | ||
+ | /* Attach the LED PWM Channel to the GPIO Pin */ | ||
+ | ledcAttachPin(LEDPin, | ||
+ | } | ||
+ | void loop() | ||
+ | { | ||
+ | /* Increasing the LED brightness with PWM */ | ||
+ | for(dutyCycle = 0; dutyCycle <= MAX_DUTY_CYCLE; | ||
+ | { | ||
+ | ledcWrite(PWMChannel, | ||
+ | delay(3); | ||
+ | // | ||
+ | } | ||
+ | /* Decreasing the LED brightness with PWM */ | ||
+ | for(dutyCycle = MAX_DUTY_CYCLE; | ||
+ | { | ||
+ | ledcWrite(PWMChannel, | ||
+ | delay(3); | ||
+ | // | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | < | ||
+ | Vous pouvez connecter plusieurs broches GPIO au même canal LEDC PWM. Si vous le faites, toutes les broches GPIO partageront les propriétés du canal (résolution et fréquence). | ||
+ | </ | ||
+ | |||
+ | =====ESP32 PWM avec ADC===== | ||
+ | |||
+ | L'une des caractéristiques importantes de PWM dans ESP32 est que les 16 canaux peuvent être configurés indépendamment, | ||
+ | |||
+ | Trois potentiomètres 5KΩ sont connectés à trois broches d' | ||
+ | |||
+ | Pour faciliter la compréhension, | ||
+ | |||
+ | ^LED^GPIO Pin^ PWM Channel^ PWM Frequency^ PWM Resolution^ | ||
+ | | RED | GPIO 16 | 0 | 5000 (5 KHz) | 12 | | ||
+ | | GREEN | GPIO 17 | 2 | 8000 (8 KHz) | 13 | | ||
+ | | BLUE | GPIO 4 | 4 | 10000 (10 KHz) | 14 | | ||
+ | |||
+ | Un autre point important à retenir est que la résolution de l'ADC de l' | ||
+ | |||
+ | ===Schéma=== | ||
+ | |||
+ | L' | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | <code c pwmledPotentiometre.ino> | ||
+ | const int redLEDPin = 16; /* GPIO16 */ | ||
+ | const int greenLEDPin = 17; /* GPIO17 */ | ||
+ | const int blueLEDPin = 4; /* GPIO4 */ | ||
+ | uint16_t redDutyCycle; | ||
+ | uint16_t greenDutyCycle; | ||
+ | uint16_t blueDutyCycle; | ||
+ | |||
+ | const int redPWMFreq = 5000; /* 5 KHz */ | ||
+ | const int redPWMChannel = 0; | ||
+ | const int redPWMResolution = 12; | ||
+ | const int RED_MAX_DUTY_CYCLE = (int)(pow(2, | ||
+ | |||
+ | const int greenPWMFreq = 8000; /* 8 KHz */ | ||
+ | const int greenPWMChannel = 2; | ||
+ | const int greenPWMResolution = 13; | ||
+ | const int GREEN_MAX_DUTY_CYCLE = (int)(pow(2, | ||
+ | |||
+ | const int bluePWMFreq = 10000; /* 10 KHz */ | ||
+ | const int bluePWMChannel = 4; | ||
+ | const int bluePWMResolution = 14; | ||
+ | const int BLUE_MAX_DUTY_CYCLE = (int)(pow(2, | ||
+ | |||
+ | const int ADC_RESOLUTION = 4095; /* 12-bit */ | ||
+ | |||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | /* Initialize Serial Port */ | ||
+ | Serial.begin(115200); | ||
+ | /* Initialize PWM Channels with Frequency and Resolution */ | ||
+ | ledcSetup(redPWMChannel, | ||
+ | ledcSetup(greenPWMChannel, | ||
+ | ledcSetup(bluePWMChannel, | ||
+ | /* Attach the LED PWM Channel to the GPIO Pin */ | ||
+ | ledcAttachPin(redLEDPin, | ||
+ | ledcAttachPin(greenLEDPin, | ||
+ | ledcAttachPin(blueLEDPin, | ||
+ | | ||
+ | } | ||
+ | void loop() | ||
+ | { | ||
+ | /* Read Analog Input from three ADC Inputs */ | ||
+ | redDutyCycle = analogRead(A0); | ||
+ | greenDutyCycle = analogRead(A3); | ||
+ | blueDutyCycle = analogRead(A4); | ||
+ | /* Map ADC Output to maximum possible dutycycle */ | ||
+ | // | ||
+ | greenDutyCycle = map(greenDutyCycle, | ||
+ | blueDutyCycle = map(blueDutyCycle, | ||
+ | /* Set PWM Output of Channel with desired dutycycle */ | ||
+ | ledcWrite(redPWMChannel, | ||
+ | ledcWrite(greenPWMChannel, | ||
+ | ledcWrite(bluePWMChannel, | ||
+ | |||
+ | Serial.println(" | ||
+ | Serial.print(redDutyCycle); | ||
+ | Serial.print(" | ||
+ | Serial.print(greenDutyCycle); | ||
+ | Serial.print(" | ||
+ | Serial.print(blueDutyCycle); | ||
+ | Serial.print(" | ||
+ | |||
+ | delay(1000); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | {{ : | ||
+ |
/home/chanteri/www/fablab37110/data/pages/start/esp32/pwm.txt · Dernière modification : 2023/01/27 16:08 de 127.0.0.1