Created
February 19, 2026 13:38
-
-
Save Robotto/ca6cdae6b9fc93cfc064f9ee228870c8 to your computer and use it in GitHub Desktop.
Feedback Servo på ESP32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| PINOUT: | |
| RØD: 3.3V | |
| ORANGE: 5V | |
| SORT: GND | |
| GRÅ: 18 | |
| */ | |
| // Include the ESP32 Arduino Servo Library instead of the original Arduino Servo Library | |
| #include <ESP32Servo.h> | |
| Servo myservo; // create servo object to control a servo | |
| // Possible PWM GPIO pins on the ESP32: 0(used by on-board button),2,4,5(used by on-board LED),12-19,21-23,25-27,32-33 | |
| // Possible PWM GPIO pins on the ESP32-S2: 0(used by on-board button),1-17,18(used by on-board LED),19-21,26,33-42 | |
| // Possible PWM GPIO pins on the ESP32-S3: 0(used by on-board button),1-21,35-45,47,48(used by on-board LED) | |
| // Possible PWM GPIO pins on the ESP32-C3: 0(used by on-board button),1-7,8(used by on-board LED),9-10,18-21 | |
| //DE ER GRÅ! | |
| int servoPin = 18; // GPIO pin used to connect the servo control (digital out) | |
| // Possible ADC pins on the ESP32: 0,2,4,12-15,32-39; 34-39 are recommended for analog input | |
| // Possible ADC pins on the ESP32-S2: 1-20 are recommended for analog input | |
| int potPin = 34; // GPIO pin used to connect the potentiometer (analog in) | |
| int ADC_Max = 4096; // This is the default ADC max value on the ESP32 (12 bit ADC width); | |
| // this width can be set (in low-level oode) from 9-12 bits, for a | |
| // a range of max values of 512-4096 | |
| int val; // variable to read the value from the analog pin | |
| //ORANGE: 5V! (VCC) | |
| //RØD: 3.3V | |
| void setup() | |
| { | |
| Serial.begin(115200); | |
| // Allow allocation of all timers | |
| ESP32PWM::allocateTimer(0); | |
| ESP32PWM::allocateTimer(1); | |
| ESP32PWM::allocateTimer(2); | |
| ESP32PWM::allocateTimer(3); | |
| myservo.setPeriodHertz(50);// Standard 50hz servo | |
| myservo.attach(servoPin, 500, 2400); // attaches the servo on pin 18 to the servo object | |
| // using SG90 servo min/max of 500us and 2400us | |
| // for MG995 large servo, use 1000us and 2000us, | |
| // which are the defaults, so this line could be | |
| // "myservo.attach(servoPin);" | |
| } | |
| void loop() { | |
| val = ADC_Max-analogRead(potPin); // read the value of the potentiometer (value between 0 and 4096) | |
| Serial.print(val); | |
| Serial.print(","); | |
| val = map(val, 0, ADC_Max, 50, 180); // scale it to use it with the servo (value between 0 and 180) | |
| Serial.println(val); | |
| myservo.write(val); // set the servo position according to the scaled value | |
| delay(200); // wait for the servo to get there | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment