3// #include <stdio.h>
// #include "freertos/FreeRTOS.h"
// #include "freertos/task.h"
// #include "driver/ledc.h"
// #include "driver/adc.h"
// #define LED_PIN GPIO_NUM_23 // PWM OUTPUT
// #define ADC_CHANNEL ADC_CHANNEL_6 // GPIO34 (A2) for ADC
// //PWM Parameters
// #define PWM_CHANNEL LEDC_CHANNEL_0//ASSIGNING ONE CHANNEL FOR PWM SIGNAL
// //The PWM_CHANNEL (which is channel 0) will be linked to the GPIO pin controlling the brightness of the LED.
// #define PWM_FREQ 5000
// #define PWM_RESOLUTION LEDC_TIMER_8_BIT
// //PWM_RESOLUTION specifies the resolution of the PWM signal, which in this case is 8-bit resolution.
// //This means the duty cycle can vary between 0 and 255.
// void app_main(void) {
// // Set up ADC (Analog-to-Digital Converter) for reading the potentiometer
// adc1_config_width(ADC_WIDTH_BIT_12);// 12-bit resolution (0-4095)
// adc1_config_channel_atten(ADC_PIN, ADC_ATTEN_DB_);// 0dB attenuation (0-3.3V)
// // Set up PWM (Pulse Width Modulation) for controlling the LED brightness
// ledc_timer_config_t pwm_timer = {
// .speed_mode = LEDC_HIGH_SPEED_MODE,
// .timer_num = LEDC_TIMER_0,
// .duty_resolution = PWM_RESOLUTION,
// .freq_hz = PWM_FREQ,// Frequency set to 5 kHz
// .clk_cfg = LEDC_AUTO_CLK
// };
// ledc_timer_config(&pwm_timer);
// ledc_channel_config_t pwm_channel = {
// .channel = PWM_CHANNEL,
// .duty = 0,
// .gpio_num = LED_PIN,
// .speed_mode = LEDC_HIGH_SPEED_MODE,
// .hpoint = 0,
// .timer_sel = LEDC_TIMER_0
// };
// ledc_channel_config(&pwm_channel);
// // gpio_config_t cfg = {
// // .pin_bit_mask = (1<<LED);
// // .mode = GPIO_MODE_OUTPUT;
// // }
// // gpio_config(&cfg);
// while (true) {
// int adc_value = adc1_get_raw(ADC_PIN);
// int duty_cycle = (adc_value * 255)/4095; // Map 0-4095 to 0-255
// ledc_set_duty(LEDC_HIGH_SPEED_MODE, PWM_CHANNEL, duty_cycle);
// ledc_update_duty(LEDC_HIGH_SPEED_MODE, PWM_CHANNEL);
// vTaskDelay(50 / portTICK_PERIOD_MS);
// }
// }
#include <stdio.h>
#include "driver/ledc.h"
#include "driver/adc.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
// Define the pins and channels
#define ADC_PIN ADC1_CHANNEL_6 // GPIO34 for potentiometer (ADC)
#define LED_PIN GPIO_NUM_23 // GPIO23 for LED (PWM)
// Define PWM parameters
#define PWM_CHANNEL LEDC_CHANNEL_0 // PWM Channel 0
#define PWM_FREQ 5000 // PWM Frequency in Hz
#define PWM_RESOLUTION LEDC_TIMER_8_BIT // 8-bit resolution (0-255 duty cycle)
void app_main(void) {
// Set up ADC (Analog-to-Digital Converter) for reading the potentiometer
adc1_config_width(ADC_WIDTH_BIT_12); // 12-bit resolution (0-4095)
adc1_config_channel_atten(ADC_PIN, ADC_ATTEN_DB_0); // 0dB attenuation (0-3.3V)
// Set up PWM (Pulse Width Modulation) for controlling the LED brightness
ledc_timer_config_t pwm_timer = {
.speed_mode = LEDC_HIGH_SPEED_MODE, // High-speed mode
.timer_num = LEDC_TIMER_0, // Timer 0
.duty_resolution = PWM_RESOLUTION, // 8-bit resolution (0-255 duty cycle)
.freq_hz = PWM_FREQ, // Frequency set to 5 kHz
.clk_cfg = LEDC_AUTO_CLK // Auto-select clock
};
ledc_timer_config(&pwm_timer);
ledc_channel_config_t pwm_channel = {
.channel = PWM_CHANNEL, // Use PWM Channel 0
.duty = 0, // Initial duty cycle 0 (LED off)
.gpio_num = LED_PIN, // Control LED on GPIO 23
.speed_mode = LEDC_HIGH_SPEED_MODE, // High-speed mode
.hpoint = 0, // No high point offset
.timer_sel = LEDC_TIMER_0 // Link to Timer 0
};
ledc_channel_config(&pwm_channel);
while (1) {
// Read the potentiometer value (0-4095)
int adc_value = adc1_get_raw(ADC_PIN);
printf("ADC Value: %d\n", adc_value); // Print the value for debugging
// Map the potentiometer value (0-4095) to PWM duty cycle (0-255)
int duty_cycle = (adc_value * 255) / 4095;
// Set the PWM duty cycle to control LED brightness
ledc_set_duty(LEDC_HIGH_SPEED_MODE, PWM_CHANNEL, duty_cycle);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, PWM_CHANNEL);
vTaskDelay(50 / portTICK_PERIOD_MS); // Small delay of 50ms
}
}
//PWM_CHANNEL: Defines which PWM channel is used (here, it's channel 0).
//PWM_FREQ (5000 Hz): Sets the PWM signal frequency to 5 kHz, which ensures smooth dimming of the LED without noticeable flicker.
//PWM_RESOLUTION (8-bit): Specifies the resolution of the PWM signal. With 8-bit resolution, you get 256 different duty cycle levels (0-255) to control the LED brightness.