Arduino en español
Circuitos con Arduino - Juan Antonio Villalpando
-- Tutorial de iniciación a Arduino --
Volver al índice del tutorial
____________________________
43.- Interruptor. Sensor de detección de lluvia. Salida analógica y digital.
- Detecta la lluvia. Consta de un panel
níquel-plateado, eficaz contra la oxidación
Según la humedad y el umbral del potenciómetro, la salida digital será ALTA o BAJA.
Cuando esté seco estará en ALTA.
Cuando esté mojado estará en BAJA.
Mediante el potenciómetro podremos establecer el umbral entre seco y mojado.
La salida analógica es un divisor de tensión con una resistencia fija de 10K y el sensor.
El sensor tendrá una resistencia de 100K cuando esté mojado y de 2M cuando este seco.
En el siguiente código obtendremos unos valores comprendido entre 1024 cuando esté seco y unos 200 cuando esté totalmente mojado.
- AC Salida analógica.
- OUT salida digital.
_______________________
- Código.
- Lo conectamos a Vcc (5V), Gnd, el terminal DO al pin digital 2 del Arduino y el terminal A0 al terminal analógico 0 del Arduino.
- Cuando supera el umbral, se enciende el LED13.
- El umbral de humedad se establece mediante el potenciómetro.
- En el siguiente código obtendremos unos valores comprendido entre 1024 cuando esté seco y unos 200 cuando esté totalmente mojado.
Código |
/*
Juan A. Villalpando
kio4.com
*/
int LED13 = 13;
int sensorD = 2;
int sensorA = A0;
int valorD = 0;
int valorA = 0;
void setup(){
Serial.begin(9600);
pinMode (LED13,OUTPUT);
pinMode (valorD,INPUT);
}
void loop(){
valorD = digitalRead(sensorD);
valorA = analogRead(sensorA);
Serial.println(valorA);
if (valorD == HIGH){
digitalWrite(LED13,HIGH);
Serial.println("D2 = ALTO");
}
else {
digitalWrite(LED13,LOW);
Serial.println("D2 = BAJO");
}
delay(200);
} |
_______________________________________
- Otro código.
Código
Rain_Sensor_by_Reichenstein7 |
/* Flame Sensor analog example.
Code by Reichenstein7 (thejamerson.com)
For use with a Rain Sensor with an analog out!
To test view the output, point a serial monitor such as Putty at your Arduino.
- If the Sensor Board is completely soaked; "case 0" will be activated and " Flood " will be sent to the serial monitor.
- If the Sensor Board has water droplets on it; "case 1" will be activated and " Rain Warning " will be sent to the serial monitor.
- If the Sensor Board is dry; "case 2" will be activated and " Not Raining " will be sent to the serial monitor.
*/
// lowest and highest sensor readings:
const int sensorMin = 0; // sensor minimum
const int sensorMax = 1024; // sensor maximum
void setup() {
// initialize serial communication @ 9600 baud:
Serial.begin(9600);
}
void loop() {
// read the sensor on analog A0:
int sensorReading = analogRead(A0);
// map the sensor range (four options):
// ex: 'long int map(long int, long int, long int, long int, long int)'
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// range value:
switch (range) {
case 0: // Sensor getting wet
Serial.println("Flood");
break;
case 1: // Sensor getting wet
Serial.println("Rain Warning");
break;
case 2: // Sensor dry - To shut this up delete the " Serial.println("Not Raining"); " below.
Serial.println("Not Raining");
break;
}
delay(1); // delay between reads
}
|
________________________________
|