Arduino guru - digital write question

Guess this fits this category…

Looking for an Arduino guru to help me out with something. I’ve written a small Arduino program that is supposed to drive a solid state relay on a brewing rig.

It’s got an LCD shield over the top with 4 buttons to adjust the ‘power’ of the heater. Essentially, I’m running a 2 second cycle that has an on segment for x% and an off segment with (1-x)%.

Simple enough… got it to run exactly as expected when I have the ‘driven pin’ as pin 13… the onboard LED.
at 0%, it’s always off, at 100% it’s always on, and at any % between it is correspondingly on then off.

HOWEVER…

Now that I’m ready to drive the external SSR, I’ve soldered 2 wires in, the first to Digital Pin 02, and the second to one of the three +5V pinouts.

When I initialize the program, I have it starting at 0% by a digitalwrite(Pin02, LOW)

However, my multimeter shows an output of 2.75V.

I can do the same thing and initialize it to HIGH, but it still shows the 2.75V.

NOW THE CRAZY PART:

As soon as I move it away from 100% on or off, it works just fine; the voltage briefly jumps to 5V and then 0.

When I go back to 100* or 0% it just goes back to the 2.75V

What am I missing to get the initialization to to be 0 or 5V?

Did you forget to call pinMode?

nope, I have

  • pinMode(DrivenPin, OUTPUT);

as the first line of the void setup() section

Taking it back to basics, I’ve found that the HIGH and LOW have exactly the opposite effect for the +5V / Digital Pin 2.

On a sketch started from scratch, if I write Digital Pin 2 to HIGH, it gets 0 volts, and if I write it to LOW, I get +5 Volts. Still not sure of the logic here, I’ll have to go back and check my Arduino 101 again for this one

But I still haven’t figured out why on my other program it sits at 2.75 when I have it set for 100 or 0%

The back to basics sketch:
///////////////////////////////////////////////////////
int DrivenPin = 2
;

void setup() {
pinMode(DrivenPin, OUTPUT); //
digitalWrite(DrivenPin, LOW);

}

void loop() {
if (millis()<300)
{digitalWrite(DrivenPin, HIGH);
delay (10000);
}
digitalWrite(DrivenPin, LOW); // sets the digital pin 13 +5 VOLTS
delay(5000); // waits for 5 seconds
digitalWrite(DrivenPin, HIGH); // sets the digital pin 13 0 Volts
delay(2000); // waits for 2 seconds

}

Board?​​​​​​​​​​​​​​​​​​​​​​​

“const” would be a good addition…

const int DrivenPin = 2;

2 Likes

Ugh… like most programming issues, it all comes down to the placement of a single bracket…

Had an IF loop that I accidentally closed before it should have been.

But thanks for the tips on adding CONST to my constants… always like to be efficient.

As far as the revers effect of the On / Off states, I just added another pair of constants at the front called PowerON and PowerOFF and can easily switch them between HIGH and LOW as needed. Also made my code easier to read!

Thanks for the help Brian!

3 Likes

For pin definitions, using #define is handy. The compiler essentially does a find-and-replace on them, so no additional memory is used to store a variable that will never change during runtime.

#define PIN_OUTPUT 2

void setup() {
    pinMode(PIN_OUTPUT, OUTPUT);
}

how is that different than setting it as a constant as Brian had said?

const int PIN_OUTPUT = 2

Is one more programatically efficient than the other?

-Ian

For use with pinMode / digitalWrite, it is no different. (Ignoring the usual caveats of #define.)

In general #define is worse because it lacks type.

…is also true for typed constants. You need to have more faith in the optimizer.

1 Like