-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensoresUltrasonidos.html
161 lines (140 loc) · 15.6 KB
/
sensoresUltrasonidos.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!DOCTYPE html>
<html lang="es">
<head>
<meta name="description=" content="curso tutorial aprende arduino básico desde cero facíl">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="Curso Arduino">
<meta name="author" content="Studio Miranda">
<title>arduinoes</title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.css" rel="stylesheet">
<!-- Custom fonts for this template -->
<link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css">
<link href='https://fonts.googleapis.com/css?family=Kaushan+Script' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<!-- <link href='https://fonts.googleapis.com/css?family=Roboto+Slab:400,100,300,700' rel='stylesheet' type='text/css'> -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans|Roboto&display=swap" rel="stylesheet" type='text/css'>
<link href="css/studio.css" rel="stylesheet">
</head>
<body>
<!-- Sección -->
<div class="container" style = "margin-top: 50px">
<div class="row">
<div class="col-lg-8 mx-auto">
<div class="modal-body">
<div class="miranda">
<!-- Project Details Go Here -->
<h2 class="text-uppercase text-center"><bl>ESP8266 ARDUINO CON WIFI</bl></h2>
<br>
<img class="img-fluid d-block mx-auto" src="img/tutoriales/Arduino1.png" alt="">
<br>
<h2 class="text-center"><or>CARACTERÍSTICAS DEL ESP8266</or></h2>
<br>
<div class="text-left">
<!-- ESP8266 -->
<h3>Ultrasonidos Sketch</h3>
<br>
<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: rgb(240, 240, 240); color: rgb(68, 68, 68);"><span class="hljs-keyword" style="font-weight: 700;">const</span> <span class="hljs-keyword" style="font-weight: 700;">int</span> TriggerPin = <span class="hljs-number" style="color: rgb(136, 0, 0);">12</span>; <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Asigna el pin de disparo</span>
<span class="hljs-keyword" style="font-weight: 700;">const</span> <span class="hljs-keyword" style="font-weight: 700;">int</span> EchoPin = <span class="hljs-number" style="color: rgb(136, 0, 0);">13</span>; <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Asignamos el pin de escucha</span>
<span class="hljs-keyword" style="font-weight: 700;">long</span> duracion, distanciaCm; <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Iniciamos variables en 0</span>
<span class="hljs-keyword" style="font-weight: 700;">void</span> <span class="hljs-built_in" style="color: rgb(57, 115, 0);">setup</span>() {
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">Serial</span>.<span class="hljs-built_in" style="color: rgb(57, 115, 0);">begin</span>(<span class="hljs-number" style="color: rgb(136, 0, 0);">9600</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Iniciamos la comunicación serial</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">pinMode</span>(TriggerPin, <span class="hljs-literal" style="color: rgb(120, 169, 96);">OUTPUT</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// El disparo es de salida</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">pinMode</span>(EchoPin, <span class="hljs-literal" style="color: rgb(120, 169, 96);">INPUT</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// La escucha es de entrada</span>
}
<span class="hljs-keyword" style="font-weight: 700;">void</span> <span class="hljs-built_in" style="color: rgb(57, 115, 0);">loop</span>() {
<span class="hljs-keyword" style="font-weight: 700;">int</span> cm = ping(TriggerPin, EchoPin);
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">Serial</span>.<span class="hljs-built_in" style="color: rgb(57, 115, 0);">print</span>(<span class="hljs-string" style="color: rgb(136, 0, 0);">"Distancia: "</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Imprime el texto</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">Serial</span>.<span class="hljs-built_in" style="color: rgb(57, 115, 0);">println</span>(cm); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// </span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">delay</span>(<span class="hljs-number" style="color: rgb(136, 0, 0);">1000</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Espera 1 segundo para una nueva lectura</span>
}
<span class="hljs-keyword" style="font-weight: 700;">int</span> ping(<span class="hljs-keyword" style="font-weight: 700;">int</span> TriggerPin, <span class="hljs-keyword" style="font-weight: 700;">int</span> EchoPin) { <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Función ping, parámetros disparo/escucha</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">digitalWrite</span>(TriggerPin, <span class="hljs-literal" style="color: rgb(120, 169, 96);">LOW</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Apagamos el pulso(LOW)</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">delayMicroseconds</span>(<span class="hljs-number" style="color: rgb(136, 0, 0);">4</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Esperamos 4 microsegundos</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">digitalWrite</span>(TriggerPin, <span class="hljs-literal" style="color: rgb(120, 169, 96);">HIGH</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Encendemos el pulso (HIGH)</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">delayMicroseconds</span>(<span class="hljs-number" style="color: rgb(136, 0, 0);">10</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Esperamos 10 microsegundos</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">digitalWrite</span>(TriggerPin, <span class="hljs-literal" style="color: rgb(120, 169, 96);">LOW</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Apagamos el pulso</span>
duracion = <span class="hljs-built_in" style="color: rgb(57, 115, 0);">pulseIn</span>(EchoPin, <span class="hljs-literal" style="color: rgb(120, 169, 96);">HIGH</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">//medimos el tiempo entre el disparo y la escucha pulsos</span>
distanciaCm = duracion * <span class="hljs-number" style="color: rgb(136, 0, 0);">10</span> / <span class="hljs-number" style="color: rgb(136, 0, 0);">292</span>/ <span class="hljs-number" style="color: rgb(136, 0, 0);">2</span>; <span class="hljs-comment" style="color: rgb(136, 136, 136);">//convertimos a distancia, en cm</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">return</span> distanciaCm;
}</pre>
<br>
<p>1 - Creamos 2 variables uno para el pin "disparador" y otro para pin de "escucha" llamados TriggerPin y EchoPin ,
respectivamente. El pin de disparo lo conectamos al Pin digital 12 ,
y los pin de escucha lo conectamos al Pin digital 13.</p>
<br>
<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: rgb(240, 240, 240); color: rgb(68, 68, 68);"><span class="hljs-keyword" style="font-weight: 700;">const</span> <span class="hljs-built_in" style="color: rgb(57, 115, 0);">int</span> TriggerPin = <span class="hljs-number" style="color: rgb(136, 0, 0);">12</span>; <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Asigna el pin de disparo</span>
<span class="hljs-keyword" style="font-weight: 700;">const</span> <span class="hljs-built_in" style="color: rgb(57, 115, 0);">int</span> EchoPin = <span class="hljs-number" style="color: rgb(136, 0, 0);">13</span>; <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Asignamos el pin de escucha</span></pre>
<br>
<p>
2 - También creamos 2 <or>variables</or> de tipo <or>long</or>, "duracion" y "distanciaCm". La variable de duración es el
tiempo entre la emisión (disparo) y la recepción (escucha) del pulso. La variable distanciaCm
guardará la distancia en centímetros.</p>
<br>
<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: rgb(240, 240, 240); color: rgb(68, 68, 68);"><span class="hljs-keyword" style="font-weight: 700;">long</span> duracion, distanciaCm; <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Iniciamos variables en 0</span></pre>
<br>
<p>
3 - En <or>void setup()</or>, inicializamos el puerto serie a una velocidad en baudios de 9600 y
configuramos el TriggerPin como salida y el EchoPin como entrada.</p>
<br>
<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: rgb(240, 240, 240); color: rgb(68, 68, 68);"><span class="hljs-keyword" style="font-weight: 700;">void</span> <span class="hljs-built_in" style="color: rgb(57, 115, 0);">setup</span>() {
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">Serial</span>.<span class="hljs-built_in" style="color: rgb(57, 115, 0);">begin</span>(<span class="hljs-number" style="color: rgb(136, 0, 0);">9600</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Iniciamos la comunicación serial</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">pinMode</span>(TriggerPin, <span class="hljs-literal" style="color: rgb(120, 169, 96);">OUTPUT</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// El disparo es de salida</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">pinMode</span>(EchoPin, <span class="hljs-literal" style="color: rgb(120, 169, 96);">INPUT</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// La escucha es de entrada</span>
}</pre>
<br>
<p>
4 - En <or>void loop()</or>, disparamos el sensor enviando un pulso HIGH de 10 microsegundos. Pero, antes de eso,
le damos un pulso LOW corto para asegurarse de obtener un pulso ALTO limpio</p>
<br>
<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: rgb(240, 240, 240); color: rgb(68, 68, 68);"><span class="hljs-keyword" style="font-weight: 700;">int</span> ping(<span class="hljs-keyword" style="font-weight: 700;">int</span> TriggerPin, <span class="hljs-keyword" style="font-weight: 700;">int</span> EchoPin) { <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Función ping, parámetros disparo/escucha</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">digitalWrite</span>(TriggerPin, <span class="hljs-literal" style="color: rgb(120, 169, 96);">LOW</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Apagamos el pulso(LOW)</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">delayMicroseconds</span>(<span class="hljs-number" style="color: rgb(136, 0, 0);">4</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Esperamos 4 microsegundos</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">digitalWrite</span>(TriggerPin, <span class="hljs-literal" style="color: rgb(120, 169, 96);">HIGH</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Encendemos el pulso (HIGH)</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">delayMicroseconds</span>(<span class="hljs-number" style="color: rgb(136, 0, 0);">10</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Esperamos 10 microsegundos</span>
<span class="hljs-built_in" style="color: rgb(57, 115, 0);">digitalWrite</span>(TriggerPin, <span class="hljs-literal" style="color: rgb(120, 169, 96);">LOW</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// Apagamos el pulso</span></pre>
<br>
<p><a href="https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/" target="_blank">digitalWrite()</a></p>
<br>
<p>
5 - Escribe un valor encedido "HIGH" (5 voltios) o apagado "LOW" (0 voltios) un pin digital.
</p>
<p>Ahora ya podemos escuchar el disparo del sensor: un pulso HIGH cuya duración es
en microsegundos el tiempo entre el disparo hasta la escucha el pulso.</p>
<br>
<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: rgb(240, 240, 240); color: rgb(68, 68, 68);">duracion = <span class="hljs-built_in" style="color: rgb(57, 115, 0);">pulseIn</span>(EchoPin, <span class="hljs-literal" style="color: rgb(120, 169, 96);">HIGH</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">//medimos el tiempo entre el disparo y la escucha pulsos</span></pre>
<br>
<p><a href="https://www.arduino.cc/reference/en/language/functions/advanced-io/pulsein/" target="_blank">pulseIn()</a></p>
<br>
<p>
6 - Lee un pulso (ya sea HIGH o LOW) en un pin. Por ejemplo, si el valor es HIGH, pulseIn()
espera a que el pin pase de LOW a HIGH, comienza a cronometrar, luego espera a que el pin
se vaya LOW y detiene el cronometraje. Devuelve la duración del pulso en microsegundos o
se da por vencido y devuelve 0 si no se recibió un pulso completo dentro del tiempo de espera.
El tiempo de esta función se ha determinado empíricamente y probablemente mostrará errores en
pulsos más largos. Funciona en pulsos de 10 microsegundos a 3 minutos de duración.
</p>
<p>
7 - Finalmente, solo necesitamos convertir la duración en una distancia. Podemos calcular la
distancia usando la siguiente fórmula (La velocidad del sonido es: 343 m/s).</p>
<br>
<img class="img-fluid d-block mx-auto" src="img/Ultrasonidos/formulaSonido.png" alt="">
<br>
</p>
<br>
<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: rgb(240, 240, 240); color: rgb(68, 68, 68);">distanciaCm = duracion * <span class="hljs-number" style="color: rgb(136, 0, 0);">10</span> <span class="hljs-regexp" style="color: rgb(188, 96, 96);">/ 292/</span> <span class="hljs-number" style="color: rgb(136, 0, 0);">2</span>; <span class="hljs-comment" style="color: rgb(136, 136, 136);">//convertimos a distancia, en cm</span>
<span class="hljs-keyword" style="font-weight: 700;">return</span> distanciaCm;
}</pre>
<br>
</div>
<br>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>