Skip to content

Instantly share code, notes, and snippets.

@dwblair
Created May 28, 2014 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwblair/5d56cb15971b8f5239f1 to your computer and use it in GitHub Desktop.
Save dwblair/5d56cb15971b8f5239f1 to your computer and use it in GitHub Desktop.
long pulseCount = 0; //a pulse counter variable
unsigned long pulseTime,lastTime, duration, totalDuration;
int samplingPeriod=2; // the number of seconds to measure 555 oscillations
int fivefivefive = 13; // the pin that powers the 555 subcircuit
void setup()
{
Serial.begin(9600);
pinMode(fivefivefive, OUTPUT);
}
void loop()
{
delay(1000); // imagine riffle is doing other stuff (like sleeping)
// now make a conductivity measurement
//turn on the 555 system
digitalWrite(fivefivefive,HIGH); //turns on the 555 timer subcircuit
pulseCount=0; //reset the pulse counter
totalDuration=0; //reset the totalDuration of all pulses measured
attachInterrupt(1,onPulse,RISING); //attach an interrupt counter to interrupt pin 1 (digital pin #3) -- the only other possible pin on the 328p is interrupt pin #0 (digital pin #2)
pulseTime=micros(); // start the stopwatch
delay(samplingPeriod*1000); //give ourselves samplingPeriod seconds to make this measurement, during which the "onPulse" function will count up all the pulses, and sum the total time they took as 'totalDuration'
detachInterrupt(1); //we've finished sampling, so detach the interrupt function -- don't count any more pulses
//turn off the 555 system
digitalWrite(fivefivefive,LOW);
if (pulseCount>0) { //use this logic in case something went wrong
double durationS=totalDuration/double(pulseCount)/1000000.; //the total duration, in seconds, per pulse (note that totalDuration was in microseconds)
// print out stats
Serial.print("sampling period=");
Serial.print(samplingPeriod);
Serial.print(" sec; #pulses=");
Serial.print(pulseCount);
Serial.print("; duration per pulse (sec)=");
Serial.println(durationS,4);
}
}
void onPulse()
{
pulseCount++;
//Serial.print("pulsecount=");
//Serial.println(pulseCount);
lastTime = pulseTime;
pulseTime = micros();
duration=pulseTime-lastTime;
totalDuration+=duration;
//Serial.println(totalDuration);
}
@dwblair
Copy link
Author

dwblair commented May 28, 2014

With these settings for resistors and capacitors, using Arduino Uno, got:

RA=21000;RB=47;C=10_10__-6;f=1/(0.7_(RA+2_RB)_C);1/f
Out[9]: 0.14765799999999998

got this:

sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.2145
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.2148
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.2145
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.2143
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.2146
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.2146
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.2143
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.2147
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.2143
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.2144

@dwblair
Copy link
Author

dwblair commented May 28, 2014

Woah! On Mini Pro, got:
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14029
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14034
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14030
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14031
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14033
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14031
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14033
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14032
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14029
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14031
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14029
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14028
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14029
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14029
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14030

Need to make sure this isn't a coincidence -- but odd that it's spot-on for Arduino Mini Pro, and not for UNO?

@dwblair
Copy link
Author

dwblair commented May 28, 2014

Ah, that was a fluke -- I had the UNO plugged into the circuit at the same time -- now I'm recovering the same values on the Pro Mini as I had on the UNO:

sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.21455
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.21466
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.21462
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.21451
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.21466
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.21463
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.21453
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.21466
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.21463
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.21453
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.21468
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.21452
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.21468

-- which is actually reassuring. The difference between the pulse duration theory and actual is to be expected from deviations in the resistor and capacitor from their nominal values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment