Hello all
I am integrating the Arduino Pro Mini circuit with my project into one motherboard, and my project requires a high timing accuracy of up to half a millisecond. What is the appropriate oscillator to get the highest possible accuracy in timing?
over what time interval?
From half a millisecond to one millisecond
No, that is meaningless. For example half a millisecond per year is not the same as half a millisecond per second.
I will give you an example, I want to turn on and turn OFF
the LED, for example, it turns on 10 milliseconds, then turns off 7 milliseconds, and I want the accuracy in this very short time by 99%
That is still not what we want to know. What happens after the led is off by 7 milliseconds ? Do you want to turn it on again ? Do you want to blink a led for a whole day and after 24 hours still have a maximum of half a millisecond inaccuracy ?
Use micros().
My project contains a lot of optocouplers, which are highly sensitive, and all I need from the Arduino is to give pulses of volts and these pulses are measured in milliseconds
There are 1000 microseconds in one millisecond. Please answer the questions that were asked.
I will give you an example code
I want to install an oscillator that gives me a precise time delay in the following code
int ButtonPin=3;
int M1=12;
int M2=11;
int M3=10;
int M4=9;
int count=8;
void setup() {
pinMode(ButtonPin, INPUT_PULLUP);
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(M3, OUTPUT);
pinMode(M4, OUTPUT);
}
void loop() {
if (digitalRead(ButtonPin) == LOW){
digitalWrite(M1, HIGH);
digitalWrite(M3, HIGH);
delay(10);
digitalWrite(M1, LOW);
digitalWrite(M3, LOW);
delay(50);
digitalWrite(M3, HIGH);
digitalWrite(M4, HIGH);
delay(50);
digitalWrite(M3, LOW);
digitalWrite(M4, LOW);
}
}
Okay, then... use delayMicroseconds(). Any oscillator will work for this. Even the horribly inaccurate resonator that is standard. Any cheapo quartz crystal can will get you 100x better too, for only a few pennies more...
Oh really
Can I use any oscillator?
And my code is too long, will changing milliseconds to microseconds give me more accuracy?
Example
int ButtonPin=3;
int M1=12;
int M2=11;
int M3=10;
int M4=9;
int count=8;
void setup() {
pinMode(ButtonPin, INPUT_PULLUP);
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(M3, OUTPUT);
pinMode(M4, OUTPUT);
}
void loop() {
if (digitalRead(ButtonPin) == LOW){
digitalWrite(M1, HIGH);
digitalWrite(M3, HIGH);
delay(10);
digitalWrite(M1, LOW);
digitalWrite(M3, LOW);
delay(50);
digitalWrite(M3, HIGH);
digitalWrite(M4, HIGH);
delay(50);
digitalWrite(M3, LOW);
digitalWrite(M4, LOW);
delay(7)
digitalWrite(M1, HIGH);
digitalWrite(M3, HIGH);
delay(10);
digitalWrite(M1, LOW);
digitalWrite(M3, LOW);
delay(24);
digitalWrite(M3, HIGH);
digitalWrite(M4, HIGH);
delay(50);
digitalWrite(M3, LOW);
digitalWrite(M4, LOW);
delay(66)
digitalWrite(M1, HIGH);
digitalWrite(M3, HIGH);
delay(10);
digitalWrite(M1, LOW);
digitalWrite(M3, LOW);
delay(50);
digitalWrite(M3, HIGH);
digitalWrite(M4, HIGH);
delay(50);
digitalWrite(M3, LOW);
digitalWrite(M4, LOW);
delay(35)
digitalWrite(M1, HIGH);
digitalWrite(M3, HIGH);
delay(10);
digitalWrite(M1, LOW);
digitalWrite(M3, LOW);
delay(50);
digitalWrite(M3, HIGH);
digitalWrite(M4, HIGH);
delay(50);
digitalWrite(M3, LOW);
digitalWrite(M4, LOW);
delay(87)
digitalWrite(M1, HIGH);
digitalWrite(M3, HIGH);
delay(10);
digitalWrite(M1, LOW);
digitalWrite(M3, LOW);
delay(50);
digitalWrite(M3, HIGH);
digitalWrite(M4, HIGH);
delay(50);
digitalWrite(M3, LOW);
digitalWrite(M4, LOW);
delay(7)
digitalWrite(M1, HIGH);
digitalWrite(M3, HIGH);
delay(10);
digitalWrite(M1, LOW);
digitalWrite(M3, LOW);
delay(50);
digitalWrite(M3, HIGH);
digitalWrite(M4, HIGH);
delay(50);
digitalWrite(M3, LOW);
digitalWrite(M4, LOW);
}
}
That's the whole idea. A digitalWrite operation takes about 10uS on an AVR.
Can I use any oscillator?
What oscillator have you looked at so far?
Thank you, that was very helpful
If you are hand assembling your board, the resonator will be too hard to solder anyway... a quartz can with through hole leads is much easier to deal with. It will be about 100,000 times more accurate than you need. Unless there is something about your mystery project that you haven't told us...
In fact, I have no background in oscillators, and this made me turn to the forum. Would you recommend me a specific type, I don't care about the cost?
I will not solder electronic components, because JLCPCB offers the service of manufacturing boards and assembling and soldering electronic components to get you ready and The quality of the huge factories ..
As for my mysterious project, it is a small drawing machine that is fast in performance and I am still doing tests on it and it has not been completed yet.
In case you want more:
- Since all pins M1,M2,M3,M4 are at PortD of the ATmega328P it is possible to toggle those pins at the same time (using the pin-toggle option by writing to the PINB register while the pin is set as output).
- If you don't need to do other things in your sketch, it is possible to turn off the interrupts and use the cpu clock cycles to delay. The delay will be in steps of 62.5 ns. I have used that some time ago, but I can not find it yet. Since toggling the output pins also takes 62.5ns, it is possible to calculate the delay to have 100% accurate timing.
What you mentioned may be very useful, can you put a code as an example ?
I have never used this method before or I don't know about it