analogWrite DAC only once for each pinMode( ,OUTPUT)

Had trouble getting a little DAC demo running and finally found that I have to set the mode to output again before each analogWrite otherwise the DAC doesn’t get the new value. (Photon)

void loop()
{
      for (int degree = 0; degree < 360; degree++) {
        dacValue = dacBias + 
           (sinLookup10k(degree) * dacPeakValue / 10000);
        pinMode(dacPin, OUTPUT); // ?? works now
        analogWrite(dacPin, dacValue);
      }
}

@noter, thanks for this input!

I’ve just confirmed your finding with this little test code

uint16_t i;
void setup() 
{ 
  pinMode(DAC, OUTPUT);    
}

void loop() 
{
  pinMode(DAC, OUTPUT);    
  analogWrite(DAC, (i+=10) % 4096);
}

which results in this

while commenting the pinMode(DAC, OUTPUT); line in loop() results in this

Maybe @satishgn and/or @mdma want to have a look at this, since this is at least unexpected behaviour :wink:


I’ve just opened an issue on the open source repo for the docs and firmware.

2 Likes

Hi

I have seen the issue with pinMode(DAC1, 4095) before each analogWrite and I have included this in some simple code
but do not measure any voltage output on DAC1. Does there need to be an external resistor (1k) to Gnd

This is the code but it does not work

Any advice?

Sorry and the code

int led = D7;

void setup() {
      pinMode(DAC1, OUTPUT);
      pinMode(D7, OUTPUT);
      digitalWrite(led, LOW);
}

void loop() {
    analogWrite(DAC1, 4095);
    digitalWrite(led, HIGH);
    
    delay(5000);
    pinMode(DAC1, OUTPUT);
    
    analogWrite(DAC1, 0);
    digitalWrite(led, LOW);
    delay(5000);
}

Just to make sure, when you’re using DAC1 you do measure on the pin that’s labled DAC (and not A3 which would be DAC2)?

Just try - as mentioned above - this

void loop() 
{
  pinMode(DAC1, OUTPUT);  // make sure to do it over and over again
  analogWrite(DAC1, 4095);
  digitalWrite(led, HIGH);

  delay(5000);
  pinMode(DAC1, OUTPUT);
  analogWrite(DAC1, 0);

  digitalWrite(led, LOW);
  delay(5000);
}

And this isn’t the conventional way to do it :wink:

1 Like

Hooray!!!

working Thanks for the help

2 Likes

Hey, I have similar kind of trouble with my photon. I can not use analogWrite() in my code, but it works with the tinker app. I’ve copied the code above and tried without success, any ideas?

There is an open issue about a regression in analogWrite()
When you use (or I guess used) Tinker this was still running on a previous system version and with a recent flash you got an auto-update causing these troubles.
You might have to downgrade or wait for the fix to be released.

How long would it normally take to get these kinds of issues resolved?

That depends on the severeness of the underlying problem and the priority in the issues list.
Hard to say, but I’ve pinged Particles firmware genious to have another look.

You can always look at this
https://github.com/spark/firmware/milestones

Hey thanks for clarifying this. How can I downgrade? simply changing the target version in device section on build.particle.io didn’t help.

Yes, unfortunately the auto-downgrade does not work.
But you can do it via CLI, when you follow these upgrade instructions in revers order
https://github.com/spark/firmware/releases/tag/v0.4.5

Or offline follow the DFU-UTIL instructions
Download the Photon files (bottom of the linked page)
Then flash part2
After this part1

Thanks, this worked for me. I could install my firmware with build target 0.4.5 via build.particle.io. Is there a possibility to set the build target in particle dev as well? It looks like it’s auto updating my device again once I try to flash via the Dev IDE.

I don’t think there is a way to set the target system version in Particle Dev, but I also think that Particle Dev does not cause an auto-update either, which might result in your application firmware not being able to run on your device if it’s not updated.
This is a muddle which I usually circumvent either by building locally or using Web IDE (Particle Build).

But maybe @suda got more background on that for you

I have this problem as well, but since I’m running vista, I can’t install particle-cli. Any advise?

The ide won’t update the firmware. Just click on devices icon (2nd from the bottom on the left), select your photon and in the details, select against which firmware you compile the code (0.4.5 instead of Latest).

I tried selecting 0.4.5, but no luck. It still is outputting 3.15V

You’ll need to manually revert to older firmware by flashing part2 first then part1 from the 0.4.5 github releases page on the firmware repo. https://github.com/spark/firmware/releases/tag/v0.4.5

wow… that was a lot of work. But it seems to be working now!!

Thanks.

does 0.4.7 have this fixed?