Can we talk about DAC0. I am just trying to see what it generates. No idea if my code is correct and if you can send DAC connections to A1 or D3 but that is what I'm trying to do and I am not getting very useful results.
/*
* DAC to ADC
* Digital to Analog Converter (DAC) and Analog to Digital Converter (ADC)
* DAC converts a digital signal into analog signal.
* ADC converts an Analog signal into a digital signal
*
* Connect Pin A0 (DAC0) to A1
* Connect Pin A0 (DAC0) to Pin 3 (D3)
*
*
* Update Feb 7th, 2020
*
* analogWriteResolution(10) assumed default for DAC 0 - 1023 --> 0 to 3.3 Volts
*/
int myRandomNumber = 0;
void setup() {
Serial.begin(9600);
pinMode(DAC0, OUTPUT);
pinMode(3, INPUT_PULLDOWN);
// pinMode(A1, INPUT); You don't have to declare A1 for Analog input, that is the default
}
void loop() {
myRandomNumber = rand() % 1023;
analogWrite(DAC0, myRandomNumber );
Serial.print("Max = 1023, DAC A0 sending: ");
Serial.println(myRandomNumber);
Serial.print("Analog Read A1: ");
Serial.println(analogRead(A1));
Serial.print("Digital Read Pin 3 (D3): ");
Serial.println(digitalRead(3));
Serial.println("...");
delay(6000); // wait a few seconds
}
Here is the signal from your DAC. I change the delay to 60ms to speed things up. As you can see the voltage randomly changes with a maximum around 2.3V. There is a lot of noise in the signal which comes from the MPM3610 converter.
I am not sure why the signal does not go up to 3.3V. Maybe I missed something in the datasheet.
Thanks Klaus_K that gives me stuff to work on and possibly explains a bit of the variability I was seeing with my AnalogRead and digitalRead. Will work on it a bit. About losing the top 1.1 volts, I assumed the DAC channel is 10 bit (0-1023) but perhaps it is something else. Will mess around with that a bit today.
Any idea if there is a hardware issue with feeding the DAC output to the ADC input? I have done that on non-Arduino boards without an issue.
That seems to solve it for me Klaus_K. Instead of a range from 0 - 1023, it seems to have a fairly accurate range from 0-800 which would correspond to about 0-2.5 volts. My AnalogRead at A1 loses about 5% of the value of whatever the DAC was set to, with larger loss above about 650. Using a range from 0-800 works nicely as the Digital pin changes around 400.
Looks to me like a hardware issue that isn't allowing the top 2.5-3.3 volts, but either way I can work with these results. Looks like it is a fairly solid linear scale from 0 - 2.3 volts.
Thanks for the Help:
Here is my printout, each third line is the random set value.
DAC0 set to: 0, A1: 0, Pin(D3): 0
DAC0 set to: 800, A1: 702, Pin(D3): 1
DAC0 set to: 795, A1: 704, Pin(D3): 1
DAC0 set to: 0, A1: 0, Pin(D3): 0
DAC0 set to: 800, A1: 705, Pin(D3): 1
DAC0 set to: 1, A1: 2, Pin(D3): 0
DAC0 set to: 0, A1: 4, Pin(D3): 0
DAC0 set to: 800, A1: 704, Pin(D3): 1
DAC0 set to: 306, A1: 292, Pin(D3): 0
DAC0 set to: 0, A1: 2, Pin(D3): 0
DAC0 set to: 800, A1: 705, Pin(D3): 1
DAC0 set to: 371, A1: 354, Pin(D3): 0
DAC0 set to: 0, A1: 1, Pin(D3): 0
DAC0 set to: 800, A1: 703, Pin(D3): 1
DAC0 set to: 574, A1: 547, Pin(D3): 1
DAC0 set to: 0, A1: 2, Pin(D3): 0
DAC0 set to: 800, A1: 704, Pin(D3): 1
DAC0 set to: 376, A1: 358, Pin(D3): 0
DAC0 set to: 0, A1: 2, Pin(D3): 0
DAC0 set to: 800, A1: 706, Pin(D3): 1
DAC0 set to: 879, A1: 706, Pin(D3): 1
DAC0 set to: 0, A1: 0, Pin(D3): 0
DAC0 set to: 800, A1: 705, Pin(D3): 1
DAC0 set to: 415, A1: 397, Pin(D3): 1
DAC0 set to: 0, A1: 1, Pin(D3): 0
DAC0 set to: 800, A1: 704, Pin(D3): 1
DAC0 set to: 1017, A1: 703, Pin(D3): 1
DAC0 set to: 0, A1: 3, Pin(D3): 0
DAC0 set to: 800, A1: 705, Pin(D3): 1
DAC0 set to: 380, A1: 354, Pin(D3): 0
DAC0 set to: 0, A1: 3, Pin(D3): 0
DAC0 set to: 800, A1: 705, Pin(D3): 1
DAC0 set to: 288, A1: 275, Pin(D3): 0
DAC0 set to: 0, A1: 0, Pin(D3): 0
DAC0 set to: 800, A1: 705, Pin(D3): 1
DAC0 set to: 686, A1: 654, Pin(D3): 1
Here's the code I used. Connecting a wire between A0 and A1 and also between A0 and D3.
void setup() {
analogWriteResolution(10);
Serial.begin(9600);
pinMode(DAC0, OUTPUT);
pinMode(3, INPUT_PULLDOWN);
// pinMode(A1, INPUT); You don't have to declare A1 for Analog input, that is the default
}
void loop() {
myDac(0);
myDac(800);
int myRandomNumber = rand() % 1023;
// int myRandomNumber = rand() % 50+380;
myDac(myRandomNumber);
Serial.println("----------------------");
delay(6000); // wait a few seconds
}
void myDac(int myDacSet){
analogWrite(DAC0, myDacSet);
Serial.print("DAC0 set to: ");
Serial.print(myDacSet);
Serial.print(", A1: ");
Serial.print(analogRead(A1));
Serial.print(", Pin(D3): ");
Serial.println(digitalRead(3));
}