12 Replies Latest reply on Jul 5, 2014 3:19 PM by Blackcode



      Is there a trick to getting the "pulseIn" method to work?


      On Win7, attempting to "Verify" the Ping example in the IDE from the path "arduino-1.5.3\examples\06.Sensors\Ping\Ping.ino" results in the following error message...


      Ping.ino: In function 'void loop()':

      Ping.ino:53:35: error: 'pulseIn' was not declared in this scope

        • 1. Re: pulseIn

          pulseIn() is not implemented yet for Galileo.

          This has already been addressed in a post on this forum


          As a general rule of thumb, it's good forum etiquette to use the search function on the forum before you post your question- as it may have already been answered!



          • 2. Re: pulseIn

            Let's try this then...


            Click the "Intel Makers Community" header.  In the "Search ONLY in Makers Community" box, type "pulseIn".  On my win7 machine using IE10, I get nothing back.


            As a general rule of thumb, it's good forum etiquette to not assume your fellow community members are idiots.

            • 3. Re: pulseIn

              yeah that doesn't work. Annoying, I know.

              But it works in the sub- categories, like Programming, hardware etc. which is how I found it.

              I don't assume you're an idiot. Just trying to be informative and helpful


              1 of 1 people found this helpful
              • 4. Re: pulseIn

                I have an ultrasonic sensor, and I assume this is what you were trying to use. HC-SR04


                I, too, tried to use pulseIn() and got compile errors. I found this thread after a Google search. I also found "newping," which said it resolved errors with pulseIn()... but not for Quark/Galileo. Newping uses direct hardware calls to port registers and thus won't work on Quark.



                /home/[user]/arduino/libraries/NewPing/NewPing.h:113:20: fatal error: avr/io.h: No such file or directory

                compilation terminated.


                "avr/io.h" is specific to the ATMega/etc. Arduino hardware and won't work with Quark.


                Just wanted to post for community knowledge. Still don't know how to use my ultrasonic sensor with Quark/Galileo. Will post more if I figure it out. Maybe I have to figure out how to emulate or simulate pulseIn()?

                • 5. Re: pulseIn

                  I have a pulseIn function alternative


                  #define trigPin 2
                  #define echoPin 3

                  void setup() {

                  Serial.begin (9600);

                  pinMode(trigPin, OUTPUT_FAST);
                  pinMode(echoPin, INPUT_FAST);

                  void loop() {
                  long duration, distance;
                  digitalWrite(trigPin, LOW); // Added this line
                  delayMicroseconds(2); // Added this line

                  digitalWrite(trigPin, HIGH);
                  // delayMicroseconds(1000); - Removed this line
                  delayMicroseconds(10); // Added this line
                  digitalWrite(trigPin, LOW);

                  duration = pulseIn2(echoPin, HIGH);
                  distance = (duration/2) / 29.1;
                  if (distance >= 200 || distance <= 0){
                  Serial.println("Out of range");
                  else {

                  Serial.println(" cm");


                  unsigned long pulseIn2(uint8_t pin, uint8_t state) {

                      unsigned long pulseWidth = 0;
                      unsigned long loopCount = 0;
                      unsigned long loopMax = 5000000;

                      // While the pin is *not* in the target state we make sure the timeout hasn't been reached.
                      while ((digitalRead(pin)) != state) {
                          if (loopCount++ == loopMax) {
                              return 0;

                      // When the pin *is* in the target state we bump the counter while still keeping track of the timeout.
                      while ((digitalRead(pin)) == state) {
                          if (loopCount++ == loopMax) {
                              return 0;

                      // Return the pulse time in microsecond!
                      return pulseWidth * 50; // Calculated the pulseWidth //this is the value to be tweaked

                  • 6. Re: pulseIn


                    That seem like a good alternative!

                    My two cents: use IO2\3 in fast mode for Trig and Echo (OUTPUT_FAST, INPUT_FAST), fine tuning delays and maybe the loops and it should work nicely

                    If it works please share the code.

                    • 7. Re: pulseIn

                      Thansk ItMo,

                      I tried this with the fast IOs and I was able to get it to responed. I changed the conversion from 29.1 to 4.8 and it seems to give the correct distance.

                      I don't know if that is the proper to convert it. If not the proper way, please let me know.

                      • 8. Re: Re: pulseIn

                        You should not modify inside the ping sensor code.

                        You should tune the "return pulseWidth * 2.36; // Calculated the pulseWidth"

                        I found that 50 is a close value


                        The speed of sound is 340 m/s or 29 microseconds per centimeter (Time difference per sound path distance ms per meter or length millimeters time of arrival milliseconds calculation calc…)


                        of air in °C
                        Speed of sound
                        c in m/s
                        Time per 1 m
                        Δ t in ms/m
                        • 9. Re: pulseIn

                          Thanks for this great contribution

                          could connect more than one HS-SR04 sensor with your code?

                          • 10. Re: pulseIn

                            pulseIn function is available from 1.02 firmware update but with this code did not managed to make it work yet...



                            #define trigPin 2

                            #define echoPin 3


                            void setup() {

                              // initialize serial communication:




                            void loop()


                              // establish variables for duration of the ping,

                              // and the distance result in inches and centimeters:

                              long duration, inches, cm;


                              // The sensor is triggered by a HIGH pulse of 10 or more microseconds.

                              // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

                              pinMode(trigPin, OUTPUT_FAST);

                              digitalWrite(trigPin, LOW);


                              digitalWrite(trigPin, HIGH);


                              digitalWrite(trigPin, LOW);


                              // Read the signal from the sensor: a HIGH pulse whose

                              // duration is the time (in microseconds) from the sending

                              // of the ping to the reception of its echo off of an object.

                              pinMode(echoPin, INPUT_FAST);

                              duration = pulseIn(echoPin, HIGH);


                              // convert the time into a distance

                              inches = microsecondsToInches(duration);

                              cm = microsecondsToCentimeters(duration);



                              Serial.print("in, ");








                            long microsecondsToInches(long microseconds)


                              // According to Parallax's datasheet for the PING))), there are

                              // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per

                              // second).  This gives the distance travelled by the ping, outbound

                              // and return, so we divide by 2 to get the distance of the obstacle.

                              // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf

                              return microseconds / 74 / 2;



                            long microsecondsToCentimeters(long microseconds)


                              // The speed of sound is 340 m/s or 29 microseconds per centimeter.

                              // The ping travels out and back, so to find the distance of the

                              // object we take half of the distance travelled.

                              return microseconds / 29 / 2;


                            • 11. Re: pulseIn

                              Hi Blackcode,


                              Can you try setting the pinMode for the echoPin to INPUT_FAST inside setup() instead of the inside  loop().


                              also delayMicroseconds() does not work reliably for timings less than 200 uS.
                              You can replace this with a while loop.


                              int wait = 2;

                              unsigned long t1;

                              unsigned long t2;


                              t1 = micros();

                              t2 = t1;

                              while((t2-t1) < wait)


                                   t2 = micros();


                              • 12. Re: pulseIn

                                I have moved Input and Output fast out of the main loop and converted the micro delay to the while loops but nothing works. answers are 0 cm, 0 inches


                                the same example works flawless on an arduino yun.