element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • 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
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • 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 12920 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…
  • rictrajano
    rictrajano over 7 years ago in reply to mcb1

    Ok, so I try out your code and it didn't work. It said something about the buzzer not being declared. I went ahead and declared with "int buzzer = 11" but still didn't work.

    I've then tried to put the part of your code that made sense to me to be altered and finally managed to make it work. Success.

    The next phase of my project is having a second alarm for a different distance / voltage. For example from 1st Alarm - from 3v to 1.5v, 2nd Alarm - everything below 1.5V. How would you code this condition?

     

    Thank you so much for your help

     

    Here's the code that made it happen:

     

    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 < 1.5)
           { 
            tone(buzzer, 700);
            delay(150);
           }    
      //delay(500);  
      noTone(buzzer); 
      delay(150);  
      Serial.println(voltage); 
      //delay(1);        // delay in between reads for stability 
    }

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

    Thanks so much for your input. I’ll test the code tomorrow and will give you the feedback of how it went. Fingers crossed    

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

    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 analogue reading is greater than 2 (line 16), but you're also driving the pin into a state as an output.

     

    I suggest trying this code instead.

    // 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 > 2) 
         {
             tone(buzzer, 700, 500); 
         }  
       //delay(500);  
      noTone(buzzer); 
      delay(500);  
      Serial.println(voltage); 
      //delay(1);        // delay in between reads for stability 
    }

    I've removed the 500 mS delay and used it for the tone duration instead.

    I also removed the delay(1) as you already have a 500 mS delay in the loop at Line13.

     

    Note that with the 500mS tone and the delay the loop runs at 1 second, so you'll need to change your methods to suit this

    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

    However lets get it playing a note as you expect before we tackle the next part.

     

     

    I haven't tried the code and I haven't got an Arduino in front of me, but this should help you to sort out what you are after.

     

     

    Cheers

    Mark

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

    So this is the code that I've been working on. I got some of it in the Arduino examples and the other in a schematics for a buzzer on youtube. I then tried to put it together using some conditions but apparently I'm not the code genius that I thought I was after watching some youtube videos on coding image image

    Please don't be too harsh on me image

     

     

     

    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 > 2) digitalWrite(buzzer, HIGH);
      else digitalWrite(buzzer, LOW);
      tone(buzzer, 700);
      delay(500);
      noTone(buzzer);
      delay(500);
      Serial.println(voltage);
      delay(1);        // delay in between reads for stability 
    }
    
    @

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

    Post the code (select it in the post, then use the >> symbol and choose Syntax Highlighting and then C++)

     

    It will give us a better idea of what might be causing it.

     

    Mark

    • Cancel
    • Vote Up 0 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 © 2026 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