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.

asked Apr 18, 2018 at 11:36
2
  • 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. Commented Apr 18, 2018 at 11:47
  • on Uno pwm pins are marked with ~ Commented Apr 18, 2018 at 11:54

2 Answers 2

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.

answered Apr 18, 2018 at 13:19
3
  • good info, but you shouldn't need a filter to satisfy a voltmeter, they are slow at math Commented Apr 19, 2018 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. Commented Apr 19, 2018 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... Commented Apr 19, 2018 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

answered Apr 18, 2018 at 15:32
3
  • 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? Commented Apr 18, 2018 at 20:40
  • A0-A5 are analog inputs only -- there is no true analog output on the UNO, only PWM. Commented Apr 18, 2018 at 20:47
  • Some ARM-based Arduinos (e.g. Due) have DACs on-board. Commented Apr 19, 2018 at 1:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.