IotBeginner

Analog Sensor Buzzer Alert

Build an alert system that detects analog sensor readings and activates a buzzer when a specified threshold is reached.

Technologies Used

Arduino UnoAnalog SensorBuzzer

Analog Sensor-Based Buzzer Alert System

1. Introduction

This Arduino project reads an analog sensor through pin A0 and controls a buzzer connected to digital pin 3.

If the sensor reading is greater than 200, the Arduino sends PWM value 50 to the buzzer pin. Otherwise, it sends 0. The process repeats every second.

Note: The original code does not specify the sensor type. Select and connect a suitable analog-output sensor for your application.

2. Objectives

  • Read an analog sensor value from A0.
  • Compare the value with a threshold of 200.
  • Activate the buzzer output when the value is above 200.
  • Turn the buzzer output off when the value is 200 or below.
  • Repeat the check every second.

3. Required Components

ComponentQuantityPurpose
Arduino Uno or compatible board1Main controller
Analog-output sensor1Provides sensor readings
Buzzer1Gives an audible alert
Jumper wiresAs neededCircuit connections
Breadboard1Optional prototyping

4. Pin Connections

Sensor Connections

Sensor PinArduino Pin
Analog Output (AO)A0
VCCSupply specified by sensor
GNDGND

Buzzer Connections

Buzzer PinArduino Pin
Signal / PositiveDigital Pin 3
GND / NegativeGND

Important: Check the buzzer's voltage and current requirements. If it draws more current than an Arduino pin can safely provide, use a suitable transistor driver.

5. Complete Arduino Code

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

6. Code Explanation

6.1 Define the Buzzer Pin

[object Object]

This gives the name BUZZER_PIN to Arduino digital pin 3.

6.2 Setup Function

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
  • setup() runs only once when the Arduino starts.
  • pinMode() configures pin 3 as an output.
  • Serial communication is disabled because its initialization is commented out.

6.3 Read the Sensor

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
  • analogRead(A0) reads the voltage at analog pin A0.
  • On a typical Arduino Uno, the 10-bit ADC returns values between 0 and 1023.

6.4 Compare the Reading with the Threshold

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
  • If the reading is greater than 200, PWM value 50 is sent to pin 3.
  • If the reading is 200 or below, value 0 is sent.
  • A reading exactly equal to 200 follows the else condition.

6.5 Delay Function

[object Object],[object Object],[object Object],[object Object],[object Object]

The Arduino waits for 1000 milliseconds (1 second) before repeating the sensor check.

7. Important Buzzer Note

On a typical Arduino Uno, analogWrite() accepts values between 0 and 255.

A value of 50 represents approximately a 20% PWM duty cycle. It does not specify an audible sound frequency.

  • Active Buzzer: Can produce sound when supplied with an appropriate ON signal.
  • Passive Buzzer: Generally requires a tone frequency. Use tone(BUZZER_PIN, frequency) and noTone(BUZZER_PIN) instead of relying on analogWrite(..., 50) to select a pitch.

8. Working Principle

  1. Arduino starts and configures pin 3 as an output.
  2. The sensor supplies an analog voltage to A0.
  3. Arduino reads the sensor value.
  4. It compares the reading with the threshold of 200.
  5. If the reading is above 200, it outputs PWM value 50.
  6. If the reading is 200 or below, it outputs 0.
  7. Arduino waits one second and repeats the process.

9. Example Output

Sensor ReadingConditionBuzzer Pin Output
100Below threshold0
199Below threshold0
200Equal to threshold0
201Above threshold50
500Above threshold50
900Above threshold50

Note: The actual sound depends on the buzzer type and its driver circuit.

10. Optional Serial Monitoring

To display sensor readings in the Serial Monitor, uncomment the serial lines and initialize Serial communication.

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Select 9600 baud in the Arduino Serial Monitor.

11. Advantages

  • Simple and suitable for Arduino beginners.
  • Uses a threshold to trigger an alert.
  • Can be adapted to different analog-output sensors.
  • Requires only a small amount of code.

12. Limitations and Safety Notes

  • The original code does not identify the sensor type.
  • The threshold may require calibration according to the sensor and environment.
  • The program checks the sensor once per second.
  • PWM value 50 controls duty cycle, not buzzer pitch.
  • Do not exceed Arduino pin voltage or current limits.
  • Use a suitable driver circuit if required.
  • This prototype is not a certified safety alarm.

13. Conclusion

The Analog Sensor-Based Buzzer Alert System monitors an analog sensor connected to A0. When its reading rises above 200, the Arduino sends PWM value 50 to the buzzer pin. Otherwise, it sends 0.

This process repeats every second. The sensor and threshold should be selected and calibrated according to the intended application.

14. Revision Questions

  1. What is the purpose of BUZZER_PIN?
  2. Which pin reads the sensor value?
  3. What does analogRead(A0) do?
  4. What is the typical analog reading range on an Arduino Uno?
  5. What happens when the sensor reading is exactly 200?
  6. What happens when the reading is 201?
  7. What does analogWrite() control?
  8. Does analogWrite(..., 50) set the buzzer frequency?
  9. What is the difference between an active and passive buzzer?
  10. Why is delay(1000) used?