Hot answers tagged

20 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 /...
Edgar Bonet's user avatar
  • 42.2k
14 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, ...
Edgar Bonet's user avatar
  • 42.2k
13 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 ...
Edgar Bonet's user avatar
  • 42.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 ...
st2000's user avatar
  • 7,255
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 ...
Majenko's user avatar
  • 105k
12 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 ...
JRobert's user avatar
  • 15.1k
10 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 ...
Gerben's user avatar
  • 11.3k
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 ...
Majenko's user avatar
  • 105k
8 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, ...
Edgar Bonet's user avatar
  • 42.2k
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 ...
Dave X's user avatar
  • 2,332
7 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 ...
Edgar Bonet's user avatar
  • 42.2k
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-...
Majenko's user avatar
  • 105k
6 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 ...
xandy's user avatar
  • 161
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 ...
CharlieHanson's user avatar
6 votes
Accepted

Can I use the PWM pins as regular digital input/output pins?

Yes you can use them as ordinary digital io pins. PWM is essentially a normal digital output pin that is repeatedly turned on and off at a high rate. The magic is that the turning off and on is done ...
GMc's user avatar
  • 393
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 ...
chrisl's user avatar
  • 15.8k
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... ...
Sahasrar's user avatar
  • 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 ...
Boggyman's user avatar
  • 609
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 ...
Majenko's user avatar
  • 105k
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 ...
Mikael Patel's user avatar
  • 7,959
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...
Dat Ha's user avatar
  • 2,913
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, ...
Majenko's user avatar
  • 105k
5 votes
Accepted

Platform-independent functions for FastPWM?

It is highly unlikely there would be direct 1:1 functions that implement such discrete operations. Simply because such operations are pretty unique to a single family of microcontrollers. They are too ...
Majenko's user avatar
  • 105k
5 votes

Why are the PWM variables in this RC code snippet handled this way? What are the magic numbers?

There is not enough context to be certain, but given the variable names I can assume that this is code from a sketch that receives information from an RC controller and is intended to control parts of ...
Majenko's user avatar
  • 105k
5 votes
Accepted

Output voltage range between 0-5v

Short answer: No. Arduino Unos and Megas have PWM (Pulse Width Modulation) output, which rapidly turn the output on and off with a varying "duty cycle" (The ratio of on time to off time.) ...
Duncan C's user avatar
  • 5,652
5 votes

Dimming this ~30V led with PWM, what do I need?

It will not be possible for you to PWM dim your LED with that LED driver (power supply). That is a constant current driver with under/over-voltage protection. It delivers a specific amount of current (...
Majenko's user avatar
  • 105k
5 votes

Fast PWM with Arduino, the inputs outputs don't work

This line: TIMSK = (1<<OCIE1A) | (1<<TOIE1); enables a couple of interrupts generated by Timer 1. Whenever you enable an interrupt source, you should make sure that the matching ISR has ...
Edgar Bonet's user avatar
  • 42.2k
4 votes

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

For greater precision, less code, faster execution and no need for capacitors read the PWM "analog" signal on the other Arduino from a digital input pin with pulseIn(pin, HIGH, timeout) The timeout ...
CORTEX Systems's user avatar
4 votes
Accepted

How to eliminate noise from PWM controlled 12V fan at low speed

I had the same problem with a 5V fan. Something simple worked for me: To decrease the PWM frequency. You probably want to do this using pin 9 or 10 (for the Uno) as changing the frequencies of other ...
SAS's user avatar
  • 56

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