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

    3 Pin ultrasonic sensor in galileo

    Cadeq

      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
          DiegoV_Intel

          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.

           

          Regards,

          Diego.

          • 2. Re: 3 Pin ultrasonic sensor in galileo
            Cadeq

            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
              DiegoV_Intel

              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.

               

              Regards,

              Diego.

              • 4. Re: 3 Pin ultrasonic sensor in galileo
                Cadeq

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

                • 5. Re: 3 Pin ultrasonic sensor in galileo
                  DinoT_Intel

                  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.

                   

                  Thanks,

                  Dino Tinitigan

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

                    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

                    {

                    public:

                    Ultrasonic(int pin);

                      void DistanceMeasure(void);

                    long microsecondsToCentimeters(void);

                    long microsecondsToInches(void);

                    private:

                    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);

                    delayMicroseconds(2);

                    digitalWrite(_pin, HIGH);

                    delayMicroseconds(5);

                    digitalWrite(_pin,LOW);

                    pinMode(_pin,INPUT);

                    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()

                    {

                    Serial.begin(9600);

                    }

                    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.print(RangeInCentimeters);//0~400cm

                    Serial.println(" cm");

                    delay(100);

                    }

                    ************************

                    • 7. Re: 3 Pin ultrasonic sensor in galileo
                      DinoT_Intel

                      Hi Cadeq,

                       

                      This should work:

                       

                      galPing.png

                       

                      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
                        Cadeq

                        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
                          DiegoV_Intel

                          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?

                           

                          Regards,

                          Diego.

                          • 10. Re: 3 Pin ultrasonic sensor in galileo
                            Cadeq

                            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);

                             

                              Timer1.initialize(SEC);

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

                            }

                             

                            void pingUltra()

                             

                            {

                              fastDigitalWrite(trigPin, LOW);

                              delayMicroseconds(2);

                              fastDigitalWrite(trigPin, HIGH);

                              delayMicroseconds(10);

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

                              pstart=micros();

                             

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

                              pstop=micros();

                            }

                             

                            double media(int x)

                            {

                              double med=0;

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

                              {

                                pingUltra();

                                med+=((pstop-pstart)/57)+4;

                              }

                              return med/x;

                            }

                             

                            void loop()

                              double dist=media(10);

                              if(dist!=0)

                              {

                                  Serial.print("CM: ");

                                  Serial.println(dist);

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

                                 {

                                      analogWrite(speakerPin,200);

                                      delayMicroseconds(tt);

                                      analogWrite(speakerPin,0);

                                      delayMicroseconds(tt);

                                 }

                                    analogWrite(speakerPin,00);

                              }

                              delay(1000);

                            }

                             

                            **********************************

                            • 11. Re: 3 Pin ultrasonic sensor in galileo
                              microwave_safe_bowl

                              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?