Tuesday, December 24, 2013

HC-SR04: avoiding oversampling errors




We have seen in previous posts (here and here) that if the time between two measurements is too short then the sensor outputs a 0 to signal a measurement error. We call these kind of errors ‘oversampling’, i.e. sampling too frequently beyond the capacity of the sensor.

From data collected in this post, we have seen that a delay of 26ms is enough to solve the problem. This note tries to answer the following question:

        can we minimize this time?

The idea

We have conceived a bisection procedure which will try to determine the minimal delay to use in order to avoid oversampling. The idea is to start with delay D=26ms. Therefore the search interval will be

I0=(a0,b0)=[0,D]

Then, if there are no oversampling errors, I_0 is halved and a new sample batch is run. If at some stage i oversampling errors are more than a certain threshold ε, we search in the interval

(ai+1,bi+1)=[b,(b-ai)/2]   for i>1

otherwise we keep on halving the interval. The procedure is repeated until |ai-bi|=1ms.

At this point, a second procedure will search for the right number of us. According to Arduino specifications (see here), this second procedure is stopped if the length of the interval is less than 3us (i.e. |ai-bi|<=3us).

The program
The program which performs the experiment is given at the end of this post. Let us comment its parts. First of all, the constants

#define SAMPLESIZE 1000
#define NUMTESTS 5

are to indicate that we are going to perform 5 (NUMTESTS) batches and that each batch will consist of 1000 (SAMPLESIZE) tests. A test fails if the percentage of errors (zeroes) which are measured in a batch is greater than a TOLERANCE. Here the tolerance is fixed at 1% (see below).

#define TOLERANCE .01

Finally the following two constants define the initial length of the search interval for milliseconds and for microseconds.

#define INITDELAYMS 26
#define INITDELAYUS 1000

As we have already said, the value for INITDELAYMS is fixed to 26ms because of previous experiences. INITDELAYUS is naturally fixed to 1000us since it is the amount of micros in a millisecond.
The following portion of code is taken directly for here and just performs one read of the sensor. Remark that echo pin is connected to Arduino pin 2 and trigger pin is connected to Arduino pin 4.

Wednesday, December 18, 2013

HC-SR04: what is the distribution law for the frequencies of values? (Part 2)





Recalling the conclusions of our last experiment with the HC-SR04 (you can find it here), we shall repeat the sampling batch leaving enough time to the HC-SR04 so to avoid errors given by oversampling. Recalling the computations made here, we know that maximal time for a round trip is 25000us, that is to say 25 milliseconds. We can overestimate to 26 millis to be absolutely sure.

Therefore we modify the setup function in our former program as follows:


void setup() {
  
  uint16_t i;
  
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // wait for serial port to connect. Needed for Leonardo only
  while (!Serial); 

  Serial.println("*******************************");
  
  for(i=1; i<SAMPLESIZE;i++) {
    delay(26);
    Serial.println(readoutSensor());
  }
  Serial.println("*******************************");

  Serial.println("Sample generation terminated.");
}
(Remark that here output is on the Serial Monitor and no more on the SD card.)

Friday, December 13, 2013

HC-SR04: what is the distribution law for the frequencies of values? (Part 1)




In this small note we are going to study the distribution of the values returned by our preferred ultraso-nic sensor, namely, the HC-SR04. We suspect that it is a normal distribution. As suggested in [1, pag. 18 and ff], we use the following steps to verify this hypothesis:

1) generate sample data
2) compute the mean and standard deviation of sampled data
3) compute the kurtosis and skewness
4) compute standard error of kurtosis and skewness
5) compute the z-score for kurtosis and skewness
6) compare the above values with the corresponding ones of the normal distribution



Generate the sample data

The first operation is to obtain a large enough sample data. The idea to fix the obstacle at 1 meter from the sensor and then take ten thousand measurements (this is hopefully large enough). Since one cannot open files directly on a PC using Arduino, we are going to user the card reader (see THIS post) and store data on the SD card. The following short program will do the job.

Sunday, December 1, 2013

Lost treasures 2: a strange LCD 1x16 (or 2x8?)






This was an electric typewriting machine.

While disassembling an old electric typewriter which was sleeping since years in a corner, I’ve found this LCD.




The strange LCD screen found.