0

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

  • (I could understand it not working on non-PWM digital pins, because perhaps analogWrite doesn't make the necessary use of timers to simulate a square-wave, unlike the function tone. But why wouldn't analog pins write analog?! – Anti Earth Feb 2 '15 at 1:31
  • When simulating on 123d.circuits.io, again only PWM digital pins work as expected, but analog and digital pins interpret analogWrite as binary digitalWrites, thresholding past 255/2 (slightly different to always being 0 on my Galileo) – Anti Earth Feb 2 '15 at 1:43
  • As per other answer: analogWrite ONLY works on hardware-PWM capable pins*. What you see is correct operation. * uC's with DAC may allow analog.write on DAC equipp3d pins. – Russell McMahon Feb 2 '15 at 3:44
1

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 Feb 2 '15 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 Feb 2 '15 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 the analogWrite function, nor in the Servo definition. Have I understood correctly? – Anti Earth Feb 2 '15 at 8:09
  • @AntiEarth You're right about everything except the Servo library. It can work with any pin because it doesn't use analogWrite(). 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 Feb 2 '15 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 Feb 2 '15 at 10:13
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.