23

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); In short, finding the end of the file is linear operation in a FAT filesystem. It must follow the allocation chain to find (or create), the last cluster of the ...


20

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 / 15 kHz = 16,000 kHz / 15 kHz ≈ 1066.67 CPU cycles If you round this to the nearest integer, you get F_CPU / 1,067 = 16,000 kHz / 1,067 ≈ 14.9953 kHz This is ...


12

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 between 0 and 320, inclusive. void analogWrite25k(int pin, int value) { switch (pin) { case 9: OCR1A = value; break; ...


11

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, which has some side effects since the whole program will run half as fast. It also involves reconfiguring the three timers, which means loosing the Arduino ...


11

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 couple of reads over that section to get familiar with the hardware and learn what else T/C-1 is used for, and that you might have to give up while using it ...


9

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 CLOCKOUT = 9; // Uno, Duemilanove, etc. #endif void setup () { // set up 8 MHz timer on CLOCKOUT (OC1A) pinMode (CLOCKOUT, OUTPUT); // set up Timer 1 ...


9

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 value two bits to the right, turning a 10-bit value (0-1023) into an 8-bit value (0-255). It's by far the most efficient way of dividing by 4. On more complex ...


9

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 of the library, and see that you can kind of bypass the limitation it has by only calculating a more accurate value for the ICR1 register. Look at cycles = ((...


7

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 MHz timer on CLOCKOUT (OC1A) pinMode (CLOCKOUT, OUTPUT); // set up Timer 1 TCCR1A = bit (COM1A0); // toggle OC1A on Compare Match TCCR1B = bit (WGM12) |...


7

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 output the state of its MSB. This can give incredible resolution, and is probably the best you can do. But though the average frequency will be dead-on, the ...


6

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 enough, and if there's only one channel to sample, it should be able to do the job. Edit: To answer Connor's comment: Of course, this does only apply when ...


6

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's initialization by setting TCCR1A to zero. Two comments on the ISR: ISR (ANALOG_COMP_vect) { timeNow = TCNT1; Serial.print (timeNow); Serial....


6

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 always slower than directly working with the control registers of the pin (Reference). As you didn't show your code, we cannot say more about that. If you need to ...


5

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 some frequency.) But to generate a clean sine wave (without a lot of distortion), you need to update a lot more frequenclly than two points per cycle. ...


5

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...


5

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. Once you program the chip the first time, if you configure it to use an external resonator/oscillator, then you do need to have that attached in order to reprogram ...


5

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), the clock stability and jitter would be so bad that it won't be usable but for simple sketches not requiring any timing accuracy. At that point, you would be ...


5

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 using a timer. You can read up on how to set one up in the microcontrollers datasheet. Unfortunately, I don't have access to an Arduino Leonardo, but here is ...


4

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 call loop, but basically you have: for (;;) { loop(); aa:0e 94 49 00 call 0x92; 0x92 <loop> if (serialEventRun) serialEventRun(); ae:20 97 sbi ...


4

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 / 1024 / 65535 / 2 = 0.1192 Hz We change the PWM frequency by changing the "top" value. For higher frequencies, lower the top value, and/or pick a smaller ...


4

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 implementation. The math We are dealing with a frequency-modulated signal, with the frequency ramping linearly from f = 0 at t = 0 up to a ...


4

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-libc. Note that with this method your Digispark will still be overclocked when booting, so it might not be 100% reliable. But it will be overclocked only for ...


4

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 particularly, is that of stability. Using a crystal and capacitors you can get very accurate timing at high frequencies. Using a 555 you can get ballpark timing ...


4

I understand Arduino should be somewhat simpler [than PIC]. The whole Arduino platform (boards, core library and IDE) makes it super easy to get started and do simple things. However, if you want to “push the limits” and use the full potential of the MCU, you may have to dig into low-level programming, and this has a steep learning curve. The 250V mains ...


4

The execution time of your loop() will be slightly longer when the CPU runs at 8 MHz v.s. 16 MHz, but I don't expect the difference to be significant. CPU-bound functions, such as digitalWrite(), will run at half the speed. These functions, however, are quite fast. In contrast, analogRead() is slow, as it takes roughly 110 µs to execute, so this is likely ...


4

To keep the same brightness, you have to keep the same duty cycle (percent of on time over off time). So to change the rate without changing the brightness, you have to increase delayMicroseconds(onTime) by the same percent you increase delay(strobeDelay). But note that as you go slower, the on time will be too long to freeze the motion you are trying to ...


3

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 the parameter "value" is the duty cycle ranges from 0 (always off) to 255 (always on) since it is a 8-bit PWM generator inside Arduino. If you need a PWM wave ...


3

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 const byte POTENTIOMETER = A0; const byte LED = 10; // Timer 1 "B" output: OC1B // Clock frequency divided by 38 kHz frequency desired const long ...


3

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-yourself board that has a crystal. I am sure there are many others. The ruggeduino is another that uses a crystal and also markets itself as being practically ...


3

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 definition; one for your bare and the other being the stock (i.g. UNO). Where now in IDE 1.5 see github.com/arduino/Arduino/wiki/… the parameter uno.build.board=...


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