Hi,
I am trying to turn OFF and turn ON gpio's with Precise delay at different time with different delays. Any libraries or example's I can use for my implementation.
Thank you
Hi,
I am trying to turn OFF and turn ON gpio's with Precise delay at different time with different delays. Any libraries or example's I can use for my implementation.
Thank you
What degree of precision is required ?
Specify precise.
1. Which Arduino you are using -- UNR3, UNOR4, NANO, nanoESP32, ESP32, MEGA?
2. What are those IO pins? A(Assume UNOR3 and DPin-4, 5, 6).
3. At what interval you want to activate/deactive DPin-4?
4. At what interval you want to activate/deactive DPin-5?
5. At what interval you want to activate/deactive DPin-6?
I want to generate different levels of frequencies (range of 50Hz to 20k Hz) with 50% duty cycle on multiple GPIO's (4 gpio's all are at different freq). I tried using "Blink without Delay" example using micros() instead of millis(). For higher frequencies I am not able to achieve my target. I believe because of other functionality in loop(), in case of higher frequencies unable to catchup with small delays.
So I want to generate exact delay.
for example to get 20k Hz frequency the OFF time should me 25 micros and ON 25 micros.
I am using Arduino UNO r3, Gopi's I am using pins 11, 10, 6 and 5. All GPIO's should give output parallelly. small negligible difference is fine
You have said frequency range is: 50 Hz to 20 kHz. Please, be specific as to --
1. What frequency (or range of frequency) at DPin-11?
2. What frequency (or range of frequency) at DPin-10?
3. What frequency (or range of frequency) at DPin-6?
4. What frequency (or range of frequency) at DPin-5?
5. Do you have Arduino MEGA Board?
6. If you want, I can provide you an example of generating a square wave signal of frequency range 50 Hz - 20 kHz at DPin-9 or 10.
Please post your best attempt at using BWoD and micros so that we can see what you are doing in loop()
A standard firmata plus code, i2c implementation to connect to host PC.
I am Sorry code copy past restrictions in PC
Does this occur at a specfc time of day, or just some time after the board is powered on.
You should read up the hardware timers, tone(), millis() and micros(), ( not delay)
Possibly adding an RTC.
I tried with Tone(), but tone function works only one GPIO output at a time. So I Implemented Blink without Delay example.
any examples I can refer for RTC?
And I am planning to implement timers in compare mode to generate delay, but I need reference for this
your describing "how" you think you need to implement the code
but you're not describing "what" you need to do
do the gpio need to change periodically or on and off at specific times?
it's easy enough to have a table containing parameters for periodic actions as well as non-periodic actions that occur at specific times
On Uno R3 is only one 16bit Timer - Timer1, that controls pins 9 and 10. The two other timers - Timer0 and Timer2, are 8 bit timers and can't produce a full range of frequencies you need.
Moreover, changing the frequency of Timer0 ( which pins 5 & 6 belongs to) may affect the arduino system timing such as millis(0 and micros().
Therefore, it would be better to change the board to another one with two 16bit timers, such as Arduino Mega, STM32, RP2040 or W806. Staying on Uno R3, you need at least change the pins from 5 & 6 (Timer0) to 3 &11 (Timer2)
I will get input from PC/Laptop through serial communication. On which GPIO what Frequency should be output.
I have implemented 3 GPIOS as digital out (Pin 6, 5, 10)
case1:
If I get input command to drive pin6 with freq 100hz ( command from PC/Laptop).
The output on pin 6 should be square wave of frequency 100Hz continuously until next input for pin 6.
Case2:
If I get multiple input command to drive pin6, pin5, pin10 with freq 100hz, 500Hz, 1kHz respectively ( command from PC/Laptop).
The output on Pin6 -> 100Hz square wave
Pin5 -> 500Hz square wave
Pin10-> 1kHz square wave
These outputs on pins should be simultaneously running until any further commands to change frequencies on any Pins
Hope this helps
Your combination of using 3 IO-pins with frequencies up 20000 Hz is very demanding for an Arduino Uno.
What would be a lower frequency that would be acceptable for you?
10 kHz?, 5 kHz?, 1 kHz?
One approach to create frequencies n multiple IO-pins is to use a timer-interrupt and to use counters.
If you want to keep 3 IO-pins and frequencies of 50 Hz up to 20 kHz where you want to be able to create
19.000 kHz
19.100 kHz
19.200 kHz
18.350 kHz
etc.
you should use a microcontroller with
best regards Stefan
so you can use a timer
const int Npin = 3;
const byte Pin [Npin] = { 6, 5, 10 };
unsigned long msecPin [Npin];
unsigned long msecPeriod [Npin];
// -----------------------------------------------------------------------------
void loop ()
{
unsigned long msec = millis ();
for (int n = 0; n < Npin; n++) {
if (0 != msecPeriod [n]) { // active
if (msec - msecPin [n] > msecPeriod [n]) {
msecPin [n] = msec;
digitalWrite (Pin [n], ! digitalRead (Pin [n])); // toggle
}
}
}
// ... code to process commands
}
// -----------------------------------------------------------------------------
void setup ()
{
Serial.begin (9600);
for (int n = 0; n < Npin; n++) {
pinMode (Pin [n], OUTPUT);
}
}
How do you plan to avoid the serial interrupt for incoming characters, which will undoubtedly block any software-based timing? You can't just turn it off, you need to service that UART before the next character is ready, so?
This thread has some more infos
Though for such high frequencies I would use a ESP32
1. Refer to Fig-1, you can generate frequency of the range: 1 Hz - 8 MHz using TC1 (NOT TC0/TC2) on DPin-9/10.
2. TC0 being 8-bit, you can get as low as 30.63 Hz squave wave in CTC-4 Mode (Fig-2). It is recommended that you don't touch TC0 as it is related with millis() amd micros() functions of the IDE. You can freely use TC1 and TC2.
3.
4.
5. Equations for Waveforms (for detailed explanation, see data sheets)
f = 16 MHz/(N x (1 + TOP) //Mode: 4 , 12
f = 16 MHz/(N x (1+TOP)) //Mode: 5, 6, 7, 14, 15
f = 16 MHz/(2 x N x TOP) //Mode: 1, 2, 3, 10
f = 16 MHz/(2 x N x TOP) //Mode: 8, 9