With a very simple connection of an LED (which can withstand 5V) to ground and to a pin on my Galileo gen2, I can never output a non-zero voltage from the pin via analogWrite
, regardless of whether the pin is digital or analog (although PWM digital works)
The LED is in the correct bias and I'm calling pinMode(pin, OUTPUT)
before attempting to call analogWrite(pin, 255)
(or any 2nd parameter in-between), even though it's not even necessary.
The LED glows if I call digitalWrite(pin, HIGH)
on both analog and digital pins, but does not glow at all for analogWrite(pin, 255)
with the same configuration and pins, nor does it for any 2nd param in [0, 255]. This holds for whether the call is attempted in loop()
or setup()
.
The only possible way I can get the LED to receive any power from analogWrite
is through a digital PWM pin, but I know the other digital pins can still produce square-waves with timers and the analog pins should certainly be able to use PWM.
(So far, I've been to scared of short-circuits to connect the analogWrite output straight into an analog input to read the voltage)
Is there something I don't understand about the underlying board, or am I experiencing some strange malfunction?
Thanks,
Tyson
3 Answers 3
On most boards, the standard implementation of Arduino's analogWrite()
will only ever produce PWM, and only on a specific set of (digital) pins. These are usually pins 3, 5, 6, 9, 10, and 11, although it's slightly different on some boards.
The only official board which has 'true' analog output is the Due (pins DAC0 and DAC1).
If you want any other behaviour (such as outputting PWM on non-standard pins), you'll either need to modify the Arduino implementation, or directly manipulate the microcontroller's special registers. Note though that modifying the chip's configuration could cause other standard Arduino functions not to work as expected.
-
So the analog pins don't support PWM (they're only titled such because they can read analog)? What about the use of timers to produce square-waves which emulate analog as discussed here: arduino.stackexchange.com/questions/8357/…Anti Earth– Anti Earth2015年02月02日 02:15:05 +00:00Commented Feb 2, 2015 at 2:15
-
@AntiEarth Yes, analog pins are for reading analog only. For square wave output, the MC's timers can usually be routed to any GPIO pin in theory. The
tone()
function can output to any pin, for example, but it comes with its own limitations. It's important to note that Arduino is only designed to support a subset of the underlying MC's capabilities. The official Arduino website documents all of this if you need to look anything up.Peter Bloomfield– Peter Bloomfield2015年02月02日 02:31:12 +00:00Commented Feb 2, 2015 at 2:31 -
So to clarify/summarize: only digital PWM pins can actually produce square waves upon calling
analogWrite
. The other pins (normal & analog) can achieve square waves using timers, but this is not implemented in theanalogWrite
function, nor in the Servo definition. Have I understood correctly?Anti Earth– Anti Earth2015年02月02日 08:09:54 +00:00Commented Feb 2, 2015 at 8:09 -
@AntiEarth You're right about everything except the
Servo
library. It can work with any pin because it doesn't useanalogWrite()
. It manipulates the timers directly. This unfortunately means it can prevent PWM from working in some cases, so you have to be careful about how you use it.Peter Bloomfield– Peter Bloomfield2015年02月02日 09:27:45 +00:00Commented Feb 2, 2015 at 9:27 -
That is exactly opposite what I've discovered experimentally; simple calls to the Servo functions only work on digital PWM pins on my Galileo (gen2); they absolutely do not work on other digital pins or analog pins. The mystery continues!Anti Earth– Anti Earth2015年02月02日 10:13:22 +00:00Commented Feb 2, 2015 at 10:13
I don't believe Galileo has a D/A converter, which is what it sounds like you're expecting. In the Arduino 'eco-system', analogWrite() sets a PWM duty cycle - that's kinda-sorta somewhat like an analog output, but not really. You could add external circuitry to low-pass filter the PWM and produce some sort of proportional DC but it might be a good trick get it clean enough for whatever you plan to put downstream.
-
As discussed here arduino.stackexchange.com/questions/8357/…, I'm just trying to emulate an analog signal by digital square-wavesAnti Earth– Anti Earth2015年02月02日 01:46:43 +00:00Commented Feb 2, 2015 at 1:46
I am not sure where you are getting your LEDs but the ones I purchase do not survive 5 volts. You need to put a resistor in series with then, something in the 220 to 510 ohm range. When you get some resistors test your LEDs you may have damaged them as well. A good indication of this if there color if obviously off and or dim. If the timers are set up correctly the digitalwrite will set the PWM value, somewhere from 8 to 16 bits depending on the Arduino and timer used. This PWM output will appear to control the brightness of the LED. It does this by simply turning it on and off at a speed the eyes cannot follow. the longer it is on the brighter it is. Hopefully this will help you get your arms around the circuit.
analogWrite
doesn't make the necessary use of timers to simulate a square-wave, unlike the functiontone
. But why wouldn't analog pins write analog?!analogWrite
as binarydigitalWrite
s, thresholding past 255/2 (slightly different to always being 0 on my Galileo)