Tutorial del Internet de las Cosas y Bluetooth con el ESP32
Juan Antonio Villalpando
Volver al índice del tutorial
____________________________
212.- Wemos D1 R32 ESP32. Un potenciómetro mueve un Servo.
- Al mover el potenciómetro se mueve el Servo. Este servo tiene un ángulo de giro de 180º.
- El Servo funciona a una frecuencia de 50 Hz.
- Utilizaremos el canal 0 y una resolución de 8 bits.
- En este ejemplo no utilizaremos librería para el servo, en el ejemplo de la página 214 sí utilizaremos librería.
________________________________
- Conexiones.

- En este ejemplo he conectado el Servo directamente a los 5V de la tarjeta, normalmente se suele conectar a una alimentación externa.
- Cuidado con los terminales analógicos porque si vas a trabajar con librerías WiFi no podrán utilizar algunos terminales como entrada analógica:
The esp32 integrates two 12-bit ACD registers. ADC1 whit 8 channels attached to GPIOs 32-39 and ADC2 whit 10 channels in another pins. The thing is the ESP32 uses the ADC2 to manage wifi functions, so if you use Wifi, you can´t use that register.
The ADC driver API supports ADC1 (8 channels, attached to GPIOs 32 - 39), and ADC2 (10 channels, attached to GPIOs 0, 2, 4, 12 - 15 and 25 - 27). However, the usage of ADC2 has some restrictions for the application:
-
ADC2 is used by the Wi-Fi driver. Therefore the application can only use ADC2 when the Wi-Fi driver has not started.
-
Some of the ADC2 pins are used as strapping pins (GPIO 0, 2, 15) thus cannot be used freely.
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html
________________________________
- Código.
|
// Juan A. Villalpando
// kio4.com
#define IO2_Analogico 2
int valor_analogico = 0;
#define IO14_Servo 14
int frecuencia = 50;
int canal = 0;
int resolucion = 8;
int dutyCycle = 21;
void setup()
{
Serial.begin(115200);
ledcSetup(canal, frecuencia, resolucion);
ledcAttachPin(IO14_Servo, canal);
ledcWrite(canal, dutyCycle);
}
void loop()
{
valor_analogico = analogRead(IO2_Analogico);
Serial.print(valor_analogico);
Serial.print(" Duty Cycle = ");
Serial.println(dutyCycle);
dutyCycle = map(valor_analogico, 0, 4095, 0, 100);
ledcWrite(canal, dutyCycle);
delay(50);
}
|
______________________________________________
- Potenciómetro en WiFi.
- Utilizo el terminal 34.
- Saca valores de 0 a 3,3 mediante map.
|
// Juan A. Villalpando.
// KIO4.COM
// Potenciometro.
#include <WiFi.h>
const char* ssid = "Nombre_Red";
const char* password = "Contraseña_Red";
// Setting Static IP.
IPAddress local_IP(192, 168, 1, 115);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); //opcional
IPAddress secondaryDNS(8, 8, 4, 4); //opcional
WiFiServer server(80); // Port 80
int wait30 = 30000; // time to reconnect when connection is lost.
const int Ent_Analogica = 34; // Entrada analógica.
int valor = 0;
float valor_map = 0;
void setup() {
Serial.begin(115200);
// Setting Static IP.
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("Error in configuration.");
}
// Connect WiFi net.
Serial.println();
Serial.print("Connecting with ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected with WiFi.");
// Start Web Server.
server.begin();
Serial.println("Web Server started.");
// This is IP
Serial.print("This is IP to connect to the WebServer: ");
Serial.print("http://");
Serial.println(WiFi.localIP());
}
void loop() {
// If disconnected, try to reconnect every 30 seconds.
if ((WiFi.status() != WL_CONNECTED) && (millis() > wait30)) {
Serial.println("Trying to reconnect WiFi...");
WiFi.disconnect();
WiFi.begin(ssid, password);
wait30 = millis() + 30000;
}
// Check if a client has connected..
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.print("New client: ");
Serial.println(client.remoteIP());
// Espera hasta que el cliente envíe datos.
// while(!client.available()){ delay(1); }
/////////////////////////////////////////////////////
// Read the information sent by the client.
String req = client.readStringUntil('\r');
Serial.println(req);
// Make the client's request.
if (req.indexOf("consulta") != -1){
valor = analogRead(Ent_Analogica);
valor_map = map(valor, 0, 4095, 0, 330);
Serial.println(valor);
}
//////////////////////////////////////////////
// Página WEB. ////////////////////////////
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // Comillas importantes.
client.println(valor_map/100); // Return status.
//client.flush();
//client.stop();
Serial.println("Client disconnected.");
}
|
_______________________________
|