Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

Hot answers tagged

35 votes
Accepted

What is the frequency of PWM output on Arduino

The PWM signal is generated by timers on the AVR chips. Each timer can generate a PWM signal on two or three different pins. Each pin can have it's own duty cycle, but they share the PWM frequency. ...
user avatar
  • 1,343
19 votes
Accepted

Is it possible to generate an exact 15 kHz clock pulse using an Arduino?

You can get pretty close if you program Timer 1 directly (not through the library), and have it run with the prescaler set to 1. Ideally, you want the period of the timer in clock cycles to be: F_CPU /...
user avatar
  • 35.5k
16 votes
Accepted

Increase PWM bit resolution

The Arduino Uno is based on an ATmega382P microcontroller. This chip has two 8-bit timers, driving two PWM channels each, and one 16-bit timer, driving the last two channels. You cannot increase the ...
user avatar
  • 35.5k
13 votes
Accepted

Why do I get negative voltages for a PWM waveform?

At first you had the scope AC (Alternating Current) coupled. Then you switched it to DC (Direct Current) coupled. AC coupling is used to remove the DC component of a signal. It places a large ...
user avatar
  • 6,451
13 votes
Accepted

How to output a true analog voltage at output pin

You basically have three options: Switch to an Arduino Due which has a built-in DAC which outputs a real voltage. Add an external DAC chip (such as the MCP4821/2) to create the voltage for you Use a ...
user avatar
  • 101k
12 votes

Set PWM frequency to 25 kHz

You can configure Timer 1 to cycle at 25 kHz in phase correct PWM mode, and use it's two outputs on pins 9 and 10 like so: // PWM output @ 25 kHz, only on pins 9 and 10. // Output value should be ...
user avatar
  • 35.5k
12 votes
Accepted

Set PWM frequency to 25 kHz

I am posting this second answer since I realized it is possible to have 4 PWM channels at 25 kHz with 161 steps on a single Arduino Uno. This involves changing the main clock frequency to 8 MHz, ...
user avatar
  • 35.5k
11 votes
Accepted

ArduinoUNO/MEGA, Is there a way to exceed 256 in analogWrite

Section 16, "16-bit Timer/Counter1 with PWM" of Atmel-8271J-AVR- ATmega-Datasheet_11/2015 describes using Timer/Counter-1 for 16-bit PWM, including sample code in both C and assembler. It might take a ...
user avatar
  • 14.4k
9 votes
Accepted

Arduino PWM malfunctioning

Pin 11 is shared between both PWM and SPI. By activating SPI you've disabled PWM from working on that pin. Either use another PWM pin instead, or switch to software SPI on different pins.
user avatar
9 votes

ArduinoUNO/MEGA, Is there a way to exceed 256 in analogWrite

The simplest way is to change your value range to 0-255. You can't change analogWrite's range, since that is fixed in the core software. analogWrite(5, val >> 2); The >> 2 bit-shifts the ...
user avatar
  • 101k
9 votes

Is it possible to generate an exact 15 kHz clock pulse using an Arduino?

Since the timer1 library only accepts whole numbers for the µs parameter you get a error. You could skip using the library and configure the timer directly. Or you could have a look at the source code ...
user avatar
  • 11.1k
8 votes

attiny85 and arduino - analogRead + pwmOut

Analog read is 10-bits (2^10 = 0-1023 range), analog write is 8-bits (2^8 = 0-255 range). Ditch the lower two bits of the result either by doing: analogIn = analogIn >> 2; which can be ...
user avatar
  • 5,170
8 votes
Accepted

PWM signaling with Arduinos: What is the reason for the ground connection?

Is PWM really a signal? To me, it looks like an electric current turned on and kept on for a certain time, then off for a while repeatedly. What makes this a "signal"? At the most basic, it is a ...
user avatar
7 votes
Accepted

Does the square-wave nature of PWM dictate how much resistance you need?

It's... mostly wrong. For three independent reasons. The LEDs themselves, like all diodes, absorb some of the electrovoltaic potential of the electricity passing through them, resulting in a voltage ...
user avatar
7 votes
Accepted

Can I connect a PWM pin on one Arduino to an analog input on another?

Yes, you can do this with "a little programming" or more easily by adding two cheap components. If you connect a say 47k resistor from PWM_Out to Analog_In and a say 100 uF capacitor from Anaolg_In ...
user avatar
7 votes

How to output a true analog voltage at output pin

At 5V, a 1 ohm resistor will try to sink 1A and far exceed the 40mA specs. Please use at least a 5/0.040=125 ohm resistor to protect your pin. And if you put the a capacitor between your resistor ...
user avatar
  • 2,292
7 votes
Accepted

How do you get 2 PWM pins to inversely bitbang at 25 KHz on a Nano?

There are various solutions. I suggest using two PWM channels driven by a single timer. This way you do not have to worry about synchronizing two timers. You can configure the channels identically, ...
user avatar
  • 35.5k
6 votes
Accepted

Use Arduino's PWM to control motor direction?

You seem to have misunderstood PWM here. It's actually a digital signal which is constantly pulsing on and off very quickly (several hundred times per second). You don't actually change the output ...
user avatar
6 votes
Accepted

Arduino LED backlight - should PWM be used with transistor or capacitor?

You don't need the MOSFET, you don't need the capacitor, you do need the resistor. The pins on your Arduino can deliver up to 20mA comfortably, so in this case there is no need for the MOSFET. ...
user avatar
  • 464
6 votes
Accepted

Does millis() conflict with the PWM pins associated with timer 0?

They do not conflict as millis() strictly reads the immediate value in TCNT0 whereas PWM via timer 0 uses the hardware's ability to compare the value of TCNT0 with the values in OCR0x without ...
user avatar
6 votes
Accepted

Why does an RGB LED between VCC and PWM work?

This is a common arrangement and is known as Common Anode, and the I/O pin is said to be sinking current. To better understand what is happening here it's best to think of an I/O pin as a two-...
user avatar
  • 101k
6 votes

How do I set a 25 kHz PWM signal on an ESP-WROOM-32?

It is very simple to set up a PWM signal at any frequency you desire, with any resolution you desire, provided you don't exceed the maximum 'bandwidth': the frequency multplied by the resolution must ...
user avatar
6 votes
Accepted

Is it possible to measure analog output?

First: Why would you need to measure the PWM value. You already know it; it's saved in the val variable. Why not just use this variable for later use in code? Second: Doing the analogRead on the same ...
user avatar
  • 13.7k
6 votes

How do I convert 0-5v PWM to a 0-12v PWM

I advice you to learn about basics of how to connect external components to MCU. Anwser to your question is simple transistor amplifier... So you can use bipolar transistor (NPN/PNP) or MOSFET... ...
user avatar
  • 151
6 votes

Is it possible to generate an exact 15 kHz clock pulse using an Arduino?

A better way to do the above average 15 kHz (or any other frequency) is with a phase accumulator scheme. There are no IF tests; on each tick of an interrupt, you add a step to an accumulator and ...
user avatar
  • 595
6 votes

Can’t set duty cycle to 0% with custom PWM

That is how it is supposed to be. In this fast PWM mode, the duty cycle is (OCR1A+1)/(ICR1+1). If you want a duty cycle of zero, you have to disengage the PWM and turn the pin to OUTPUT LOW. This is ...
user avatar
  • 35.5k
5 votes

Does millis() conflict with the PWM pins associated with timer 0?

Just to add to @Ignacio's answer which has directly answered your question. The "conflict" you speak of is in relation to Timer0's prescaler. For the most part, you can use those pins (incidentally ...
user avatar
  • 1,302
5 votes
Accepted

Arduino PWM fading led

From your code you are using Timer0 in FastPWM mode with top = 0xFF. In this mode, as used in your code, OCR0A defines the pulse width rate (more precisely, OCR0A / 256). Timer0 uses port OC0A and ...
user avatar
  • 8,809
5 votes
Accepted

How can the Arduino Uno support up to 12 servos if it only has 6 digital PWM pins?

Although I haven't looked at the source myself, in these types of situations they usually use interrupt timer 1, which in PWM mode would have been associated with pins 9 and 10. This would explain why ...
user avatar
5 votes

Can I connect a PWM pin on one Arduino to an analog input on another?

It is possible, but not the best idea. The analog inputs require a reasonably stable input - this can be achieved with a RC filter to smooth the PWM output. This would work for reasonably slow data ...
user avatar
  • 1,635

Only top scored, non community-wiki answers of a minimum length are eligible