element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Blog Controlling Buzzer using Analog input
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: rictrajano
  • Date Created: 4 Apr 2018 8:42 AM Date Created
  • Views 12052 views
  • Likes 3 likes
  • Comments 12 comments
Related
Recommended

Controlling Buzzer using Analog input

rictrajano
rictrajano
4 Apr 2018

Hello,

 

I’ve been an electronics curious since a child but never got in too deep. I’ve recently bought an Arduino, start playing with it and realised the potential of this device. I started thinking and it would be the perfect solution for a project that I’ve been wanting to do for a long time.

 

I have a laser that mesures distances with a board which I can make it output a Voltage depending on how far it is from an object, and the final objecting is activating a buzzer whenever in reaches a certain threshold. I was planing on having two different signals for different distances e.g at 5m would start to buzz every half a second in a certain frequency (700Hz) and when it would reach 2m would buzz every 1/8 of a second in the same frequency. Of course the distances in this case would be read as voltages for the arduino.

 

So far I manage to make the Arduino read the voltage fluctuation and already created a project where I make the buzzer work in diferent timer intervals and frequencies. I’m only missig the code to do everything interact with each other. I’ve been fideling around with it but couldn’t manage to find a solution.

 

Does anyone would know how could I be able to achieve this amazingly complex engineering project. image

 

Thank you very much for your help

  • Sign in to reply

Top Comments

  • mcb1
    mcb1 over 7 years ago in reply to rictrajano +2
    rictrajano Thanks for the code, and I've now discovered the tone command. Tone drives the appropriate pin (buzzer) with a 50% duty cycle at the desired frequency. You wish to generate this tone when the…
  • rictrajano
    rictrajano over 7 years ago +2
    mcb1 So finally I managed to make the code work. The only thing that still is making me pull my hair is that the readings in the Arduino serial monitor are only refreshed as fast as the Buzzer delay. So…
  • rictrajano
    rictrajano over 7 years ago in reply to beacon_dave +1
    I’ve used some conditions and I’ve managed to make it work with a LED but for the buzzer I need to specify the tone and the delay between buzzes abd that’s when tge conditions stop working and I just get…
  • mcb1
    mcb1 over 7 years ago in reply to beacon_dave
    You probably want to look at replacing the delay() functions with the millis() timer function

    It takes time to run the tone, so I'm not sure it has any advantages.

     

     

     

    I've used Millis() as the timer in many projects so I understand the functioning well, and in this case I didn't see it would help.

    The issue is the Serial.Println which is to see the data, but it needs to be more clever and only see a change or every tenth data/2 secs or something.

     

     

    Mark

     

     

    BTW my next Open Arduino is for a project where it did two different functions in the Arduino and no delays were harmed in the making of the sketch. image

     

     

     

     

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • beacon_dave
    beacon_dave over 7 years ago in reply to rictrajano

    You probably want to look at replacing the delay() functions with the millis() timer function. This allows your loop() to run continuously without pauses so your Serial.println update is not affected by the delay() being used to create the beeping interval.

     

    Otherwise each time your code hits a delay() function it pauses until the delay time outs after which your serial monitor then get updated. This is sometimes referred to as 'blocking'.

     

    By using millis() you can compare the current time with a stored value and use that to determine the delay.

    ttps://www.arduino.cc/reference/en/language/functions/time/millis/

     

    Perhaps take a look at this example as to how you can use millis() instead of delay()

    https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

     

    Alternatively you could consider changing the tone of a steady audio signal instead of the beeping interval as an indication of distance to object. )

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • mcb1
    mcb1 over 7 years ago in reply to rictrajano

    rictrajano

     

    Well done on the code, apart from the hair pulling I'm sure you've learnt heaps.

    If someone has a clue of how to get it to display the readings at a fast rate all the time I'll appreciate the help.

    Firstly you can change the serial speed.

    Each numerial is 8 bits of data and that takes time to send, so bump the Serial.begin from 9600 to 115200 (don't forget to set the terminal to 115200)

     

    analogRead takes time (about 104uS) so thats not really holding you up, but the Serial.print will be.

     

    You can very that by commenting it out and seeing how quick it responds.

    If you need to have data in your final version, you could simply send the output only when it changes which of the if statements you are in of one of 4 states.

     

    Even if you speed up this, you're going to be overwhelmed by the data coming in, so you may want to think about how useful it is, or what you want in the final version.

     

     

     

    Do you have any specs on what rate your laser ranger does the detection and conversion to a voltage?.

    It could be that this is also adding what appears to be a delay in your video.

     

     

     

    Mark

     

     

     

     

    BTW

    Here is a chapter from Arduino Cookbook.

    https://www.safaribooksonline.com/library/view/arduino-cookbook/9781449399368/ch04.html

    it is something I highly recommend as it tackles problems from the "I have this issue what can I use" rather than just explaining what each command does.

     

    If you hunt around the internet you'll find free copies and this includes the later version.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • rictrajano
    rictrajano over 7 years ago

    mcb1

     

    So finally I managed to make the code work. The only thing that still is making me pull my hair is that the readings in the Arduino serial monitor are only refreshed as fast as the Buzzer delay. So if the distance detected is not activating the buzzer, the reading is very fast, but as soon as the buzzer is activated, I only get the readings each time the buzzer sounds, which means depending on the delay of the sound the faster or slower I get the readings.

     

    If someone has a clue of how to get it to display the readings at a fast rate all the time I'll appreciate the help.

     

    Here's the working code and the video of it working. I hope it helps someone on a similar quest.

     

    Thanks for all the help

     

    int buzzer = 11;
    // the setup routine runs once when you press reset:
    void setup() {
      // initialize serial communication at 9600 bits per second:
      Serial.begin(9600);
      pinMode(buzzer, OUTPUT);
    }
    
    
    
    
    
    
    
    
    // the loop routine runs over and over again forever:
    void loop() {
      // read the input on analog pin 0:
      int sensorValue = analogRead(A0);
      // print out the value you read:
      float voltage = sensorValue * (3.3 / 1023.0);
      if  (voltage < 0.05)
           { 
            tone(buzzer, 700);
            delay(75);
            noTone(buzzer); 
            delay(75); 
           }   
           if (voltage >= 0.05 && voltage <= 0.1)
           {
            tone(buzzer, 700);
            delay(150);  
            noTone(buzzer); 
            delay(150);   
           }
           if (voltage > 0.1 && voltage <= 0.2)
           {
            tone(buzzer, 700);
            delay(200);  
            noTone(buzzer); 
            delay(200);   
           }
           if (voltage > 0.2)
           {
            noTone(buzzer);
           }
      Serial.println(voltage); 
    }

     

    This video is unavailable.
    You don't have permission to edit metadata of this video.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • mcb1
    mcb1 over 7 years ago in reply to rictrajano

    rictrajano

     

    Ahh ...  I made the assumption you were reasonably familar with Arduino IDE.

     

    I only posted the loop() part of the code.

    The first part of the code sets up the compiler that generates the file that gets dumped into the Arduino, so it's necessary.

     

    Well done though for correctly identifying the error message and sorting out the solution.

     

     

     

     

    I'm going to give you some hints rather than the actual code ... trust me you'll learn more.

     

    You have three different detections

    • Distance greater than 3v
    • Distance between 3 and 1.5
    • Distance below 1.5

     

    You can have two if statements and an else ... basically do nothing when its above 3v except add a delay.

    You can use switch case ... you need to have the 1.5 as the first case.

    or you could even use map and have it change the figure to a frequency. ... you can have a couple to limit the frequency variation

     

    In your first comment, you wanted to change the beeping speed, so I'd suggest removing the delay(150) and add it into the "I'm not doing anything" phase for the decision process.

    You can then use the tone duration to control the beeping rate as you've done in your revised code (line 21)

     

     

    I'd play with the code you have to decide what frequency and duration you want so you have the figures to use in the next sketch.

    Have a go and come back if you've got some questions.

     

    Mark

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube