CxD Archive
Code: Robotics
Code: Robotics
  • Code Introduction
  • TUTORIALS
    • A. Meet Your Robot
      • A-1 Circuit Board
      • A-2 Motors and Wheels
      • A-3 Other Components
    • B. Hello World Test
      • B-1 Arduino Code Editor
      • B-2 New App Template
      • B-3 Global Variable
      • B-4 Setup Function
      • B-5 Loop Function
      • B-6 Upload App to Robot
      • B-7 Add Sound
      • B-8 Use Button
    • C. Driving and Turning
      • C-1 Driving
      • C-2 Turns (Pivoting)
      • C-3 Test Wheel Encoders
      • C-4 Drive for Specific Distance
      • C-5 Pivot By Specific Angle
      • C-6 Drive Straight Continuously
    • D. Detect Objects in Path
      • D-1 Test Mechanical Bumpers
      • D-2 Detect Collisions
      • D-3 Test Ultrasonic Sensor
      • D-4 Avoid Collisions
    • E. Detect Lines on Surface
      • E-1 Test IR Line Sensors
      • E-2 Follow Line
      • E-3 Avoid Line
      • E-4 Count Lines Crossed
      • E-5 Follow and Count Lines
    • F. Detect Other Conditions
      • F-1 Detect Surface Drop-Off
      • F-2 Test Accelerometer
      • F-3 Detect If Upside-Down
      • F-4 Detect If Bumped
  • REFERENCES
    • Arduino Language
    • Arduino Code Editor
      • Create New App
      • Save and Rename App
      • Include RedBot Library
      • Verify App Code
      • Upload App to Robot
      • Download Copy of App
      • Use Serial Monitor to View Data
      • Robot Demo App Template
    • Physical Inputs
      • Push Button (D12)
      • Mechanical Bumpers
      • IR Line Sensors
      • Wheel Encoders
      • Accelerometer
      • Ultrasonic Sensor *
    • Physical Outputs
      • LED Light (D13)
      • Speaker (Buzzer)
      • Motors
    • Robot Behaviors
      • Producing Alerts
      • Driving
      • Turning
      • Detecting Objects
      • Detecting Lines
      • Detecting Other Conditions
    • Navigation Modes
      • Distance Navigation
      • Line Counting Navigation
      • Line Following + Counting Navigation
      • Autonomous Navigation
  • LINKS
    • Robotics Project Guidebook
    • Arduino Create Web Editor
    • Arduino Language Reference
    • RedBot Experiment Guide
  • ELEGOO Experiments
    • Getting the Arduino IDE
    • Starting with the `techCar` Template
    • Template Code for techCar.ino
    • Template Experiments
    • Reinstall the Original Remote App Functionality, If Needed
Powered by GitBook
On this page
  • How to Code Speaker
  • Declare Variable for Speaker
  • Set Pin Mode for Speaker
  • Produce Tone
Export as PDF
  1. REFERENCES
  2. Physical Outputs

Speaker (Buzzer)

PreviousLED Light (D13)NextMotors

Last updated 6 years ago

The RedBot kit includes a "buzzer" — a small speaker that can produce simple sounds. The speaker can only play one tone (sound) at a time, but you can create different sounds or sound patterns.

The speaker can be used to to people interacting with your robot.

You can also use the speaker to one note at a time.

How to Code Speaker

To use the speaker in your robot app, you will need to:

  1. Declare a gloabl variable to store the speaker's pin number

  2. Set the pin mode for the speaker

  3. Use the tone() method to produce a sound

Declare Variable for Speaker

You'll need to create a global variable to store the pin number of the speaker, which is normally connected to pin 9. Add this code statement before the setup() function:

int speaker = 9;

Set Pin Mode for Speaker

You'll need to set the pin mode for the speaker. Add this code statement within the setup() function:

pinMode(speaker, OUTPUT);

Produce Tone

The tone() method is used to produce a sound of a specific frequency using the speaker.

The frequency should be an integer value (whole number) between 20-20000 Hertz:

  • Lower values (lower frequencies) produce a lower pitched sound (more bass).

  • Higher values (higher frequencies) produce a higher pitched sound (more treble).

To produce a sound that most people can easily hear, use a frequency value between 50 and 8000 Hertz. Try using a value of 3000, and then decide whether you want the sound to have a higher or lower pitch.

Typically, you will only want to play a tone for a certain duration of time and then turn it off.

For example, the following code will use the speaker to play a tone with a frequency of 2000 Hertz for 0.5 seconds and then turn it off:

tone(speaker, 2000);
delay(500); // wait 0.5 seconds
noTone(speaker);

Notice that the noTone() method was used to turn the speaker off. Otherwise, the tone would keep playing continuously.

Alternatively, you can include the duration of the sound (in milliseconds) when using the tone() method. In this case, the tone will automatically turn off once the duration is over:

tone(speaker, 2000, 500); // frequency 2000 Hz, duration 500 ms

VOLUME: There is NOT a way to change the volume of the tone. However, you will notice that certain frequencies will naturally seem louder to your ears.

provide alerts or feedback
play a song