#include <Arduino.h>
const byte pwm_gpio = 4; // the PWM pin the Motor is attached to
int duty = 255*0.3; // speed of Motor
const int SpeedControl = 1; // SpeedControl pin
int Speed_Control_State;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-C3!");
pinMode(SpeedControl, INPUT);
// Initialize channels
// channels 0-15, resolution 1-16 bits, freq limits depend on resolution
// ledcSetup(uint8_t channel, uint32_t freq, uint8_t resolution_bits);
#ifdef ESP_ARDUINO_VERSION_MAJOR
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
ledcAttachChannel(pwm_gpio, 5000, 8, 0); // 5kHz PWM, 8-bit resolution
#else
ledcAttachPin(pwm_gpio, 0);
ledcSetup(0, 5000, 8);
#endif
#endif
}
void loop() {
ledcWrite(pwm_gpio, duty); // set the duty of the Motor
Speed_Control_State = digitalRead(SpeedControl);
// change speed based on current state of SpeedControl pin
if (Speed_Control_State == HIGH) {
duty = 255*0.6;
} else {
duty = 255*0.3;
}
// wait for 50 milliseconds to check change in SpeedControl pin
delay(50);
}Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1