8 Replies Latest reply on Sep 17, 2014 8:22 PM by Syah

    How to Play Tone on Intel Galileo?

    Syah

      Hi,

       

      Previously on my Arduino, I use these codes below to play tone. The piezo speaker positive pin is connected to pin 10. While the negative pin is connected to pin 9.

       

      #include <toneAC.h>
      
      int frequency = 10000; //19000
      
      void playFrequency() {  
           toneAC(frequency);
      }
      
      void setup() { }
      
      void loop() {  
           playFrequency();
      }
      

       

      I am trying to achieve the same thing on the Galelio. However, above codes cannot be executed because ToneAC library is not supported by the Galileo. So, I thought the normal tone would work

       

      void setup() { } 
      
      void loop() {   
           tone(8, 1000); 
      }
      

      Well, sadly, it does not work too. I did a little googling and came across this link. https://communities.intel.com/message/214563 So, it seems that tone() function does not exist. As such, how can I play the piezo speaker with the Galileo?

       

      The main reason I used ToneAC on my Arduino previously is because the tone is much smoother at higher frequency as I wanted it to generate tones between 19kHz - 22kHz.

       

      Kindly advice me on how do I achieve the same thing with the Galileo?

       

      *I accidently marked this as answered*

       

      Thank you in advance.

        • 1. Re: How to Play Tone on Intel Galileo?
          Clayton Hofrock

          The tone library uses interrupts. The way interrupts are used on Amtel (Arduino processors) is vastly different from how they are used on x86 processors (Galileo processor). The interrupt library has not been ported to date. But that is just one of the hurdles.

           

          In addition Galileo has a relatively slow I/O speed (when compared with Arduino).  Essentially, if you need to read an input or drive an output, more than a few times a second, the Galileo might not be able to keep up.

           

          Just toggling a pin (which is essentially what you are doing in order to make a tone), you should be able to get up to 230Hz, which is audible, but this is just a free running script that constantly toggles a digital pin, no other code running at the same time.

           

          https://communities.intel.com/message/208688#208688

           

          There are two pins which can go faster. The thread above talks about that, and the speeds that can be achieved. Again this is code that is doing nothing but toggling a pin. If you need to run other code at the same time, it will not work.

           

          In theory you could toggle a pin, at the right frequency and get a tone out.

           

          It might be easier to get a USB audio device working. Although, I don't know of anyone who has done that.

          1 of 1 people found this helpful
          • 2. Re: How to Play Tone on Intel Galileo?
            Syah

            Oh cool! Did'nt know that by just toggling it the pin, you can get the tone.

             

            //I would assume the frequency generated here is 230kHz? But then on my phone, spectrum anaylzer app, it shows between 100Hz - 1Khz

            //Having said that, that's mean if i were to generate a tone, I just need to play with the delay? Is there an example of a tone generated with this method@@?

             

            void setup() {

              // put your setup code here, to run once:

              pinMode(2, OUTPUT);

            }

             

            void loop() {

              // put your main code here, to run repeatedly:

              digitalWrite(2, HIGH);

              delay(1);

              digitalWrite(2, LOW);

            }

             

             

            //Supposed to generate 477kHz. Is it possible to generate between 19-22kHz? Or is it fixed at 477kHz?

            // Connecting positive pin of the buzzer to pin 2, and negative pin to gnd, make it generate some retarded noise. Haha

            void setup(){

                pinMode(2, OUTPUT_FAST);

            }

             

            void loop() {

                register int x = 0;

                while(1){

                    digitalWrite(2, x);

                    x =!x;

                }

            }

            • 3. Re: How to Play Tone on Intel Galileo?
              DinoT_Intel

              If you have the latest version of the IDE, you can just use tone().

              All it does is basically toggle pins at the desired frequency.

               

              It works well for Galileo Gen2 but for Galileo Gen1 it can only really work well for pins 2 and 3.

              If you are going to use Gen1, you will also have to modify the tone library since there is a bug in it.

              This bug will be fixed in the next IDE release.

              in \hardware\arduino\x86\cores\arduino\Tone.cpp replace all instances of

               

              pinMode(_pin, OUTPUT) to pinMode(_pin, OUTPUT_FAST)

               

              to use tone:

              tone(pin, frequency);

              1 of 1 people found this helpful
              • 4. Re: How to Play Tone on Intel Galileo?
                Syah

                I see. I am using Gen 1, with the latest IDE. I shall proceed to modify Tone.cpp in awhile.

                 

                If it really works, that's mean I can use up tp 477kHz? (ie. tone(2, 477000))

                 

                How about ToneAC? I don't see any pinMode() under toneAc.cpp.

                • 5. Re: How to Play Tone on Intel Galileo?
                  DinoT_Intel

                  you probably would not be able to achieve 477 Khz with the tone library.

                   

                  However, pins 2 and 3 are capable of toggling at 2.9 Mhz when using fastGpioSCDigitalWriteDestructive()

                   

                  if you want something simpler you can use fastDigitalWrite()

                   

                  while(1)

                    {

                      fastDigitalWrite(3, HIGH);

                      fastDigitalWrite(3, LOW);

                    }

                   

                  produces a 655 Khz waveform. so you can tweak this a bit to get something close to 477khz.

                  Do note however that it will not be 100% accurate since the sketch is not running in a real time system.

                   

                  ToneAC is not supported on the Galileo at all.

                  1 of 1 people found this helpful
                  • 6. Re: How to Play Tone on Intel Galileo?
                    Syah

                    I just realized that I can use tone(pin, frequency) without even modifying Tone.cpp.

                     

                    However, If I use the tone() function, I cannot hear anything.

                     

                    tone(2, 1000); //It does not produce any sound. No sound at all.

                     

                    And if I were to comment the above and compile:

                     

                    digitalWrite(2, HIGH); //cannot set frequency

                    digitalWrite(2, LOW);

                     

                    The IDE will just hang and get a timeout error.

                    • 7. Re: How to Play Tone on Intel Galileo?
                      DinoT_Intel

                      If you do not modify the tone.cpp then you will be limited to 230 Hz on pins 2 and 3.

                      also you should not call digitalWrite() on the same pin that you used for tone().

                      • 8. Re: How to Play Tone on Intel Galileo?
                        Syah

                        That's another problem.

                         

                        Even with 230 Hz, I should hear some tone right? Which there is no tone in this case.

                         

                        If I modify tone.cpp, I will not be able to generate tone using digitalWrite().

                         

                        pinMode(_pin, OUTPUT_FAST); //OUTPUT

                        pinMode(tPin, OUTPUT_FAST); //OUTPUT