library/esp32_motor/esp32_motor.ino

38 lines
805 B
Arduino
Raw Normal View History

2018-07-09 11:18:52 +02:00
#define MOTOR_SPEED 27
#define MOTOR_FWRD 26
#define MOTOR_BACK 25
#define MAX_SPEED 0xffff
#include "esp32-hal-ledc.h"
void setup() {
// put your setup code here, to run once:
pinMode(MOTOR_FWRD,OUTPUT);
pinMode(MOTOR_BACK,OUTPUT);
digitalWrite(MOTOR_BACK,LOW);
digitalWrite(MOTOR_FWRD,HIGH);
//for motor ...
ledcSetup(1, 5000, 8); // channel 1, 50 Hz, 16-bit width
ledcAttachPin(MOTOR_SPEED, 1); // GPIO 22 assigned to channel 1
analogWrite(MOTOR_SPEED, 0);
Serial.begin(115200);
Serial.println("Enter values between 0 - 255");
}
void loop() {
if(Serial.available())
{
uint8_t speed = Serial.parseInt();
Serial.println(speed);
analogWrite(MOTOR_SPEED, speed );
}
}
void analogWrite(uint8_t pin, uint8_t speed){
ledcWrite(1, speed);
}