Go Down

Topic: analogWrite Arduino DAC1 output like pwm digitalWrite (Read 299 times) previous topic - next topic

wdclot1

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:

}

spycatcher2k

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.
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design & Assembly work undertaken - Arduino repairs & testing - PM for details.

wdclot1

I have  some Arduino Due borads, so there is DAC1 pin

spycatcher2k

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
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design & Assembly work undertaken - Arduino repairs & testing - PM for details.


wdclot1

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.

vaj4088

I do not have a Due so I cannot comment directly.

The documentation at
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.

wdclot1

But in my code, the analogWrite(DAC1, Output)still output signal like PWM, some pulses


spycatcher2k

Write a sketch to output 128 to the port in setup, and measure with your oscilloscope to see if the issue stays.
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design & Assembly work undertaken - Arduino repairs & testing - PM for details.

dlloyd

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)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy