Hot answers tagged

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 /...
  • 37.2k
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 ...
  • 37.2k
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 ...
  • 6,636
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 ...
  • 103k
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 ...
  • 37.2k
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, ...
  • 37.2k
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 ...
  • 14.5k
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.
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 ...
  • 103k
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 ...
  • 11.1k
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 ...
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 ...
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 ...
  • 2,322
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, ...
  • 37.2k
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. ...
  • 464
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 ...
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-...
  • 103k
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 ...
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 ...
  • 14.1k
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... ...
  • 161
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 ...
  • 605
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 ...
  • 37.2k
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 ...
  • 1,645
5 votes
Accepted

How to make sure Arduino's PWM pulses are phase shifted?

This (short) code below will achieve what I think you are asking for, on the Atmega328 (eg. the Uno, Duemilanove, etc.). byte dutyCycle = 80; void setup() { pinMode (3, OUTPUT); // Timer 2 "B"...
  • 35.3k
5 votes

Simple Speed question - Mapping PWM values

The analogWrite() function takes a value that is between 0 (fully off) and 255 (fully on). Your value of 150 to 240 is a value within that range of 0 to 255 and represents a percentage of the "on ...
  • 103k
5 votes
Accepted

Atmega328 PWM frequency setting not working

Your code assumes that TCCR0n are zero. But what if they are not. What if there is some hidden initialization? Timer0 is used by the Arduino micro/millis counter and the prescale (TCCR0B) is ...
  • 7,861
5 votes

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

I agree with @Majenko's answer, though here is a more "beginner" friendly method of doing the same. val = map(val,0,1023,0,255); analogWrite(pin, val); Not the most efficient, but still...
  • 2,803
5 votes

Transistor amplifier to drive speaker using Arduino PWM

Your amplifier arrangement is fundamentally flawed. At the moment you are "tickling" the top end of the electromagnet in the speaker - that is, you are energising it when the Arduino outputs a HIGH, ...
  • 103k
5 votes

Dshot implementation on Arduino (ESC protocol)

In short, I ended up creating a library capable to drive multiple DShot600 ESCs: DShot-Arduino It still need a lot more polish, but the bit-banging works really well. SPI method I tried the method ...
  • 151

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