2

Can we write a analog voltage to analog pins A0-A7 of the Arduino Nano (for example)?

As in:

pinMode(A3, OUTPUT);
analogWrite(A3, 200); 
5

No. The analogWrite(pin, val) function is reserved to PWM pins (D3, D5, D6, D9, D10, and D11 in Arduino Nano). Pins marked as "ANALOG IN" on the board can work either as analog input (to the Analog to Digital Converter), digital input, or digital output.

It is worth to note that the Arduino Nano (and any other Arduino board I'm aware of) actually doesn't have any 'true' analog output port. When you use analogWrite(pin, val) you are actually telling the Arduino to output a PWM signal with the duty cicle specified by val (100% for val = 255 and 0% for val = 0). If you actually do need a true analog output, you should consider using any form of analog lowpass filter at the specified PWM output port. Depending on how clean you want your signal to be, you could use a single capacitor or any other (a little more complex) filter.

If you don't know anything about filters, this could be a good starting point: ElectronicsTutorials - Passive Low Pass Filter

More insight about PWM in Arduino, here: EAS 199 - Basic Pulse Width Modulation

  • Note that the Due does have two true analog output pins (called DAC0 and DAC1), in addition to a dozen PWM pins. – Edgar Bonet Jun 27 '18 at 14:29
1

analogWrite are PWM digital pins. Analog pins on Nano are ADC read/input only (or digital I/O except A6 and A7).

1

All microcontrollers including Arduino are digital, and rely on specific circuit hardware in order to interface with the analog world. Two of these relate to your question:

  • Analog-to-digital converters (ADCs) sample an analog voltage and output a number representing the voltage relative to the measurement range - such as 0 to 1023 on a 10-bit ADC.
  • Digital-to-analog converters (DACs) do the reverse, where a digital number from the microcontroller is used to output an analog voltage, e.g. between ground and the supply voltage.

While ADCs and DACs convert analog-to-digital and vice versa, they need different circuits. In the case of Arduino boards and other microcontrollers, analog input (read) and output (write) must use different pins. Analog input pins cannot be used as outputs, and vice-versa.

The exception is where you want to produce an analog output via PWM (and a smoothing capacitor). In that case the answer is yes, you can use pins A0-A7. However, you will need to bit-bang and won't be able to use the timer hardware. It's much easier to use the pins dedicated to DAC or analogwrite if that's an option for you.

0

You can create your own PWM on non-PWM pins using Blink Without Delay techniques. Use the micros() timer and check it every pass thru loop() to if it is time to change the state of the output.

This will create 50% duty cycle, with frequency = 1/(2 x halfPeriod)

void loop(){
  currentMicros = micros(); // capture the current 'time'
  elapsedMicros = currentMicros - previousMicros;
  if (elapsedMicros >= halfPeriod){
    previousMicros = previousMicros + halfPeriod; // set the 'time' of the next transition
  }
}

all time related elements are of type unsigned long

Then filter with a RC Lowpass filter (Resistor and a capacitor) to smooth it out so it's more like an analog level.

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.