11 Replies Latest reply on Feb 27, 2016 6:54 PM by microwave_safe_bowl

    3 Pin ultrasonic sensor in galileo


      Hi everyone


      At this moment i am trying to connect several ultrasonic sensor to my galileo. I know that pulseIn doesn't work in galileo. I make work a HC-SR04 sensor but what i need is detect distance a 3 pin sensor distance (This model: Ultra Sonic range measurement module - Wiki)


      I looked the another posts about pulseIn or ultrasonic sensor and please, don't say me connect a ultrasonic sensor to arduino uno and this to galileo. it's not possible.


      I tried the same code that i used for HC-SR04 but doesn't work.


      Do you have any idea about how to make  3 pin ultrasonic sensor detects distance in galileo?


      Thank you



      PS: Sorry if my english is wrong.

        • 1. Re: 3 Pin ultrasonic sensor in galileo

          Hi Cadeq,


          Have you checked the Release Notes for Galileo? According to the documentation the pulseIn() function is now supported by the Galileo Board. Take a look to the document, you can check it here: Intel® Galileo Gen 2 Release Notes in section 1.4.2.




          • 2. Re: 3 Pin ultrasonic sensor in galileo

            Thank you for your answer.


            Sorry for if i dont checked the release notes. Now pulseIN compiles. But at this moment, the ultrasonic sensor gives me always 0. I use the code of the oficial wiki of sensor (Ultra Sonic range measurement module - Wiki)


            I tried the same code in arduino UNO and it works.


            I put the pin in the pin 2 because the release notes says that first gen of galileo works properly in this pin. Also i tried change the code to put INPUT_fast and  OUTPUT_fast and still giving me the same result

            • 3. Re: 3 Pin ultrasonic sensor in galileo

              Hi Cadeq,


              There is a new IDE version available (1.0.4). I'd suggest you to update your current IDE if it is not the 1.0.4 yet and try the pulseIn() function again.




              • 4. Re: 3 Pin ultrasonic sensor in galileo

                I updated the IDE version. The program compile but the sensor always give me 0.

                • 5. Re: 3 Pin ultrasonic sensor in galileo

                  Hi Cadeq,


                  Can you share some of your code and circuit diagram?
                  There is a specific way to use the ping sensors for Galileo and Edison.

                  You will need 2 digital pins, one for out put and another for input.


                  The reason pulseIn() is returning 0 is likely because it is timing out.

                  If you just use the code for regular arduino's it will not work because the Galileo/Edison is not fast enough in switching a pin from output to input.

                  This results with pulseIn() missing the pulse output of the ultrasonic sensors.



                  Dino Tinitigan

                  1 of 1 people found this helpful
                  • 6. Re: 3 Pin ultrasonic sensor in galileo

                    The sketch that i try come from the wiki of ultrasonic sensor Ultra Sonic range measurement module - Wiki

                    I only change the input ping to 3. I read in the release notes that for 1º gen the pings 2 and 3 give accurate results.

                    The circuit diagram is easy. Only the ultrasonic sensor and the arduino galileo. Signal pin conected to ping 3, vcc to 5v and gnd to gnd.


                    #include "Arduino.h"
                    class Ultrasonic



                    Ultrasonic(int pin);

                      void DistanceMeasure(void);

                    long microsecondsToCentimeters(void);

                    long microsecondsToInches(void);


                    int _pin;//pin number of Arduino that is connected with SIG pin of Ultrasonic Ranger.

                      long duration;// the Pulse time received;


                    Ultrasonic::Ultrasonic(int pin)


                    _pin = pin;


                    /*Begin the detection and get the pulse back signal*/

                    void Ultrasonic::DistanceMeasure(void)


                      pinMode(_pin, OUTPUT);

                    digitalWrite(_pin, LOW);


                    digitalWrite(_pin, HIGH);




                    duration = pulseIn(_pin,HIGH);


                    /*The measured distance from the range 0 to 400 Centimeters*/

                    long Ultrasonic::microsecondsToCentimeters(void)


                    return duration/29/2;


                    /*The measured distance from the range 0 to 157 Inches*/

                    long Ultrasonic::microsecondsToInches(void)


                    return duration/74/2;



                    Ultrasonic ultrasonic(3);

                    void setup()




                    void loop()


                    long RangeInInches;

                    long RangeInCentimeters;

                    ultrasonic.DistanceMeasure();// get the current signal time;

                      RangeInInches = ultrasonic.microsecondsToInches();//convert the time to inches;

                    RangeInCentimeters = ultrasonic.microsecondsToCentimeters();//convert the time to centimeters

                    Serial.println("The distance to obstacles in front is: ");

                    Serial.print(RangeInInches);//0~157 inches

                    Serial.println(" inch");


                    Serial.println(" cm");




                    • 7. Re: 3 Pin ultrasonic sensor in galileo

                      Hi Cadeq,


                      This should work:




                      Just wire it like above using a 1k resistor


                      Use the attached the modified code: pingTest.ino

                      • 8. Re: 3 Pin ultrasonic sensor in galileo

                        Sorry guys. It till giving me 0In 0Cm, I wired it like the picture using a 1K resistor and used the attached sketch. I give up with the sensors of 3 pins. I will use the 4 pins sensors. Thank you all for your help

                        • 9. Re: 3 Pin ultrasonic sensor in galileo

                          Hi Cadeq,


                          Sorry for the delay. I have just tried the code attached by DinoT_Intel, and I was able to set my 3 pin ultrasonic sensor to work. I wired the sensor as DinoT_Intel suggested and it worked with Galileo Gen 1 and Galileo Gen 2. Were you able to set the 4 pin sensor to work?




                          • 10. Re: 3 Pin ultrasonic sensor in galileo

                            I tried again the answer of DinoT_Intel and still giving me 0. Maybe is the sensor the problem. Maybe the ultrasonic sensor of seeedstudio doesn't work with that code, i don 't know.


                            My 4 pin sensor is a HC-SR04 and this is the code that i use is this. I don't remember where I get it but it works.



                            #include <TimerOne.h>

                            #define speakerPin 9

                            #define trigPin 3

                            #define echoPin2 2


                            unsigned long SEC=1000; //1sec = 10^6microsecond

                            long  pstart=0;

                            long  pstop=0;

                            boolean got=false;


                            int freq=500; //in Hz (1/sec)

                            int tt=SEC/freq/2;


                            void setup() {

                              Serial.begin (9600);


                              pinMode(trigPin, OUTPUT_FAST);

                              pinMode(echoPin2, INPUT_FAST);



                              //you need at least 15us + ping time. it didnt work below 1000us for me for below code



                            void pingUltra()



                              fastDigitalWrite(trigPin, LOW);


                              fastDigitalWrite(trigPin, HIGH);


                              fastDigitalWrite(trigPin, LOW);// NOT checking for LOW. it seemed to hang here if i used it.



                              while(digitalRead(echoPin2)==HIGH ){}




                            double media(int x)


                              double med=0;

                              for(int i=0; i<x;i++)





                              return med/x;



                            void loop()

                              double dist=media(10);



                                  Serial.print("CM: ");


                                  for(int i=0;i<10;i++)













                            • 11. Re: 3 Pin ultrasonic sensor in galileo

                              I am actually having the same problem as OP.  I have read in some places that the pin switching is slow on the Edison board.  I am pretty new to Arduino but I am an engineer by trade and job so I understand this kind of thing.  I have an Intel Edison Arduino board and a Parallax Ping Sensor (3 pin).  I tried your diagram plus your code and I also just get zeros for everything.  I don't have a 4 pin sensor but I am going to get one and try that. 


                              Did you ever make any head way or could you reproduce this problem?