// https://wokwi.com/projects/380733829667456001
// el modulo DAC no genera tensiones diferentes en el simulador
#define pin2ADC 34
#define DAC2pin 25
void setup() {
//set the resolution to 12 bits (0-4096)
analogReadResolution(12);
// voltage full-range 150mV - 3100 mV
analogSetAttenuation(ADC_11db); // ADC_0db, ADC_2_5db, ADC_6db, ADC_11db
// DAC channel 1 is attached to GPIO25, DAC channel 2 is attached to GPIO26
// acepta valores de 0 a 255 -> Vout = 0 a 3.3v
Serial.begin(115200);
Serial.printf("Ejemplo de configuracion y uso ADC-DAC \n");
}
int dacVal, adcVal;
float voltageADC, tensionDAC;
void loop() {
adcVal = analogRead(pin2ADC); // "D" leemos el valor de tensión en el pin a través del bloque ADC
voltageADC = (3.3 / 4095.0)* adcVal; // ( (3.3 / 4095.0)* adcVal --> q*D) conversion de tension ideal Vo=q*D+Vref-(Vref-=0 y q=(Vref+ - Vref-)/2^n - 1)
Serial.printf("Voltage entrada: %1.3f V => valor ADC: %d, \n", voltageADC, adcVal);
// en funcion del valor adcVal 0 a 4095 la tensión a generar en el DAC debe ser 1.5 a 2.5 --> Vref-=1,5 y Vref+=2,5
tensionDAC = 1.5 + 1.0*adcVal/4095; //Vo=q*D+Vref- --> Vo= ((2,5-1,5)/2^12 -1 )* D + 1,5
dacVal = 255.0 * ( tensionDAC / 3.3); // tensión de salida en el DAC --> D = (VeMax - Vref-)/q --> q = (Vref+ - Vref-)/2^8 -1 --> Vref+=3,3 y Vref-=0 (DAC)
dacWrite(DAC2pin, dacVal); // ilumina más o menos el LED
Serial.printf("Valor DAC: %d, Tension generada: %1.3f V \n\n", dacVal, tensionDAC);
delay(3000);
}