/****************************************************************
* DESCRIPCIÓN:
* El programa enciende el LED con la intensidad regulado por el
* potenciómetro
*
* MATERIALES:
* - 1 sensor KY-018
* - 1 LED
* - Placa de prototipos y puentes para conexionado
* - Cable USB compatible con la tarjeta
*
***************************************************************/
//#include <math.h>
#define PIN_SENSOR_PHRESISTOR 34
// Valores de la fórmula AJUSTE LINEAL
#define A 1.0f
#define B 1.0f
// Variables globales: declaración
float phtr_valor;
// Declaración de funciones
void configADC(uint8_t pin);
float phtr_medicion(uint8_t valorADC);
void setup() {
Serial.begin(115200);
//pinMode( PIN_SENSOR_PHRESISTOR, INPUT);
Serial.printf("\n Lectura de valor en PIN( %i )", PIN_SENSOR_PHRESISTOR);
configADC( PIN_SENSOR_PHRESISTOR );
}
void loop() {
phtr_valor = phtr_medicion( PIN_SENSOR_PHRESISTOR );
//Serial.printf( "\n Resistencia = %5.3f Ohmios", phtr_valor );
delay(500);
}
/****************************************************************************
* FUNCIONES DE MEDICIÓN
*****************************************************************************/
float phtr_medicion( uint8_t pin ) {
// Función para ajuste de la medida a voltios (puede ajustarse a irradiación lumínica)
static uint16_t entradaADC; // valor de la tensión de entrada en el pin => ADC
static float valor;
entradaADC = analogRead ( pin );
// Comprueba si valorADC > 0
if ( entradaADC < 1 ) {
// Serial.printf( "\n Error: Lectura invalida -> ( %i ) < 1 \n", entradaADC );
return 0;
}
// Correción del valor de lectura
valor = float( A * entradaADC ) + B;
Serial.printf( "\n valor \t ADC = %5i ", entradaADC );
//Serial.printf( "\n Valor PHTResistor \t = -%5.3f Ohms", valor ); // línea comentada para depuración
return valor;
}
/*****************************************************************************
* FUNCIONES DE CONFIGURACIÓN DE ENTRADAS ADC
******************************************************************************/
void configADC( uint8_t pin = 0 ){
/*analogReadResolution(resolution):set the sample bits and resolution.
It can be a value between 9 (0 - 511) and 12 bit (0 - 4095). Default is the 12-bit resolution
*/
analogReadResolution(12);
/*analogSetWidth(width): set the sample bits and resolution.
It can be a value between 9 (0 - 511) and 12 bit (0 - 4095). Default is the 12-bit resolution
*/
analogSetWidth(12);
/*analogSetCycles(cycles): set the number of cycles per sample.
Default is 8. Range: 1 to 255.
*/
//analogSetCycles(8);
/*analogSetSamples(samples): set the number of samples in the range.
Default is 1 sample- It has an effect of increasing sensitivity.
*/
//analogSetSamples(2);
/*analogSetClockDiv(attenuation): set the divider for the ADC clock.
Default is 1. Range: 1 to 255.
*/
//analogSetClockDiv(1);
/*analogSetAttenuation(attenuation): sets the input attenuation for all ADC pin. Default is ADC_11db. Acepted values:
ADC_0db : sets no attenuation. ADC can measure up to approximately 800 mV (1V input = ADC reading of 1088).
ADC_2_5db: The input voltage of ADC will be attenuated, extending the range of measurement to up to approx. 1100 mV. (1V input = ADC reading of 3722).
ADC_6db: The input voltage of ADC will be attenuated, extending the range of measurement to up to approx. 1350 mV. (1V input = ADC reading of 3033).
ADC_11db: The input voltage of ADC will be attenuated, extending the range of measurement to up to approx. 2600 mV. (1V input = ADC reading of 1575).
*/
//analogSetAttenuation(ADC_11db);
/*analogSetPinAttenuation(pin, attenuation): sets the input attenuation for the specified pin.
The default is ADC_11db. Attenuation values are the same from previous function.
*/
if ( pin != 0 ) analogSetPinAttenuation( pin, ADC_11db );
/*adcAttachPin(pin): Attach a pin to ADC (also clears any other analog mode that could be on).
Returns TRUE or FALSE result.
*/
/*if ( pin != 0 ){
if ( adcAttachPin(pin) ){
Serial.printf( "\n\n PIN ( %i ) CONECTADO AL ADC ", pin);
}
else{
Serial.printf( "\n\n PIN ( %i ) NO CONECTADO AL ADC ", pin);
}
}*/
/*adcStart(pin), adcBusy(pin) and resultadcEnd(pin): starts an ADC convertion on attached pin's bus. Check if conversion on the pin's ADC bus is
currently running (returns TRUE or FALSE). Get the result of the conversion: returns 16-bit integer.
*/
}