Analog Output Problems with Analog Pins

Hi everyone,

I have quite a beginner-level question.
I want to control LEDs through an 'analogWrite(...)' function to dim them.
I did this with the digital pins '3' and '11' through the PWM wave.
This worked as intended at first, but since I am also using the tone() library for another part of this project this ran into problems where the LEDs did not work in combination with the speaker output. (refer: tone() and analogWrite what are the limits)

So I opted on using analog pins for the 2 LEDs since I thought that using analog pins for analog outputs seems perfect.
This works not as intended however.

Basic code looks like this:

const int r_eye = A1;


void setup() {
  pinMode(r_eye, OUTPUT);
  
  for(int i=0;i<255;i++){
    analogWrite(r_eye, i);
    delay(20);
  }
}

This turns the LED on as soon as the value reaches 255 with 4V. Everything below 255 results in no noticable voltage.

I feel really dumb since I cant figure out what I'm missing and where my error is right now.
I'm using a Arduino Nano with an ATmega328P.

Thanks for your help

analogWrite() is not an analog signal, and works only on certain pins (not analog input pins). Check the Arduino Reference for analogWrite().

Make sure you have a current limiting resistor in series with the LED.

PWM is a digital signal. It alternates between 0V and Vcc.

@jremington
Is it even possible then to use the analog pins for analog outputs?

I am using resistors in series with the LED.

The analog pins can be used for analog input, or digital input and output (but not the PWM output produced by analogWrite(), as that function uses timer modules).

1 Like

Thanks for your help. I guess I just assumed the wrong things then.

On a "regular Arduino" the analog-input pins don't support PWM ("analog" output). However, they CAN be configured for regular digital input or output.

(There are lots of Arduino variations that use different processor chips, etc., so that MIGHT not the be case with all of them.)

1 Like

Analog pins dont provide analog voltage outputs on most "traditional" "arduinos".
and you cant use "analogWrite" because that uses the timers for pwm.
However you CAN write your own "analogOut" routine using millis (or micros) timing to generate low speed PWM signals, provided you dont mess with timer 0.

@Bennnn
from the Reference about tone():
"Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega)."

But the controller has more PWM pins than only 3 & 11, you can use analogWrite() on any combination of 5, 6, 9 &10 pins on Uno/Nano without interference with tone()

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.