When I connect a power-adapter(DC 9v 1000ma, DC 12v 1000ma) to my arduino, the DSM501a(particle sensor) starts having problems. It seems that some very long blocking starts to take place because of pulseIn.
DSM501a >> Sensor PDF
When looking at the left side (connect to power adapter)
- Ten samples returned with out timing out
- Total time to collect the samples was 36.168
- The duration of all pulseIn added together was 0.764
- board VCC is 5.091
- simple benchmark ran after code block shows 756 milliseconds (board running at correct speed)
When looking at the right side (connect to computer via usb)
- Ten samples returned with out timing out
- Total time to collect the samples was 1.757
- The duration of all pulseIn added together was 0.774
- board VCC is 4.979
- simple benchmark ran after code block shows 756 milliseconds (board running at correct speed)
Conclusion:
When connected to a power-adapter collecting the samples takes approximately 30x longer, but the total of pulesIn duration are the same, what is causing this issue?
The following code is a simple sketch to illustrate my issue, my program actually uses interrupts and an LCD but the problem is the same.
Full Code
int pin = 3;
//**************************************************************************************
// READ VOLTAGE ON VIN
long readVccA() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}
//**************************************************************************************
//GET TEMPTURE OF CPU
double GetTemp(void)
{
unsigned int wADC;
double t;
// The internal temperature has to be used
// with the internal reference of 1.1V.
// Channel 8 can not be selected with
// the analogRead function yet.
// Set the internal reference and mux.
ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));
ADCSRA |= _BV(ADEN); // enable the ADC
delay(20); // wait for voltages to become stable.
ADCSRA |= _BV(ADSC); // Start the ADC
// Detect end-of-conversion
while (bit_is_set(ADCSRA,ADSC));
// Reading register "ADCW" takes care of how to read ADCL and ADCH.
wADC = ADCW;
// The offset of 324.31 could be wrong. It is just an indication.
t = (wADC - 324.31 ) / 1.22;
// The returned temperature is in degrees Celcius.
return (t);
}
//************************************************************************
//BENCHMARK
long bench_cnt = 0;
void benchmark() {
bench_cnt = 0;
for(bench_cnt = 0; bench_cnt < 1000000; bench_cnt++);
}
//************************************************************************
//SETUP APP
void setup() {
Serial.begin(9600);
pinMode(pin,INPUT);
Serial.println( " ------------------------ ");
Serial.println( " - SETUP_COMPLETED - ");
Serial.println( " ------------------------ ");
}
//************************************************************************
//ENTERFRAME
void loop() {
long error = 0;
int sample_cnt = 10;
unsigned long sample = 0;
unsigned long duration = 0;
float start_time = micros();
for (int i=0; i <= sample_cnt-1; i++){
sample = pulseIn(pin, LOW,60000000); //needs timeout or it does time out
duration = duration + sample;
Serial.print( i+1 ); //removing these serial commands, makes the program run slower, why?
Serial.print( ": " );
Serial.println(float(sample)/1000000,3);
if( sample==0){
error++;
};
} //END
float time_end = (micros()-float(start_time))/1000000;
Serial.print( "Sample Count: ");
Serial.print( sample_cnt );
Serial.print( " || Time: ");
Serial.print( time_end,3 );
Serial.print( " || Duration: ");
Serial.print(float(duration)/1000000,3);
Serial.print(" || Error: ");
Serial.print(error);
Serial.print(" || VCC: ");
Serial.print(float(readVccB())/1000,3);
Serial.print(" || TEMP: ");
Serial.println(float(GetTemp()),2);
//***********************************************
//CLOCK SPEED IS THE SAME : THIS IS FINE
long unsigned startTime = 0;
long unsigned endTime = 0;
long unsigned fullTime = 0;
startTime = millis();
benchmark();
endTime = millis();
fullTime = endTime - startTime;
Serial.print(" >> BENCHMARK FOR CLOCK SPEED: ");
Serial.println(fullTime);
}
Relevant Code
//************************************************************************
//ENTERFRAME
void loop() {
long error = 0;
int sample_cnt = 10;
unsigned long sample = 0;
unsigned long duration = 0;
float start_time = micros();
for (int i=0; i <= sample_cnt-1; i++){
sample = pulseIn(pin, LOW,60000000); //needs timeout or it does time out
duration = duration + sample;
Serial.print( i+1 ); //removing these serial commands, makes the program run slower, why?
Serial.print( ": " );
Serial.println(float(sample)/1000000,3);
if( sample==0){
error++;
};
} //END
float time_end = (micros()-float(start_time))/1000000;
Serial.print( "Sample Count: ");
Serial.print( sample_cnt );
Serial.print( " || Time: ");
Serial.print( time_end,3 );
Serial.print( " || Duration: ");
Serial.print(float(duration)/1000000,3);
Serial.print(" || Error: ");
Serial.print(error);
Serial.print(" || VCC: ");
Serial.print(float(readVccB())/1000,3);
Serial.print(" || TEMP: ");
Serial.println(float(GetTemp()),2);
//***********************************************
//CLOCK SPEED IS THE SAME : THIS IS FINE
long unsigned startTime = 0;
long unsigned endTime = 0;
long unsigned fullTime = 0;
startTime = millis();
benchmark();
endTime = millis();
fullTime = endTime - startTime;
Serial.print(" >> BENCHMARK FOR CLOCK SPEED: ");
Serial.println(fullTime);
}