How to get 5V analog output on MEGA 2560?

I have a MEGA 2560. I would like to modulate the voltage coming out of the PWM pins with a maximum of 5V. Various articles I've read says that the default output on a pin is 5V since this is a 5V capable board. However, these PWM pins seem to limited to ~3.3V as I set them to analogWrite(FanPin, 255) and digitalWrite(FanPin, HIGH) and only got 3.2-3.4 V.
I am powering it by both the USB and a 12V adapter in the external power jack.

I'm new to Arduino stuff, so I'm probably unaware of something.

1 Like

Need to see your wiring diagram - have you other stuff connected ? How are you measuring the voltage ? And your sketch - something is wrong but it’s not obvious

GobblesKB:
I have a MEGA 2560. I would like to modulate the voltage coming out of the PWM pins with a maximum of 5V. Various articles I've read says that the default output on a pin is 5V since this is a 5V capable board. However, these PWM pins seem to limited to ~3.3V as I set them to analogWrite(FanPin, 255) and digitalWrite(FanPin, HIGH) and only got 3.2-3.4 V.
I am powering it by both the USB and a 12V adapter in the external power jack.

I'm new to Arduino stuff, so I'm probably unaware of something.

And you are measuring the voltage of a square wave using a voltmeter instead of an oscilloscope. Right?

Paul

Paul_KD7HB:
And you are measuring the voltage of a square wave using a voltmeter instead of an oscilloscope. Right?

Paul

OP mentioned digitalWrite

is it Arduino Mega 2560, or some other ATmega 2560 board?

How much current are you pulling from the pin?

hammy:
Need to see your wiring diagram - have you other stuff connected ? How are you measuring the voltage ? And your sketch - something is wrong but it’s not obvious

Attached picture of wiring setup. There's a 12V power supply to the board in addition to USB. Vin is powering another device at 12V. Pin 5 (on the PWM side) is an output going to the red (+) terminal on the fan, black (-) terminal is hooked up to breadboard bar wired to the arduino's ground.

I'm using a multimeter set to measure DC voltage and the square wave. I am placing it at the exposed yellow wire and the black wire at the base of the fan. It measures 3.23V for DC and square wave is just -000.

Sketch:

const int fan1Pin = 5;   //Output pin for the fan.  This pin is PWM enabled for analog output 0-255

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(fan1Pin, OUTPUT);
  delay(2000);
}

void loop() {
  // put your main code here, to run repeatedly:
  analogWrite(fan1Pin, 250);  //Should be linear relation between 0-5V and 0-255 analog output.
}

Attached picture of wiring setup.

Where?

Edit OK now it has appeared since I posted this. Here it is for others:-

Please read this:-
How to use this forum
about attaching files and image guide for making them show up in posts.

Pin 5 (on the PWM side) is an output going to the red (+) terminal on the fan, black (-) terminal is hooked up to breadboard bar wired to the arduino's ground.

You can not run a fan off the raw output of an Arduino pin. It needs to be switched through a bi-polar transistor or an FET. What you have done could damage the Arduino's pin because you are asking it to supply too much current. When you connect it to a circuit demanding more than 40mA you start to damage that pin.

Oh yeah, there's no way you can power a 12v fan that typically uses that type of connector via an Arduino I/O pin, even with 5v, it would draw far too much current (which is what is happening, that's why the pin isn't able to provide a whole 5v). Stop doing that before you burn out the pin driver.

You need to use a transistor to switch high current loads. If you used an N-channel MOSFET on the low side of the fan, and put the high side onto +12v, you could run the fan at a full 12v (I don't think all of those fans will even spin at 5v)

If you got a computer fan with PWM speed control (the 4-pin ones) the extra pin is for PWM speed control, and you can put 12v across the fan and use 5v PWM on the PWM speed control pin, and then you don't need additional components.

Correction: Vin is only supplying 4V when it's supposed to be supplying 12V.

DrAzzy:
Oh yeah, there's no way you can power a 12v fan that typically uses that type of connector via an Arduino I/O pin, even with 5v, it would draw far too much current (which is what is happening, that's why the pin isn't able to provide a whole 5v). Stop doing that before you burn out the pin driver.

You need to use a transistor to switch high current loads. If you used an N-channel MOSFET on the low side of the fan, and put the high side onto +12v, you could run the fan at a full 12v (I don't think all of those fans will even spin at 5v)

If you got a computer fan with PWM speed control (the 4-pin ones) the extra pin is for PWM speed control, and you can put 12v across the fan and use 5v PWM on the PWM speed control pin, and then you don't need additional components.

It's not a 12V fan, it's a single 5V fan?
The tiny fan I'm using has 3 pins, one of which is just a useless signal pin that just notifies a computer that it's working - the third pin (signal) on the fan is not connected to anything. Modulating voltage should modulate the speed (and therefore volume of air) of the fan?

I am literally following setup guides that show this exact same wiring diagram and code. Why is the Arduino trying to fry itself by supplying more current to the device than it's supposed to? What kind of transistor are you telling me to buy - 12V 40 mA and 5 V mA?

The other device is 12V. Why is the Vin pin not supplying 12V? Do I need to put an extraneous transistor in that as well? I don't see how limiting the current the Arduino supplies changes the voltage it's supposed to output.

I am literally following setup guides that show this exact same wiring diagram and code.

What guides please post a link.

Why is the Arduino trying to fry itself by supplying more current to the device than it's supposed to?

No it is the fan trying to fry the Arduino. The Arduino supplies 5V and the fan presents a load, or resistance. The Fan is too much of a load for the Arduin's capability.

I don't see how limiting the current the Arduino supplies changes the voltage it's supposed to output.

It doesn't matter what you see, what matters are the laws of physics. That is what happens when more current is demanded that a source can supply, the voltage drops. It is all to do with the impedance of the supply acting like a series resistor. This link is for the Raspberry Pi but the principals applies equally as well to the Arduino. http://www.thebox.myzen.co.uk/Raspberry/Understanding_Outputs.html

You said yourself:-

I'm new to Arduino stuff, so I'm probably unaware of something.

We are trying to tell you that something.

I have done a bit of digging and found out that the fan you are trying to use takes 0.16amp, that is 160mA which is four times the current that starts to damage the Arduino's pin.

This is the link to the data:- FAN-EC2510M05CA | asrg