Arduino Forum

Products => Arduino Due => Topic started by: wdclot1 on Oct 11, 2016, 07:51 pm

Title: analogWrite Arduino DAC1 output like pwm digitalWrite
Post by: wdclot1 on Oct 11, 2016, 07:51 pm
I use analogWrite(DAC1,Output) in my code, but I find it like digital pulse output, rather than expected analog continuous curve, i don't know why, forgive a fresh people in C language.
Code: [Select]


#include<PID_v1.h>
double Setpoint, Input, Output;
PID myPID(&Input,&Output,&Setpoint,2,5,30,DIRECT);
void setup()
{
 
  Input= analogRead(A0);
 
  Setpoint = 3000;
  Serial.begin(9600);
  myPID.SetMode(AUTOMATIC);
  myPID.SetSampleTime(10);
  // put your setup code here, to run once:

}

void loop() {
  Input = analogRead(A0);
  Serial.print("Input = ");
  Serial.println(Input);
  myPID.Compute();
  analogWrite(DAC1,Output);
  Serial.print("Output = ");
  Serial.println(Output);
 
  // put your main code here, to run repeatedly:

}
Title: Re: analogWrite Arduino DAC1 output like pwm digitalWrite
Post by: spycatcher2k on Oct 11, 2016, 07:59 pm
What Arduino do you have? If it's an UNO or MEGA then it does not have a DAC, it uses PWM output. This can be turned to a kind of analog output with a low pass filter.
Title: Re: analogWrite Arduino DAC1 output like pwm digitalWrite
Post by: wdclot1 on Oct 11, 2016, 08:05 pm
I have  some Arduino Due borads, so there is DAC1 pin
Title: Re: analogWrite Arduino DAC1 output like pwm digitalWrite
Post by: spycatcher2k on Oct 11, 2016, 08:12 pm
Could try adding

Code: [Select]
  analogWriteResolution(12);  // set the analog output resolution to 12 bit (4096 levels)
  analogReadResolution(12);   // set the analog input resolution to 12 bit


to your setup
Title: Re: analogWrite Arduino DAC1 output like pwm digitalWrite
Post by: wdclot1 on Oct 11, 2016, 08:13 pm
(http://C:\Users\吴典\Desktop) like that
Title: Re: analogWrite Arduino DAC1 output like pwm digitalWrite
Post by: wdclot1 on Oct 11, 2016, 08:19 pm
Sorry, I have tried it just now, but it's not useful, then the output in oscialloscope are very small. A proportion reduce to 255/4096.
Title: Re: analogWrite Arduino DAC1 output like pwm digitalWrite
Post by: vaj4088 on Oct 11, 2016, 08:58 pm
I do not have a Due so I cannot comment directly.

The documentation at
https://www.arduino.cc/en/Reference/AnalogWrite (https://www.arduino.cc/en/Reference/AnalogWrite)
in the third paragraph says

Quote
The Arduino Due supports analogWrite() on pins 2 through 13, plus pins DAC0 and DAC1. Unlike the PWM pins, DAC0 and DAC1 are Digital to Analog converters, and act as true analog outputs.
Title: Re: analogWrite Arduino DAC1 output like pwm digitalWrite
Post by: wdclot1 on Oct 11, 2016, 11:03 pm
But in my code, the analogWrite(DAC1, Output)still output signal like PWM, some pulses
Title: Re: analogWrite Arduino DAC1 output like pwm digitalWrite
Post by: wdclot1 on Oct 12, 2016, 06:28 pm
anyone can help me?
Title: Re: analogWrite Arduino DAC1 output like pwm digitalWrite
Post by: spycatcher2k on Oct 12, 2016, 07:20 pm
Write a sketch to output 128 to the port in setup, and measure with your oscilloscope to see if the issue stays.
Title: Re: analogWrite Arduino DAC1 output like pwm digitalWrite
Post by: dlloyd on Dec 05, 2016, 04:23 pm
Note that the voltage range on the DAC output is 0.55V to 2.75V for 0 to 4095 in code. (1/6 to 5/6 of Vcc)