Can I use A4 in output while using D2 in PWM

I use A4 to power an LED and want to use D2 as PWM output. Is that possible?

@harley, this is possible.

Do you mean drive an LED through a suitable current-limiting resistor using A4 as a digital output using pinMode(A4, OUTPUT)?

I’m sorry. I didn’t mean for that code segment to send without my comments. The battery level is checked on pin A1. Depending on the value, pins A4 or A5 is set by doing a digitalWrite(A4, HIGH) or digitalWrite(A5,HIGH). So, there may be a conflict between A4/A5 and D1/D2, RIGHT??

#define numEyesInSystem 3
int redLED = A4;
int greenLED = A5;
int batteryLevelPin = A1;
int emitterOnValue = 128;
int emitterOffValue = 0;
int eyeArray[numEyesInSystem] = { D1, D0, D2 };
void setup() {
    pinMode(redLED, OUTPUT);
    pinMode(greenLED, OUTPUT);
    analogRead(batteryLevelPin); // this puts the battery level pin in analog input mode
    for ( int i = 0; i < numEyesInSystem; i ++ ) {
        pinMode(eyeArray[i],OUTPUT);
        analogWrite(eyeArray[i],emitterOnValue,38000);
        analogWrite(eyeArray[i],emitterOffValue,38000);
        Particle.process();
    }
    digitalWrite(greenLED, LOW);
    digitalWrite(redLED, HIGH);
}
(void) loop {
    analogWrite(eyeArray[0],emitterOnValue,38000);
    analogWrite(eyeArray[1],emitterOnValue,38000);
    analogWrite(eyeArray[2],emitterOnValue,38000);
    delayMicroseconds((unsigned int)85);
    analogWrite(eyeArray[0],emitterOffValue,38000);
    analogWrite(eyeArray[1],emitterOffValue,38000);
    analogWrite(eyeArray[2],emitterOffValue,38000);
    delayMicroseconds((unsigned int)400);

    Particle.process();
}

You only get a conflict if you are using both as PWM pins. In your case you use A5 as GPIO and D2 as PWM. There is no conflict with A4 and since you are using the same frequency for all PWM output, there are not conflicts there either.

Just a note. You don't need to cast a constant they way you are doing here: delayMicroseconds((unsigned int)400); Instead, you can specify a UL or Unsigned Long qualifier like this: delayMicroseconds(400UL); However, the compiler is smart enough to know the variable type required and cast the constant to the correct type anyway.

Also, this: (void) loop { should be void loop() {. Your loop() code will run very fast, likely need 1000Hz. Is this what you intended?

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