Problem with Arduino nano analog pins

for an issue there are 6 leds needed tobe switched on/off via analog pins !
i guessed that would be really simple :grinning:

but 2 off the 6 leds are Not enlightened !
that are on A6 and A7 (A4,A5 are used for I2C)
have checked all leds electrically on connections and lighting, all ok !
whats wrong with the code ?

/*
  check leds
*/
  
#define TRAFFIC_LIGHT_SPEED 500

const int led_pins_list[6] = {A0,A1,A2,A3,A6,A7};
// the setup function runs once when you press reset or power the board
void setup()
{
  Serial.begin(9600);
  // initialize digital pin LED_BUILTIN as an output.
  for(int i = 0;i < 6;i++)
  {
    pinMode(led_pins_list[i], OUTPUT);
  }
}

// the loop function runs over and over again forever
void loop()
{
  for(int i = 0;i < 6;i++)
  {
    analogWrite(led_pins_list[i], 255);   // turn the LED on (HIGH is the voltage level)
    delay(TRAFFIC_LIGHT_SPEED);                       // wait for a second
    analogWrite(led_pins_list[i], 0);    // turn the LED off by making the voltage LOW
    delay(TRAFFIC_LIGHT_SPEED);                       // wait for a second    
  }
}

any idea ?

First see this:

image

Source: https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/

Second:

  • These pins are named "Analog" pins but only for reading!() analog values.
  • For analogWrite() you have to use the pins which are dedicated for PWM (Pulse Width Modulation).

See here: https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

and the pinout of a NANO

https://docs.arduino.cc/resources/pinouts/A000005-full-pinout.pdf

PWM pins are marked by "~" sign:

image

For a simple "switchOn/Off" you can use

  digitalWrite(pin, HIGH);  // set to HIGH level
  digitalWrite(pin, LOW); // set to LOW level

Good luck!
ec2021

1 Like

Your sketch does not test the PWM to dim the leds.
I tried your sketch with the PWM pins in Wokwi simulation:

thanks for your info..
so i have to look for a different solution..
may be a pwm-expander over i2c i have used for servos successfully
have read in this forum that can be used for leds too..

Some PWM chips are for leds and for servo motors.
There are also libraries that can create PWM on any pin (but not on A6 and A7).
Those libraries run internally at a high frequency, it might interfere with other timing things.

  1. https://github.com/bhagman/SoftPWM
  2. https://github.com/Palatis/arduino-softpwm

I used one of those, and it worked good enough, but I forgot which one I have used.

Here is a test with the first one:

Your profile suggests that you are happy with a link in German ...

https://botland.de/led-dioden/10617-neopixel-ws2812b-5mm-led-5st-adafrucht-1938-5904422301002.html

If you use these diodes all six would require only one digital pin (as they can be chained) and the use of a WS2812 library and probably ease the wiring... ?!?

1 Like

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