24 votes
Accepted

Why is our refresh rate consistently decreasing in logging on SD card?

I don't have certain answer for you, but a deep suspicion. If I get bored I'll confirm it. Confirmed below. In each pass of loop(), you have: dataFile = SD.open("log1.csv", FILE_WRITE); ...
user avatar
  • 3,868
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
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

Maximum frequency of digital signal in Arduino Uno?

Yes, use the hardware timers. You can achieve 8 MHz. Example sketch which outputs 8 MHz on pin 9 on a Uno: #ifdef __AVR_ATmega2560__ const byte CLOCKOUT = 11; // Mega 2560 #else const byte ...
user avatar
  • 35k
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
7 votes
Accepted

Arduino constant clock output

This outputs 8 MHz on pin 9: #ifdef __AVR_ATmega2560__ const byte CLOCKOUT = 11; // Mega 2560 #else const byte CLOCKOUT = 9; // Uno, Duemilanove, etc. #endif void setup () { // set up 8 ...
user avatar
  • 35k
6 votes

Is it possible to sample sound with arduino?

For the AtMega328's ADC the datasheet says • Up to 76.9kSPS (Up to 15kSPS at Maximum Resolution) So, sampling two channels at 10bit resolution and 44.1kHz is not possible. If 8bit resolution is ...
user avatar
  • 584
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
5 votes

High Frequency Sine Wave Generation

To generate a square wave, you only need to update the output at a rate of two points per cycle. (Technically, when the Arduino's PWM output is configured for 50% duty cycle, that's a square wave at ...
user avatar
  • 450
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...
user avatar
  • 2,793
5 votes

Programming standalone Atmega328p-PU with NE555N timer IC

No, you won't be able to build a 16 MHz clock signal using a 555 timer. That's far beyond the 555 capabilities (see N.B.). If you could squeeze out of the 555 a mere 1 MHz (which I seriously doubt), ...
user avatar
  • 2,092
5 votes
Accepted

Measure audio frequency through pulse duration

Timer 1 overflows at 255 because that's how it has been configured by the Arduino core, as it is intended to provide 8-bit PWM. If you want to use the timer in normal mode, you should undo the Arduino'...
user avatar
  • 35.5k
5 votes
Accepted

What is the maximum frequency input signal that I can accurately track on a GPIO pin?

As Majenko wrote, when you only rely on software to measure the frequency on a IO pin, it will always be rather slow. And it also depends on the code, that you use. Functions like digitalRead() are ...
user avatar
  • 13.7k
5 votes

ATMega4809 via Nano Every marked as 20Mhz, but does this confirm it is actually at 16Mhz?

With the Nano Every they went out of their way to make it look like as much like original Nano, to the point of providing that "register emulation" feature to make it appear that it has the ...
user avatar
  • 3,868
4 votes
Accepted

Building an adjustable low frequency PWM controller with Arduino

The ATmega328 in the Arduino Uno has a 16-bit timer (Timer/Counter 1) which can do PWM. It has a prescaler that can divide by 1024. The lowest frequency that it can generate is about 0.12 Hz: 16MHz /...
user avatar
  • 56
4 votes

Pin toggle speed

If you compile the code and examine the results you can see how many instructions it will take, and thus how many clock cycles. It's slightly more complex because of what else happens in main() to ...
user avatar
  • 101k
4 votes

Programming standalone Atmega328p-PU with NE555N timer IC

Yes, you could use a 555. You can also go even simpler and use an RC network. Even simpler still is to use the 1MHz RC network built in​ to the chip. The main drawback of these methods, and the 555 ...
user avatar
  • 101k
4 votes

Programming standalone Atmega328p-PU with NE555N timer IC

The default configuration of these chips, as they come from the factory, is to use their internal 8 MHz RC oscillator downscaled at 1 MHz. So you do not need any extra oscillator to program them. ...
user avatar
  • 35.5k
4 votes

Generated frequency is not as expected

Welcome to SE. digitalWrite() takes a few microseconds to execute as well. There is also some overhead around the delayMicroseconds() function. I you need exactly 500 kHz, you would want to consider ...
user avatar
  • 833
4 votes

Generate 1khz square signal with Arduino Mega

My first question is: Is there a way to automatically control an output pin from the timer, without having to execute any line of code in my arduino program ? Yes. It's called PWM with a 50% duty ...
user avatar
  • 101k
3 votes

Arduino constant clock output

You can use one of the PWM pins on Arduino to output a PWM signal. If you want a constant clock, you need to set the duty cycle of the PWM to be 0.5, i.e. 50%. Syntax: analogWrite(pin, value) where ...
user avatar
3 votes

Arduino coding of producing frequency

The code below generates 38 kHz and modulates its duty cycle. // Example of modulating a 38 kHz frequency duty cycle by reading a potentiometer // Author: Nick Gammon // Date: 24 September 2012 ...
user avatar
  • 35k
3 votes

Arduino Uno with crystal

Some of the Freeduino boards use crystals although the newer ones now use a cheaper USB to serial chip (the CH340) instead of the Atmel chip used on the Arduino Uno. The diavolino is another build-it-...
user avatar
  • 180
3 votes

How to detect if running Arduino or bare Atmega328p?

Bare vs Stock is nearly impossible to detect at compile time. Where it depends on what you think you are trying to determine versus detect. The best I can think of is creating a different Board ...
user avatar
  • 2,433
3 votes

Ensuring specific frequency at specific time?

Generating a linear frequency ramp with accurate timings is not an easy task. I am providing here only a partial answer, where I first go through the math of the problem, then give some ideas for the ...
user avatar
  • 35.5k
3 votes

Ensuring specific frequency at specific time?

As mentioned in comments and other answers, using timer 1 for hardware-timed bit toggling is likely to produce better results than code using delays based on calls like delayMicroseconds(), primarily ...
user avatar
3 votes
Accepted

Change Digispark clock speed, run at low voltage?

You can set the clock prescaler at run time. For example: #include <avr/power.h> void setup() { clock_prescale_set(clock_div_8); // etc... } More details in the documentation from avr-...
user avatar
  • 35.5k

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