1

I am having a problem with an Arduino Mega 2560.

Everything works fine, the ultrasonic sensors work fine, and the other LEDs work fine. Although there is 2 LEDs who do not work right and when I do analogWrite(), they actually do digitalWrite(). I have tried to attach these two LEDs to a potentiometer (potentiometer works fine, values are tested) but it always does digitalWrite() not analogWrite().

I have tested the LEDs and potentiometer on an Arduino Uno in PWM pins and analog pin and yes it does work very well.

Please mind that these LEDs are attached to analog pins. Why is this? Because I do not have anymore PWM pins available to use and so I have to use analog pins. No way I can rearrange this.

Here is part of the code (only part otherwise it's too long, most of my pins are taken):

//Leds//

int led = A4;
int ledUV = A3;

//Potentiometer//

int potReading;
int potPin = A2;
int lightDim;


void setup(){

    Serial.begin(9600);

    pinMode(led, OUTPUT);
    pinMode(ledUV, OUTPUT);

    pinMode(potPin, INPUT);


}

void loop(){

    potReading = analogRead(potPin);
    lightDim = (255./1023.)*potReading;

    analogWrite(led, lightDim);
//  analogWrite(ledUV, lightDim);
//  Serial.println("pot: ");
//  Serial.println(potReading);
//  Serial.println(" ");
    Serial.println("dim: ");
    Serial.println(lightDim);
//  analogWrite(led, 255);
//  analogWrite(ledUV, 255);

}
1
  • 1
    < Why is this? "this" being what?
    – dannyf
    Jan 20, 2017 at 21:08

2 Answers 2

0

Reference: analogWrite()

The pins 2 - 13 and 44 - 46 are the PWM pins for analogWrite().

That means you have 15 PWM pins. Did you use them all?

You could add a module with more PWM outputs. Search for: Arduino 16 channel PWM

The Arduino Mega does not have analog outputs, only digital PWM outputs.

1
  • Thank you so much man it did work! I did not knew about 44-46!
    – The Law
    Jan 21, 2017 at 0:08
4

Analog pins an only read analog values, not write them.

analogWrite() is a deceptive name. It should really be PWMWrite() since all it does is PWM. And that, of course, needs PWM pins.

4
  • This is a good answer too, but arduino uno analog pins have anolog output, as this procedure and code worked on a uno.
    – The Law
    Jan 21, 2017 at 0:08
  • 1
    No they don't. There is no analog output on any atmega chips. Whatever you did before it certainly wasn't analog output.
    – Majenko
    Jan 21, 2017 at 0:38
  • Do you want me to post a video?
    – The Law
    Jan 21, 2017 at 3:30
  • 1
    I'd love to see that, yes. As well as the code that is doing it.
    – Majenko
    Jan 21, 2017 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.