Digital Voltage Output has decay

I was given an Arduino Uno for a project; I'm trying to output a continuous 5V from one of the pins. My code is below.

The problem is, when I hook up the Arduino to an oscilloscope, I get a voltage that decays rapidly -- within 300 ms -- while I need the signal to last for approximately 4 seconds.

Why is this happening? I already checked other forum posts; this occurs whether the scope is coupled in DC or AC mode.

(Also, two other questions that came up as I was trying this: 1. Is there a difference between analogWrite() and digitalWrite()? They both seem to do the same thing. 2. Ultimately the goal of the project is to load this code onto a friend's Arduino which has a CNC Shield. The pin numbers don't seem to be the same on the shield, so how do you find out which pin to set as the output?)

Thanks everyone! I'm really new to this but really trying to get this to work!

const int pwmPin = 8; // Analog output pin that the LED is attached to
int outValue = 255;        // value output to the PWM ( in analogWrite() )

int t = 4000; //period of time to output 5V

void setup() {
  Serial.begin(9600); // initialize serial communications at 9600 bps:
  pinMode(pwmPin, OUTPUT);
}

void loop() {
  outValue = 255;
  //analogWrite(pwmPin, outValue); //I stopped using this in favor of digitalWrite(), but they seem to do the same thing.
  digitalWrite(pwmPin, HIGH);

  Serial.println("output = " + outValue); // print the results to the Serial Monitor:

  delay(t); // Wait t milliseconds

  // After the time has passed, stop outputting 5V
  outValue = 0;
  //analogWrite(pwmPin, outValue);
  digitalWrite(pwmPin, LOW);
  
  Serial.println("output = " + outValue);

  // wait
  delay(t);
}

That should not be happening. There is something wrong with how you are measuring it or the power supply for the arduino.

  1. When used on pins that support PWM output, analogWrite() will output PWM (though analogWrite(pin,0) will just do digitalWrite(pin,LOW), and writing 255 will digitalWrite() the pin HIGH).

Don't use analogWrite() when you don't want PWM - it will confuse others reading the code and yourself when you come back to the code in a few months for some reason.

  1. Examine the design of the shield - generally the pin numbers on the shield should match the arduino pin numbers, but you may have a weird shield with it's own numbering scheme or something. This depends on which of the dozens of CNC shields you have.

That clears a lot of this up. Thank you!

I am getting that issue still; I'll have to try maybe with another Arduino and/or scope. Just to verify I'm not doing anything wrong with the setup, though.... Do I need to connect a power supply to the Arduino, or should the USB connection to my PC be enough to supply the 5V? (I know you need the power supply if you aren't using USB connection, and want to run the Arduino independently, but is there ever a time where USB isn't able to provide enough power?

Thanks!

The USB power is fine as long as you're not trying to power a bunch of other stuff.

this occurs whether the scope is coupled in DC or AC mode.

Is the 'scope working correctly if you probe the 5V pin?

Or, do you have a multimeter? Or an LED+resistor?

Is there anything else connected to the output pin besides the 'scope?

Have you got the oscilloscope ground connected to one of the the Arduino GND pins?

If you are using proper oscilloscope probes this is often done by clipping the short lead with an alligator clip to the Arduino Ground pin.

These short leads are often detachable from the probes and not always put to use.

You don't need one for every probe, but at least one has to be used, or an alternative method such as a wire to the ground terminal usually found on the front panel of the oscilloscope.

The scope vertical input must be set for DC coupling in order for the trace to remain at a constant vertical position. Looking at a pure DC signal, like a digital output (not PWM) with AC coupling will result in the trace always returning to the zero voltage vertical position.

So...

I do have DC coupling enabled. I do have the ground of the scope connected to the Arduino's GND port.

I do not have anything between the scope probe and the pin output. There is no LED, resistor, nothing. I tried putting a decade resistor box in series from 9 --> resistor --> scope, but this just made the signal noisy and didn't change the delay time.

I was thinking this was the problem, but the scope has an internal impedance and, from what I can tell, it shouldn't kill the voltage easily.

Just as a basic sanity check I would test the output voltage using a multimeter and see what you get from that. Also you should definitely see what the scope reads when you test the 5V pin of the Arduino.