Last active
February 22, 2025 19:27
-
-
Save barbarbar338/62b45f8e501b6f1457a5199579d03527 to your computer and use it in GitHub Desktop.
Arduino UNO code used in AGU Digital System Design Capsule Project to plot clock output and calculate frequency
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
You might need to change the plotter | |
history size to see everything cleaner. | |
Here is a good tutorial for this: | |
https://youtu.be/qrfpPuw2W3A | |
*/ | |
const int analogPin = A0; // Clock output pin | |
const int threshold = 512; // Midpoint (~2.5V for a 5V signal) | |
unsigned long lastTime = 0; | |
unsigned long period = 0; | |
bool lastState = LOW; | |
void setup() { | |
Serial.begin(9600); | |
} | |
void loop() { | |
int signal = analogRead(analogPin); | |
bool currentState = (signal > threshold); // Detect HIGH or LOW state | |
// Detect a rising edge (LOW -> HIGH transition) | |
if (currentState == HIGH && lastState == LOW) { | |
unsigned long currentTime = millis(); | |
period = currentTime - lastTime; | |
lastTime = currentTime; | |
} | |
lastState = currentState; | |
// Calculate frequency (Hz) | |
float frequency = (period > 0) ? (1000.0 / period) : 0; | |
// Send data for plotting | |
Serial.print("Signal:"); | |
Serial.print((signal / 1023.0) * 5.0, 3); // Signal for plotting | |
Serial.print(","); | |
Serial.print("Frequency:"); | |
Serial.println(frequency, 3); // Frequency measurement | |
delayMicroseconds(2500); // Change this for smoother plot | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment