1

I'm working on a bigger project, and needs to give out a voltages between 0 and 5V. I've written this code:

int ledPin = 12;      // LED connected to digital pin 

void setup(){
pinMode(ledPin, OUTPUT);   // sets the pin as output
}

void loop(){
analogWrite(ledPin, 255);
delay(2000);
analogWrite(ledPin, 0);
delay(2000);
analogWrite(ledPin, 127);
delay(2000);
analogWrite(ledPin, 128);
delay(2000);
}

If I've understood it right, this should first give about 5V, then 0V, then 2.5V and againg 2.5V. However my voltmeter only reads 5V and 0V, 2 seconds at each. I've attached my voltmeter to pin 12 and ground. The arduino board is also grounded.

  • 2
    You don't say what board you're using, but you may want to check that pin 12 is a PWM pin on your board. You may find the page on analogWrite() on the Arduino site useful. – sempaiscuba Apr 18 '18 at 11:47
  • on Uno pwm pins are marked with ~ – Juraj Apr 18 '18 at 11:54
3

The PWM pins output a square wave from 0 to 5V. If you want something more like a DC level, then run the signal thru a Resistor-Capacitor (RC) Lowpass filter. 1K and 10uF should work well to smooth the squarewave. http://sim.okawa-denshi.jp/en/CRtool.php

On an Uno, pins 3,5,6,9,10,11 are the PWM pins and output at a frequency of ~ 490 Hz or 980 Hz, with a duty cycle from 1 to 254. 0 is full off, 255 full on.

  • good info, but you shouldn't need a filter to satisfy a voltmeter, they are slow at math – dandavis Apr 19 '18 at 0:45
  • @dandavis - on the contrary, the opposite. DC voltmeters are not designed to measure changing signals; if you want to use one, you must first stabilize the signal such as with the proposed filter. Otherwise you need to use an instrument appropriate to a changing signal, ie, a scope. – Chris Stratton Apr 19 '18 at 14:43
  • i just meant that both a dmm and analog meter will show about the same thing with or without RC, not accurately display the peak, duty, etc... – dandavis Apr 19 '18 at 14:49
3

Most Arduino boards do not have true analog output that can produce voltage range. To check if a particular board has true analog output check the specs and look for pins labeled DAC or Digital-to-analog converter. Be aware that the supported voltage range will vary depending on the board.

The analogWrite() function uses Pulse Width Modulation to simulate analog output.

If you need true analog output you will need to attach a Digital to Analog Converter(DAC) to the arduino. e.g. https://learn.adafruit.com/mcp4725-12-bit-dac-tutorial

UPDATE: If your goal is to drive a motor with a SN754410 you should be able to use the PWM analog output to drive it at varying speeds. Here is an example: http://web.ics.purdue.edu/~fwinkler/616/sn754410_Arduino.pdf

  • Now it works! It helped using an PWN pin. But should it not also work with one of the analog pins? Or are these pins actually only analog in the reading prosess? – Erlend Salte Kallelid Apr 18 '18 at 20:40
  • A0-A5 are analog inputs only -- there is no true analog output on the UNO, only PWM. – Craig Apr 18 '18 at 20:47
  • Some ARM-based Arduinos (e.g. Due) have DACs on-board. – Ignacio Vazquez-Abrams Apr 19 '18 at 1:40

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.