Hello everybody

I’d like to light a LED with an analog output (all my digital pins are used)
My code is as follows but no value is passed to the outside

someone he had this problem ?

Thank you in advance

void setup()
  pinMode(DAC1, OUTPUT);    // sets pin as output

void loop()
  analogWrite(DAC1, 4095); // sets the LED on
  delay(500);              // waits for 500mS
  analogWrite(DAC1, 0); // sets the LED on  // sets the LED off
  delay(500);              // waits for 500mS

Are you measuring the output voltage directly on the DAC pin, without anything else connected?
I think the STM32F2 DAC, with output buffer enabled, requires 5k Ohm minimum resistive load.

I’ve just made some DAC experiments and found that the output buffer was not enabled, even though looking at the code it seems it should be.
That could explain why the DAC can’t drive your LEDs. Or did you put too small a resistor in series?

Do you get the same behavior if you use pin A7 ?

Thanks for your replies.
@mars yes i had measured the output voltage directly on the DAC pin. I tried with a 5kOhm resistive load and nothing is happening. ( it must put the resistive load between the dac pin and the ground, or in series with the LED ?)
@Rockvole yes i get the same if i use A7 pin or an other one.

I do not understand why but now when I directly measure the DAC1 pin I have 3.3V output
Unfortunately when I feed the photon by the Vin pin with 5v voltage, the DAC1 output will stop responding. my code works only with the power from the USB cable. someone has an explanation for that ?

What output do you expect to see on the DAC1 pin?

This would suggest that your 5V source does not provide the needed quality DC.
Maybe use a different one, or add some proper filtering.

i would like a 3.3V output or 0V, to switch on/off leds. all my digital pins are used and i need one more

From what I can tell, the internal DAC output buffer is not enabled, which means that there will be 2x 5k resistance and very little drive capacity on the DAC output.

Can anyone else confirm this? With buffer you should be able to drive a 5k load with no problem.

@Sherkann in your case you would connect the DAC to your LED, to the resistor, to ground (ie resistor in series). 5k is probably a bit much depending on LED, you could try 1k. For driving LEDs though you really want a current source, not a voltage source like the DAC. You will get better results with PWM.

@Sherkann that’s easy, just use digitalWrite() instead!


@mars is correct, all A pins and also WKP, DAC, RX and TX can be used as digital pins too.

it’s true , simple digitalWrite() works
I’m confused, not having thought.
Thank you very all

Reading the code, I think the output buffer is enabled (note that “0” means enable and “1” means disable according the STM32F205 reference manual.

See /hal/src/stm32f2xx/dac_hal.c#L61 and /platform/MCU/STM32F1xx/STM32_StdPeriph_Driver/inc/stm32f10x_dac.h#L187

for details.

1 Like

I was looking at that and it clearly says the buffer is enabled, but using the analogWrite() method I get very different results from when I call the low-level code directly.
I’m not sure if it could be undone by DAC_Cmd(DAC_Channel_x, ENABLE); on L90 and L97, which I think belong in HAL_DAC_Init().
I’d like to test but my current setup is far from vanilla photon.

No idea if this has anything to do with this, but currently it seems that you have to set pinMode(DAC, OUTPUT); everytime before using analogWrite(DAC, x);.
This has been found and doced a while back

NOTE: While for PWM pins one single call to pinMode(pin, OUTPUT); sets the pin mode for multiple analogWrite(pin, value); calls, for DAC pins you need to set pinMode(DAC, OUTPUT); each time anew you want to perform an analogWrite().

analogWrite DAC only once for each pinMode( ,OUTPUT)

1 Like

I have been having the same issue. Just to test, I placed an LED on pin A0 and tried running through multiple analog values. None of them worked. As soon as I did a digitalWrite(A0, HIGH), it lit up perfectly. Meanwhile, analogWrite(A0, 4095) does nothing. Neither does any other value passed in to analogWrite.

What I did was:

int led = A0; 

void setup() {

void loop() {
    analogWrite(led, 4095);
    analogWrite(led, 2048);
    analogWrite(led, 1024);
    analogWrite(led, 512);
    analogWrite(led, 0);

Unless I’m mistaken:

A0 - A2 only support analogread, digitalread and digitalwrite. NOT analogwrite
A3 & A6 are the only pins that can take values up to 4095
A4, A5 & A7 can anly take values up to 255

1 Like

Hi @kaizoku0506

@justinmy is correct–here is a link to the doc:

What happens if you change your code to

int led = DAC;

and connect to the DAC (A6) pin?

Also the docs for analogWrite() provide a complete list of compatible pins