Questions tagged [analogwrite]

The analogWrite() function is used to generate a PWM (Pulse Width Modulation) output signal on compatible pins.

The analogWrite() function is part of the standard Arduino library. It does not allow true digital-to-analog conversion. Rather, it generates a PWM (Pulse Width Modulation) output signal on any compatible pin.

It has the following syntax:

analogWrite(pin, value);

The first parameter identifies the IO pin number to generate the signal on. The second parameter should be in the range 0-255, and specifies the duty cycle to use (0 is 'always off', 255 is 'always on').

Full documentation is available online here: http://arduino.cc/en/Reference/AnalogWrite

Note: Please only use this tag for questions which are specific to the analogWrite() function. For questions about PWM in general, please use the tag.

101 questions
27
votes
1 answer

What is the frequency of PWM output on Arduino

What frequency do Arduinos use for normal PWM when you use analogWrite()? And is it different for different Arduino model? I'm interested specifically in the Mega 2560, but also want to know if it's consistent between models. I've seen a passing…
Duncan C
  • 5,752
  • 3
  • 19
  • 30
22
votes
2 answers

Why do some pins have a different PWM frequency?

According to the Arduino reference for analogWrite(), the PWM frequency on most pins is ~490 Hz. However, it's ~980 Hz for pins 5 and 6 on the Uno, and for pins 3 and 11 on the Leonardo. Why are these different? Is it a deliberate design feature, or…
Peter Bloomfield
  • 10,982
  • 9
  • 48
  • 87
14
votes
6 answers

Is there a way to play sounds from an Arduino without using a sound shield?

I would like to play a wav file (spoken words) from my Arduino. I do not want to add a sound shield. Is there a way to play a sound through an output pin? The quality does not have to be great.
Hoytman
  • 747
  • 5
  • 13
  • 27
13
votes
6 answers

How to output a true analog voltage at output pin

Regarding my program, it is a program that does some calculations and then outputs a voltage based on the result using analogWrite function. However my problem is that I had done my programming based on a misconception that analogWrite function via…
bytk
  • 133
  • 1
  • 1
  • 9
10
votes
8 answers

What's the difference between analogWrite and digitalWrite?

As the title states. What's the difference between the two? analogWrite(pin,0-255) vs digitalWrite(pin,LOW-HIGH)
pijemcolu
  • 127
  • 1
  • 1
  • 8
8
votes
6 answers

Two Arduinos Send data via Analog Pin?

What I want to do is allow Arduino A to send some constant value to Arduino B. I sense something inherently wrong with my approach, but I cant figure out what it is. So in the diagram below you will see Arduino A setting its Analog Pin 0 as OUTPUT…
lucidgold
  • 233
  • 1
  • 3
  • 9
8
votes
2 answers

How to use the analog comparator to determine when an analog pin has reached a certain voltage?

I am trying to find some information on how to use the analog comparator on an Atmega328 to detect when an analog pin has reached a certain voltage (3.16 volts). I cannot find any example code that says how to do this, although I have read that the…
3871968
  • 308
  • 1
  • 2
  • 10
7
votes
2 answers

How can I differentiate between multiple circuits?

I'm building a puzzle for an escape room. I want to give the user 6 wires and 6 terminals, then confirm that they've connected the correct wires to the correct terminals (one-to-one). My first (successful) attempt to implement this puzzle was to…
6
votes
2 answers

A question about resistance measurement with arduino

Lately I was asking myself about the possibility of measuring a potentiometer resistance with Arduino. My questions are: Is it possible? If it's possible, what kind of restriction does the Arduino have with the resistance value measured?
6
votes
1 answer

Why does an LED sometimes flash when increasing brightness?

This is admittedly a cross-post from LED fade malfunction (random flash) but I can't get an answer on the Arduino forum. I was mucking around with some very basic code and I noticed that when repeatedly holding an LED at 0 brightness for 1 second…
Adrian
  • 108
  • 8
5
votes
3 answers

Sending a value from one arduino to another

I wonder if it is possible to send a value from one arduino to another. Basically, I have one arduino measuring let's say weight, but I want to display that value using an lcd in another arduino. Is it possible to send the value to a pin using…
Camilo
  • 359
  • 2
  • 5
  • 12
4
votes
2 answers

Why does a Piezo Buzzer not require a PWM digital pin (Gallileo Gen2)?

According to my current (poor) understanding, only the PWM pins (with the ~) among the digital pins (ignoring Analog pins) can be passed to analogWrite(). I understand the servo therefore - since interfaced by a variable voltage signal - must be…
Anti Earth
  • 145
  • 1
  • 2
  • 12
4
votes
1 answer

MEGA 2560: Simple program - One analogWrite turns off the other

I'm using Windows 10, Arduino 1.8.12 (Windows Store 1.8.33.0) I have an Arduino MEGA 2560 and a red LED connected to pin 4. #define RED 4 #define GREEN 2 void setup() { analogWrite(GREEN, 0); analogWrite(RED, 100); } void loop() { } This…
Mark
  • 197
  • 1
  • 1
  • 8
3
votes
2 answers

How to implement multiple LED modes in a sketch with potentiometers?

My hardware is 4 potentiometers connected to Arduino Analog 0-3, the output is an RGB led driver connected to Digital outputs 9,10,11. The output channels provide access to PWM control of those pins. So idea is the color and intensity can be…
user608
  • 31
  • 2
3
votes
4 answers

Writing analog voltages on analog pins of Arduino

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);
Jarvis1997
  • 31
  • 1
  • 3
1
2 3 4 5 6 7