domingo, 8 de julio de 2012

Lectura de valores en el puerto analogo de arduino

 

En este proyecto podremos visualizar como trabaja el puerto análogo, es sencillo pero al comprenderlo podemos aplicarlo a muchas ideas como, Voltímetro, control de temperaturas para unidades de acondicionadores de aire, controladores de carga para bancos de baterías de sistemas solares o eólicos (molinos de viento), en fin son muchísimas las aplicaciones que podemos darle al Arduino utilizando los puertos análogos.

arduinoLedPotSerial

Los componentes los podemos ver en la imagen. Publicare tres Sketch para poder comprender como funciona el puerto análogo.

El primer Sketch podremos ver como al variar la posición del pot la lectura en el puerto análogo (pin 0) se reflejara en la velocidad con que el led prende y apaga. La variación será en milisegundos.


/*
Pot sketch
blink an LED at a rate set by the position of a potentiometer
*/
const int potPin = 0; // select the input pin for the potentiometer
const int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup()
{
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(potPin); // read the voltage on the pot
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // blink rate set by pot value (in milliseconds)
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // turn led off for same period as it was turned on
}


En el segundo Sketch podremos observar como Arduino interpreta la variación de voltaje en el puerto análogo (pin0) y nos la mostrara en el Serial Monitor del programa Arduino como lo podrán ver en la imagen.

Podremos notar que cuando en el pin sea 0 Voltios el serial les mostrara 0 y cuando sea 5 Voltios entonces el serial les mostrara 1023, Los valores serán entre 0 Hasta 1023.

image


/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}


En el siguiente Sketch combinare los dos Sketch anteriores haciendo unas modificaciones al segundo para incorporarlo al primero, para que sirva de ejemplo.


/*
Pot sketch
blink an LED at a rate set by the position of a potentiometer
*/
const int potPin = 0; // select the input pin for the potentiometer
const int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup()
{
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);

}

void loop() {
val = analogRead(potPin); // read the voltage on the pot
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // blink rate set by pot value (in milliseconds)
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // turn led off for same period as it was turned on

// read the input on analog pin 0:
int val = analogRead(potPin);
// print out the value you read:
Serial.println(val);
delay(1); // delay in between reads for stability
}


Esta parte es importante comprenderla porque será la base para otros proyectos que mostrare, como obtener lecturas remotas, pero será mas adelante. Espero les sirva para practicar, luego mostrare como hacer el voltímetro y el control de temperatura, para la próxima.

No hay comentarios.: